spring boot整合netty总结

spring boot整合netty总结


第一部分 加载


加载可以用2种方法 

1.使用spring 提供的注解

@PostConstruct
public  ChannelFuture start()  {

    logger.warn("tcp server start init");
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ChannelFuture f = null;
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();

                        p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(

                                new ZDWServerHandler());
                    }
                });

        // Start the server.
        f = b.bind(portNumber).sync();
        channel = f.channel();

        logger.warn("tcp server start ok");

        App.Instance().InitDevices();
        App.Instance().InitTimer();


        // Wait until the server socket is closed.

    } catch (Exception e){

        String msg = e.getMessage();
        logger.warn(msg);
    } finally{
        // Shut down all event loops to terminate all threads.

    }
    return f;
}
 
 
2.
Application继承CommandLineRunner 的方式

第二部分
为非spring boot 管理代码注入依赖,非spring boot管理的代码,无法自动注入依赖,目前测试通过的一种方法是通过保存
SpringApplication来通过
getBean获取到对应的依赖
 
 
Spring Boot部分代码
 
 
 
 
 
 
 
 
 
 

 
 

@SpringBootApplication

public class Application   implements ApplicationContextAware {
    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(Application.class);
        app.setWebEnvironment(false);
        app.run(args);
    }


    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        Application.applicationContext = applicationContext;
    }
    /**
     * 注意 bean name默认 = 类名(首字母小写)
     * 例如: A8sClusterDao = getBean("k8sClusterDao")
     * @param name
     * @return
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    /**
     * 根据类名获取到bean
     * @param <T>
     * @param clazz
     * @return
     * @throws BeansException
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBeanByName(Class<T> clazz) throws BeansException {
        try {
            char[] cs=clazz.getSimpleName().toCharArray();
            cs[0] += 32;//首字母大写到小写
            return (T) applicationContext.getBean(String.valueOf(cs));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }


}
 
 
使用依赖的代码部分
 
 
 
 

deviceStatusService = Application.getBeanByName(DeviceStatusService.class);
 
 
 
 
目前使用中遇到的问题列表
1.在尝试将共用的jpa代码放到单独的module时,web引用OK,netty目前没有搞定
2.scan的basedir参数,添加jpa所在的包会导致运行出错。
 
 

猜你喜欢

转载自blog.csdn.net/w18637468680/article/details/78664799
今日推荐