启动事件

实现ApplicationListener ContextRefreshedEvent

@Service
public class StartAddDataListener  implements ApplicationListener<ContextRefreshedEvent> {
    private Logger logger= LoggerFactory.getLogger(StartAddDataListener.class);
    @Autowired
    TCPServer  tcpServer;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){
            logger.info("spring Start Success");
            NettyStartService nettyStartService=new NettyStartService();
            nettyStartService.setTcpServer(tcpServer);
            new Thread(nettyStartService).start();
        }
    }
}

关闭事件

implements ApplicationListener

@Service
public class StopAddDataListener implements ApplicationListener<ContextClosedEvent> {
        private Logger logger= LoggerFactory.getLogger(StartAddDataListener.class);
        @Autowired
        TCPServer  tcpServer;

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        if(contextClosedEvent.getApplicationContext().getParent() == null) {
            NettyStopService nettyStopService = new NettyStopService();
            nettyStopService.setTcpServer(tcpServer);
            new Thread(nettyStopService).start();
        }
    }
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐