• 用spring的 InitializingBean 的 afterPropertiesSet 来初始化


     

     
    void afterPropertiesSet() throws Exception;

    这个方法将在所有的属性被初始化后调用。

    但是会在init前调用。

    但是主要的是如果是延迟加载的话,则马上执行。

    所以可以在类上加上注解:

    import org.springframework.context.annotation.Lazy;

    @Lazy(false)

    这样spring容器初始化的时候afterPropertiesSet就会被调用。

    只需要实现InitializingBean接口就行。

    复制代码
    public class MessageRecvExecutor implements ApplicationContextAware, InitializingBean {
    
        private String serverAddress;
        private final static String DELIMITER = ":";
    
        private Map<String, Object> handlerMap = new ConcurrentHashMap<String, Object>();
    
        private static ThreadPoolExecutor threadPoolExecutor;
    
        public MessageRecvExecutor(String serverAddress) {
            this.serverAddress = serverAddress;
        }
    
        public static void submit(Runnable task) {
            if (threadPoolExecutor == null) {
                synchronized (MessageRecvExecutor.class) {
                    if (threadPoolExecutor == null) {
                        threadPoolExecutor = (ThreadPoolExecutor) RpcThreadPool.getExecutor(16, -1);
                    }
                }
            }
            threadPoolExecutor.submit(task);
        }
    
        public void setApplicationContext(ApplicationContext ctx) throws BeansException {
            try {
                MessageKeyVal keyVal = (MessageKeyVal) ctx.getBean(Class.forName("newlandframework.netty.rpc.model.MessageKeyVal"));
                Map<String, Object> rpcServiceObject = keyVal.getMessageKeyVal();
    
                Set s = rpcServiceObject.entrySet();
                Iterator<Map.Entry<String, Object>> it = s.iterator();
                Map.Entry<String, Object> entry;
    
                while (it.hasNext()) {
                    entry = it.next();
                    handlerMap.put(entry.getKey(), entry.getValue());
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(MessageRecvExecutor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public void afterPropertiesSet() throws Exception {
            //netty的线程池模型设置成主从线程池模式,这样可以应对高并发请求
            //当然netty还支持单线程、多线程网络IO模型,可以根据业务需求灵活配置
            ThreadFactory threadRpcFactory = new NamedThreadFactory("NettyRPC ThreadFactory");
            
            //方法返回到Java虚拟机的可用的处理器数量
            int parallel = Runtime.getRuntime().availableProcessors() * 2;
        
            EventLoopGroup boss = new NioEventLoopGroup();
            EventLoopGroup worker = new NioEventLoopGroup(parallel,threadRpcFactory,SelectorProvider.provider());
            
            try {
                ServerBootstrap bootstrap = new ServerBootstrap();
                bootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
                        .childHandler(new MessageRecvChannelInitializer(handlerMap))
                        .option(ChannelOption.SO_BACKLOG, 128)
                        .childOption(ChannelOption.SO_KEEPALIVE, true);
    
                String[] ipAddr = serverAddress.split(MessageRecvExecutor.DELIMITER);
    
                if (ipAddr.length == 2) {
                    String host = ipAddr[0];
                    int port = Integer.parseInt(ipAddr[1]);
                    ChannelFuture future = bootstrap.bind(host, port).sync();
                    System.out.printf("[author tangjie] Netty RPC Server start success ip:%s port:%d
    ", host, port);
                    future.channel().closeFuture().sync();
                } else {
                    System.out.printf("[author tangjie] Netty RPC Server start fail!
    ");
                }
            } finally {
                worker.shutdownGracefully();
                boss.shutdownGracefully();
            }
        }
    }
    复制代码
  • 相关阅读:
    从Github上将laravel项目拉到新开发环境
    Nginx-Primary script unknown的报错的解决方法
    CentOS 7 安装PHP7+Nginx+Mysql5.7开发环境
    程序员面试经常会被问到的12个问题
    IOC(控制反转)的理解
    用冒泡排序的方法将数组从小到大排列
    常用设计模式详解
    PHP常见面试题总结
    能够遍历一个文件夹下的所有文件和子文件夹的函数
    线特征---LineMatching原理(四)
  • 原文地址:https://www.cnblogs.com/findbetterme/p/11523346.html
Copyright © 2020-2023  润新知