nsq消息队列 初始2
This commit is contained in:
@@ -9,21 +9,21 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
@Service
|
//@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class NsqConfig {
|
public class NsqConfig {
|
||||||
// /**
|
/**
|
||||||
// * 端口号
|
* 端口号
|
||||||
// */
|
*/
|
||||||
// @Value("${nsq.port}")
|
@Value("${nsq.port.consumer}")
|
||||||
// private int port;
|
private int port;
|
||||||
//
|
|
||||||
// @Value("${nsq.address}")
|
@Value("${nsq.address}")
|
||||||
// private String address;
|
private String address;
|
||||||
//
|
|
||||||
// @PostConstruct
|
// @PostConstruct
|
||||||
// public void startNSQChannel() {
|
public void startNSQChannel() {
|
||||||
// new NSQConsumers(address, port, "test", "ch1", new Print2MessageHandlerAdapter());
|
new NSQConsumers(address, port, "test", "ch1", new Print2MessageHandlerAdapter());
|
||||||
// new NSQConsumers(address, port, "test", "ch2", new PrintMessageHandlerAdapter());
|
new NSQConsumers(address, port, "test", "ch2", new PrintMessageHandlerAdapter());
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/main/java/com/sqx/nsqChannel/config/NsqProduce.java
Normal file
53
src/main/java/com/sqx/nsqChannel/config/NsqProduce.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package com.sqx.nsqChannel.config;
|
||||||
|
|
||||||
|
import com.github.brainlag.nsq.NSQProducer;
|
||||||
|
import com.github.brainlag.nsq.exceptions.NSQException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
//@Component
|
||||||
|
public class NsqProduce {
|
||||||
|
@Value("${nsq.port.produce}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Value("${nsq.address}")
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
|
||||||
|
private NSQProducer producer;
|
||||||
|
private static boolean is_start = false;
|
||||||
|
private static final byte[] LOCK = new byte[0];
|
||||||
|
|
||||||
|
public NsqProduce() {
|
||||||
|
producer = new NSQProducer();
|
||||||
|
producer.addAddress(address, port).start();
|
||||||
|
is_start = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NSQProducer getProducer() {
|
||||||
|
if (!is_start) {
|
||||||
|
log.info("========================NSQProduce no start====================");
|
||||||
|
}
|
||||||
|
if (producer == null) {
|
||||||
|
synchronized (LOCK) {
|
||||||
|
if (producer == null) {
|
||||||
|
producer = new NSQProducer();
|
||||||
|
producer.addAddress(address, port).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return producer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMsgToTestTopic(String topic,String msg) {
|
||||||
|
try {
|
||||||
|
this.getProducer().produce(topic, msg.getBytes());
|
||||||
|
} catch (NSQException | TimeoutException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/main/java/com/sqx/nsqChannel/enums/TopicEnums.java
Normal file
4
src/main/java/com/sqx/nsqChannel/enums/TopicEnums.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.sqx.nsqChannel.enums;
|
||||||
|
|
||||||
|
public enum TopicEnums {
|
||||||
|
}
|
||||||
@@ -10,7 +10,9 @@ server:
|
|||||||
|
|
||||||
nsq:
|
nsq:
|
||||||
address: 47.122.26.160
|
address: 47.122.26.160
|
||||||
port: 4161
|
port:
|
||||||
|
produce: 4150
|
||||||
|
consumer: 4161
|
||||||
|
|
||||||
# 数据源的一些配置
|
# 数据源的一些配置
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ server:
|
|||||||
|
|
||||||
nsq:
|
nsq:
|
||||||
address: 47.122.26.160
|
address: 47.122.26.160
|
||||||
port: 4161
|
port:
|
||||||
|
produce: 4150
|
||||||
|
consumer: 4161
|
||||||
|
|
||||||
# 数据源的一些配置
|
# 数据源的一些配置
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ server:
|
|||||||
|
|
||||||
nsq:
|
nsq:
|
||||||
address: 127.0.0.1
|
address: 127.0.0.1
|
||||||
port: 4161
|
port:
|
||||||
|
produce: 4150
|
||||||
|
consumer: 4161
|
||||||
|
|
||||||
# 数据源的一些配置
|
# 数据源的一些配置
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|||||||
Reference in New Issue
Block a user