上饶警务-警车数据读取服务(udp)
zhongrj
2023-08-18 982bf012d98f9a77462ad69cdf3a0d0c8d628684
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
package org.springblade.modules.netty.event;
 
 
import org.springblade.modules.netty.config.SysConfig;
import org.springblade.modules.netty.server.UdpServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
 
/**
 * netty udp 服务启动监听
 * @author zhongrj
 * @date 2023-02-21
 */
@Component
public class StartupEvent implements ApplicationListener<ContextRefreshedEvent> {
 
    private static ApplicationContext context;
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            context = contextRefreshedEvent.getApplicationContext();
            SysConfig sysConfig = (SysConfig) context.getBean(SysConfig.class);
            //获取 udpServer
            UdpServer udpServer = (UdpServer) StartupEvent.getBean(UdpServer.class);
            //开启
            udpServer.run(sysConfig.getUdpReceivePort());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static Object getBean(Class beanName) {
        return context != null ? context.getBean(beanName) : null;
    }
}