|
@@ -14,6 +14,7 @@ import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
|
|
import io.netty.handler.codec.LengthFieldPrepender;
|
|
|
import io.netty.handler.codec.string.StringDecoder;
|
|
|
import io.netty.handler.codec.string.StringEncoder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -28,13 +29,14 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
* @date 2020年8月31日 上午10:34:45
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class GroupNettyClient {
|
|
|
@Autowired
|
|
|
AlarmDefineService alarmDefineService;
|
|
|
@Autowired
|
|
|
AlarmRecordRepository alarmRecordRepository;
|
|
|
static Bootstrap groupBootstrap = new Bootstrap();
|
|
|
- static Channel channelGroup;
|
|
|
+ public static Channel channelGroup;
|
|
|
@Value("${group.alarm.host:127.0.0.1}")
|
|
|
public String host;
|
|
|
@Value("${group.alarm.port}")
|
|
@@ -102,14 +104,14 @@ public class GroupNettyClient {
|
|
|
|
|
|
public void connect(GroupNettyClient groupNettyClient, Channel channel) {
|
|
|
// 加入断线后自动重连监听器
|
|
|
- System.out.println("try connect!");
|
|
|
+ log.info("try connect!");
|
|
|
for (Map.Entry<String, Channel> entry : channelsMap.entrySet()) {
|
|
|
if (entry.getValue() == channel && !channel.isActive()) {
|
|
|
channelGroup = groupBootstrap.connect(groupNettyClient.host, groupNettyClient.port).addListener(new ChannelFutureListener() {
|
|
|
@Override
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
if (future.cause() != null) {
|
|
|
- System.out.println("Failed to connect: " + future.cause());
|
|
|
+ log.info("Failed to connect: " + future.cause());
|
|
|
}
|
|
|
}
|
|
|
}).channel();
|
|
@@ -126,11 +128,11 @@ public class GroupNettyClient {
|
|
|
/* public void sendMessage(Object msg,String type) {
|
|
|
for (Map.Entry<String, Channel> ch:channelsMap.entrySet() ) {
|
|
|
if(type.equals(ch.getKey())) {
|
|
|
- System.out.println(msg);
|
|
|
- System.out.println(ch.getKey());
|
|
|
+ log.info(msg);
|
|
|
+ log.info(ch.getKey());
|
|
|
ch.getValue().writeAndFlush(msg);
|
|
|
}else {
|
|
|
- System.out.println("----to group --");
|
|
|
+ log.info("----to group --");
|
|
|
ch.getValue().writeAndFlush(msg);
|
|
|
}
|
|
|
}
|