|
@@ -1,133 +0,0 @@
|
|
|
-package com.tidecloud.dataacceptance.service;
|
|
|
-
|
|
|
-import java.util.Set;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.LinkedBlockingQueue;
|
|
|
-import java.util.concurrent.ThreadFactory;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-
|
|
|
-import org.apache.tomcat.util.threads.ThreadPoolExecutor;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
|
-import com.smartsanitation.common.util.StringUtil;
|
|
|
-import com.tidecloud.dataacceptance.common.Constants;
|
|
|
-import com.tidecloud.dataacceptance.entity.ConnectMsg;
|
|
|
-import com.tidecloud.dataacceptance.service.handle.YiTongGpsServerHandler;
|
|
|
-
|
|
|
-import io.netty.bootstrap.ServerBootstrap;
|
|
|
-import io.netty.buffer.Unpooled;
|
|
|
-import io.netty.channel.ChannelFuture;
|
|
|
-import io.netty.channel.ChannelInitializer;
|
|
|
-import io.netty.channel.ChannelOption;
|
|
|
-import io.netty.channel.EventLoopGroup;
|
|
|
-import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
-import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
-import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
|
|
-import io.netty.handler.timeout.IdleStateHandler;
|
|
|
-import redis.clients.jedis.Jedis;
|
|
|
-import redis.clients.jedis.JedisPool;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author: chudk
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class AcceptanceService {
|
|
|
-
|
|
|
- @Value("${server.netty.port}")
|
|
|
- private Integer port;
|
|
|
-
|
|
|
- @Value("${server.localaddress}")
|
|
|
- private String address;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private YiTongGpsServerHandler yiTongGpsServerHandler;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private JedisPool jedisPool;
|
|
|
-
|
|
|
- private static final Logger LOG = LoggerFactory.getLogger(AcceptanceService.class);
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void run() throws Exception {
|
|
|
- cleanRedisLinkData();
|
|
|
- ThreadFactory nettyThreadFactory = new ThreadFactoryBuilder().setNameFormat("netty-pool-%d").build();
|
|
|
- ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
|
|
|
- new LinkedBlockingQueue<Runnable>(1024), nettyThreadFactory, new java.util.concurrent.ThreadPoolExecutor.AbortPolicy());
|
|
|
- singleThreadPool.execute(new Runnable() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- EventLoopGroup bossGroup = new NioEventLoopGroup();
|
|
|
- EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
|
- byte[] splitBytes1 = new byte[]{0x7e};
|
|
|
- byte[] splitBytes2 = new byte[]{0x7e, 0x7e};
|
|
|
- try {
|
|
|
- ServerBootstrap b = new ServerBootstrap();
|
|
|
- b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
|
|
- .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
- ch.pipeline().addLast("idleStateHandler",
|
|
|
- new IdleStateHandler(Constants.TCP_CLIENT_IDLE_MINUTES, 0, 0, TimeUnit.MINUTES));
|
|
|
-
|
|
|
- ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1204, Unpooled.copiedBuffer(splitBytes1),
|
|
|
- Unpooled.copiedBuffer(splitBytes2)));
|
|
|
- ch.pipeline().addLast(yiTongGpsServerHandler);
|
|
|
- }
|
|
|
- }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
|
|
|
- ChannelFuture f = b.bind(port).sync();
|
|
|
- f.channel().closeFuture().sync();
|
|
|
-
|
|
|
- } catch (InterruptedException e) {
|
|
|
- LOG.error("netty server InterruptedException:"+e.getMessage());
|
|
|
- } finally {
|
|
|
- // cleanRedisLinkData();
|
|
|
- workerGroup.shutdownGracefully();
|
|
|
- bossGroup.shutdownGracefully();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
- singleThreadPool.shutdown();
|
|
|
- }
|
|
|
-
|
|
|
- private void cleanRedisLinkData() {
|
|
|
- Jedis jedis = null;
|
|
|
- try {
|
|
|
- jedis = jedisPool.getResource();
|
|
|
- jedis.select(YiTongGpsServerHandler.REDIS_INDEX_LINK);
|
|
|
- String addressStr = ConnectMsg.ipToLong(address);
|
|
|
- String selectKey = YiTongGpsServerHandler.PREFIX_LINK_BACK + addressStr;
|
|
|
- Set<String> values = jedis.smembers(selectKey);
|
|
|
-
|
|
|
- for (String deviceId : values) {
|
|
|
- String deleteKeyOfDevice = YiTongGpsServerHandler.PREFIX_DEVICE + deviceId;
|
|
|
- String deleteKeyOfLink = YiTongGpsServerHandler.PREFIX_LINK + addressStr;
|
|
|
- String connectMsgStr = jedis.get(deleteKeyOfDevice);
|
|
|
- if (connectMsgStr != null) {
|
|
|
- ConnectMsg connectMsg = StringUtil.convert2Object(connectMsgStr, ConnectMsg.class);
|
|
|
- String socketId = connectMsg.getSocketId();
|
|
|
- jedis.del(deleteKeyOfDevice);
|
|
|
- jedis.srem(deleteKeyOfLink, socketId);
|
|
|
- }else {
|
|
|
- LOG.error("error deviceId [{}] in select [{}] key [{}]", deviceId, 15, deleteKeyOfDevice);
|
|
|
- }
|
|
|
- }
|
|
|
- jedis.del(selectKey);
|
|
|
- } catch (Exception e) {
|
|
|
- LOG.error(e.getLocalizedMessage());
|
|
|
- } finally {
|
|
|
- if (jedis != null) {
|
|
|
- jedisPool.returnResource(jedis);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|