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\n", host, port);
future.channel().closeFuture().sync();
} else {
System.out.printf("[author tangjie] Netty RPC Server start fail!\n");
}
} finally {
worker.shutdownGracefully();
boss.shutdownGracefully();
}
}
}

用spring的 InitializingBean 的 afterPropertiesSet 来初始化的更多相关文章

  1. spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的区别联系

    InitializingBean Spirng的InitializingBean为bean提供了定义初始化方法的方式.InitializingBean是一个接口,它仅仅包含一个方法:afterProp ...

  2. spring中InitializingBean接口使用理解

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  3. 启动就加载(三)initializingbean实现afterPropertiesSet方法

    TransactionTemplate,就直接以TransactionTemplate为入口开始学习. TransactionTemplate的源码如下: public class Transacti ...

  4. spring中InitializingBean接口使用理解(转)

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  5. 【spring】InitializingBean接口

    apollo 源码中有这么一个类 public class ReleaseMessageScanner implements InitializingBean @Override public voi ...

  6. SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据

    在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationRunn ...

  7. 工厂模式-Spring的InitializingBean实现

    一.创建产品角色接口: package org.burning.sport.design.pattern.factorypattern.spring.factory; public interface ...

  8. Spring MVC的handlermapping之SimpleUrlHandlerMapping初始化

    前面信息同BeanNameUrlHandlerMapping,这里不再过多分析,详情请看 :Spring MVC的handlermapping之BeanNameUrlHandlerMapping初始化 ...

  9. Spring MVC的handlermapping之BeanNameUrlHandlerMapping初始化

    先介绍一下: BeanNameUrlHandlerMapping是基于配置文件的方式; 所有处理器需要在XML文件中,以Bean的形式配置. 缺点:配置繁琐; 如果多个URL对应同一个处理器,那么需要 ...

随机推荐

  1. execve(".. ",[".. ",".. "],[/* ..*/])第二个 参数 数组硬传

    strace.第一次跟踪点东西的启动. 第一次看见..execve(".. ",[".. ",".. "],[/* ..*/]) 第二个 参 ...

  2. cin、cout、cerr、clog------c++ Primer Plus

    cin对象与标准输入流相对应. cout对象与标准输出流相对应. cerr对象与标准错误流相对应,常用于程序错误信息,不缓冲,直接被发送给屏幕. clog对象也对应标准错误流(这点儿和cerr是一样的 ...

  3. Django之模型的创建

    在web应用中,经常涉及到和数据库的的交互,比如我们在京东上买一个东西,查询的时候网站会自动转到后端数据库去查询,然后呈现在网页上 Django 里更关注的是模型(Model).模板(Template ...

  4. completer自动完成

    由于项目需要,在输入框中要做一些输入限制的同时,更加要求用户体验,提供一些自动完成设置.所以有需求,总会有解决方式,下面说一下自动完成插件的原理: html的body部分: <span styl ...

  5. 云端搭建Linux学习环境 链接https://edu.aliyun.com/article/19 (阿里云ECS服务器 )课堂

    云端搭建Linux学习环境 链接https://edu.aliyun.com/article/19 1. 开通云服务器 2 1.包年包月   按量付费(适合测试数据的时候) 2 2.地域   服务器数 ...

  6. Linux安装vim编辑器

    1.ubuntu系统:普通用户下输入命令:sudo apt-get install vim-gtk (注:出现E: Unable to locate package则将命令改成sudo apt-get ...

  7. springboot整合dubbo注解方式(三)

    笔者用自己代码测试时候发现这个版本信息,可以没有,也可以制定一个就可以了,没有太大的问题,只是需要注意引入的依赖问题,两个jar与一个jar的问题, 然后就是接口公共依赖问题,案例是把我找的几个案例进 ...

  8. php curl 请求api 接口

    官方文档curl : http://www.runoob.com/php/php-ref-curl.html php开启curl :  ① 打开 php.ini 文件  extension=php_c ...

  9. Vue全家桶了解一下(待补充)

    vue全家桶了解一下 一.vue+vue-router+vuex+axios1.vue:使用vue-cli,生成最基本的vue项目2.vue-router:vue项目中的路由管理插件3.vuex:vu ...

  10. ORA-14517: Subpartition of index "string.string" is in unusable state

    今天碰到个ORA-03113, 原因不明. 猜测因为某些table DDL操作过后导致index unuable的case, 然后进行analyze table, 再碰到ORA-14517.  最后通 ...