springContextStati是一个实现了Applicationcontextaware的类的名字,可以getBean(..)RegistryTask是一个在RegistryServer调用netty封装的channelinitializer时候,的clienthandler被触发读事件的时候 new 的
@Component @DependsOn(value={"springContextStatic","normalConfig","channelCache","serviceAddrCache"})DependsOn的参数都是String字符串,类的名称,后面在RegistryTask的构造函数中,必须用appcontext.getBean(String name)获得bean,否则阻塞。 public class RegistryServer { ..... ...... @PostConstruct private void doRunServer() { try { 此处被注释的代码部分如果不执行,那么后面一个RegistryTask阻塞在构造函数上 执行此处getBean(Class<T>)代码的意义是什么?加载?玄。。 /*LOGGER.info("doRunServer:appctx:{}",SpringContextStatic.getApplicationContext()); LOGGER.info("normalconfig:{}",SpringContextStatic.getBean(NormalConfig.class)); LOGGER.info("channelcache:{}",SpringContextStatic.getBean(ChannelCache.class)); LOGGER.info("serviceaddrcache:{}",SpringContextStatic.getBean(ServiceAddrCache.class));*/ //创建并初始化 Netty 服务端辅助启动对象 ServerBootstrap ServerBootstrap serverBootstrap = initServerBootstrap(bossGroup, workerGroup); //绑定对应ip和端口,同步等待成功 ChannelFuture future = serverBootstrap.bind(paramConfig.getServerport()).sync(); LOGGER.info("regist server 已启动,端口:{}", paramConfig.getServerport()); //等待服务端监听端口关闭 future.channel().closeFuture().sync(); } catch (InterruptedException i) { LOGGER.error("regist server 出现异常,端口:{}, cause:", paramConfig.getServerport(), i.getMessage()); } finally { //优雅退出,释放 NIO 线程组 workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } /** * 创建并初始化 Netty 服务端辅助启动对象 ServerBootstrap * * @param bossGroup * @param workerGroup * @return */ private ServerBootstrap initServerBootstrap(EventLoopGroup bossGroup, EventLoopGroup workerGroup) { return new ServerBootstrap() .group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ServerChannelInitializer(new RegistryServerHandler(threadPool))); } }
public class RegistryTask implements Runnable { private static Logger log = LoggerFactory.getLogger(RegistryTask.class); private ChannelHandlerContext ctx; private RegistryMessage msg; private ChannelCache channelCache = null; private ServiceAddrCache serviceAddrCache = null; private NormalConfig normalConfig = null; /*private ChannelCache channelCache = SpringContextStatic.getBean(ChannelCache.class); private ServiceAddrCache serviceAddrCache = SpringContextStatic.getBean(ServiceAddrCache.class); private NormalConfig normalConfig = SpringContextStatic.getBean(NormalConfig.class);*/ public RegistryTask(ChannelHandlerContext ctx, RegistryMessage msg) { System.out.println("constructing registrytask,appctx:"+SpringContextStatic.getApplicationContext()); log.info("doRunServer:appctx:{}",SpringContextStatic.getApplicationContext()); log.info("normalconfig:{}",SpringContextStatic.getBean("normalConfig")); /*log.info("channelcache:{}",SpringContextStatic.getBean(ChannelCache.class)); log.info("serviceaddrcache:{}",SpringContextStatic.getBean(ServiceAddrCache.class));*/ this.ctx = ctx; this.msg = msg; 上一个文件的被注释部分getBean(Class<T>)如果不执行,此处会阻塞,why? /*this.channelCache = SpringContextStatic.getBean(ChannelCache.class); this.serviceAddrCache = SpringContextStatic.getBean(ServiceAddrCache.class); this.normalConfig = SpringContextStatic.getBean(NormalConfig.class);*/ 正确写法:getBean(String name) this.channelCache = (ChannelCache) SpringContextStatic.getBean("channelCache"); this.serviceAddrCache = (ServiceAddrCache) SpringContextStatic.getBean("serviceAddrCache"); this.normalConfig = (NormalConfig) SpringContextStatic.getBean("normalConfig"); }
原文地址:https://www.cnblogs.com/CreatorKou/p/11354682.html
时间: 2024-10-11 03:01:55