zoukankan      html  css  js  c++  java
  • ApplicationContextAware的作用

    ApplicationContextAware
    其实我们看到---Aware就知道是干嘛用的了,就是属性注入的,

    但是这个ApplicationContextAware的不同地方在于,实现了这个接口的bean,

    当spring容器初始化的时候,会自动的将ApplicationContext注入进来

    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();
            }
        }
    }
  • 相关阅读:
    Android:短信发送
    Android 自制拍照软件
    Android 联系人的读取,查询,添加
    android activity生命周期
    android 让 EditText, TextView自动识别链接
    android Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
    FileOutputStream 读文件的模式
    Android 使用 SharedPreferences 保存和加载软件参数
    使用google的GSON处理JSON
    Android SqlLite数据库的创建、增、删、改、查、使用事务
  • 原文地址:https://www.cnblogs.com/feiyun126/p/7685357.html
Copyright © 2011-2022 走看看