HeartBeatServer.java
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.crossoverjie.netty.action.server;
import com.alibaba.fastjson.JSON;
import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import com.crossoverjie.netty.action.util.NettySocketHolder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;
/**
* Function:
*
* @author crossoverJie
* Date: 21/05/2018 00:30
* @since JDK 1.8
*/
@Component
public class HeartBeatServer {
private final static Logger LOGGER = LoggerFactory.getLogger(HeartBeatServer.class);
private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup work = new NioEventLoopGroup();
@Value("${netty.server.port}")
private int nettyPort ;
private NioServerSocketChannel channel ;
/**
* 启动 Netty
* @return
* @throws InterruptedException
*/
@PostConstruct
public void start() throws InterruptedException {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(boss, work)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(nettyPort))
//保持长连接
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new HeartbeatInitializer());
ChannelFuture future = bootstrap.bind().sync();
if (future.isSuccess()){
LOGGER.info("启动 Netty 成功");
}
channel = (NioServerSocketChannel) future.channel() ;
}
/**
* 销毁
*/
@PreDestroy
public void destroy(){
boss.shutdownGracefully().syncUninterruptibly() ;
work.shutdownGracefully().syncUninterruptibly() ;
LOGGER.info("关闭 Netty 成功");
}
/**
* 发送消息
* @param customProtocol
*/
public void sendMsg(CustomProtocol customProtocol){
NioSocketChannel socketChannel = NettySocketHolder.get(customProtocol.getId());
if (null == socketChannel){
throw new NullPointerException("没有["+customProtocol.getId()+"]的socketChannel") ;
}
ChannelFuture future = socketChannel.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8));
future.addListener((ChannelFutureListener) channelFuture ->
LOGGER.info("服务端手动发消息成功={}", JSON.toJSONString(customProtocol)));
}
}