Spring boot integration netty summary

Spring boot integration netty summary


Loading the first part


Loading can be done in 2 ways 

1. Use the annotations provided by 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.
How Application inherits CommandLineRunner

the second part
Inject dependencies for non-spring boot management code. Non-spring boot management code cannot automatically inject dependencies. Currently, one way to pass the test is to save
SpringApplication to pass
getBean gets the corresponding dependencies
 
 
Spring Boot part of the code
 
 
 
 
 
 
 
 
 
 

 
 

@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所在的包会导致运行出错。
 
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325954922&siteId=291194637