首页

分享dubbo源码中关于如何自定义启动SpringContainer容器作服务器的示例

标签:SpringContainer,容器服务器,ClassPathXmlApplicationContext,alibaba,消息中间件,设计,实现原理     发布时间:2017-12-03   

一、前言

这边通过分享alibaba的dubbo源码包中定义SpringContainer作为服务容器来启动运行的源码实例,主要源码类:com.alibaba.dubbo.container.Container、com.alibaba.dubbo.container.spring.SpringContainer,主要应用场景自定义容器应用服务器等。

二、源码说明

1.服务器容器定义Container

package com.alibaba.dubbo.container;@b@@b@import com.alibaba.dubbo.common.extension.SPI;@b@@b@@SPI("spring")@b@public abstract interface Container@b@{@b@  public abstract void start();@b@@b@  public abstract void stop();@b@}

2.基于spring的容器实现类SpringContainer

package com.alibaba.dubbo.container.spring;@b@@b@import com.alibaba.dubbo.common.logger.Logger;@b@import com.alibaba.dubbo.common.logger.LoggerFactory;@b@import com.alibaba.dubbo.common.utils.ConfigUtils;@b@import com.alibaba.dubbo.container.Container;@b@import org.springframework.context.support.ClassPathXmlApplicationContext;@b@@b@public class SpringContainer@b@  implements Container@b@{@b@  private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class);@b@  public static final String SPRING_CONFIG = "dubbo.spring.config";@b@  public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";@b@  static ClassPathXmlApplicationContext context;@b@@b@  public static ClassPathXmlApplicationContext getContext()@b@  {@b@    return context;@b@  }@b@@b@  public void start() {@b@    String configPath = ConfigUtils.getProperty("dubbo.spring.config");@b@    if ((configPath == null) || (configPath.length() == 0))@b@      configPath = "classpath*:META-INF/spring/*.xml";@b@@b@    context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"));@b@    context.start();@b@  }@b@@b@  public void stop() {@b@    try {@b@      if (context != null) {@b@        context.stop();@b@        context.close();@b@        context = null;@b@      }@b@    } catch (Throwable e) {@b@      logger.error(e.getMessage(), e);@b@    }@b@  }@b@}

运行启动后日志如下 - 会启动DefaultLifecycleProcessor处理服务器

2017-12-03 23:07:53 INFO  [main] org.springframework.context.support.ClassPathXmlApplicationContext  - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@578088c0: startup date [Sun Dec 03 23:07:53 CST 2017]; root of context hierarchy@b@2017-12-03 23:07:54 INFO  [main] org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [C:\WS\NJ\project\xwood-project\RDC\bin\spring\spring-config.xml]@b@2017-12-03 23:07:54 INFO  [main] org.springframework.scheduling.quartz.SchedulerFactoryBean  - Spring's Quartz 1.x support is deprecated - please upgrade to Quartz 2.0+@b@2017-12-03 23:07:54 INFO  [main] org.quartz.core.QuartzScheduler  - Quartz Scheduler v.1.6.0 created.@b@2017-12-03 23:07:54 INFO  [main] org.quartz.simpl.RAMJobStore  - RAMJobStore initialized.@b@2017-12-03 23:07:54 INFO  [main] org.quartz.impl.StdSchedulerFactory  - Quartz scheduler 'clearupQuertz' initialized from an externally provided properties instance.@b@2017-12-03 23:07:54 INFO  [main] org.quartz.impl.StdSchedulerFactory  - Quartz scheduler version: 1.6.0@b@2017-12-03 23:07:54 INFO  [main] org.quartz.core.QuartzScheduler  - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@564ac216@b@2017-12-03 23:07:54 INFO  [main] org.springframework.scheduling.quartz.SchedulerFactoryBean  - Spring's Quartz 1.x support is deprecated - please upgrade to Quartz 2.0+@b@2017-12-03 23:07:54 INFO  [main] org.quartz.core.QuartzScheduler  - Quartz Scheduler v.1.6.0 created.@b@2017-12-03 23:07:54 INFO  [main] org.quartz.simpl.RAMJobStore  - RAMJobStore initialized.@b@2017-12-03 23:07:54 INFO  [main] org.quartz.impl.StdSchedulerFactory  - Quartz scheduler 'sQuertz' initialized from an externally provided properties instance.@b@2017-12-03 23:07:54 INFO  [main] org.quartz.impl.StdSchedulerFactory  - Quartz scheduler version: 1.6.0@b@2017-12-03 23:07:54 INFO  [main] org.quartz.core.QuartzScheduler  - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@39697b67@b@2017-12-03 23:07:54 INFO  [main] org.springframework.context.support.DefaultLifecycleProcessor  - Starting beans in phase 2147483647@b@2017-12-03 23:07:54 INFO  [main] org.springframework.scheduling.quartz.SchedulerFactoryBean  - Starting Quartz Scheduler now@b@2017-12-03 23:07:54 INFO  [main] org.quartz.core.QuartzScheduler  - Scheduler clearupQuertz_$_NON_CLUSTERED started.@b@2017-12-03 23:07:54 INFO  [main] org.springframework.scheduling.quartz.SchedulerFactoryBean  - Starting Quartz Scheduler now@b@2017-12-03 23:07:54 INFO  [main] org.quartz.core.QuartzScheduler  - Scheduler sQuertz_$_NON_CLUSTERED started.@b@2017-12-03 23:07:54 INFO  [main] org.springframework.context.support.DefaultLifecycleProcessor  - Starting beans in phase 2147483647