activemq分析(一)启动流程

简介:

activemq 启动是非常简单的。

1.从二进制发布包启动

On Windows:

1
2
cd  [activemq_install_dir]
bin\activemq start

2.从源码方式启动

wKioL1eCJReRNI5JAAA6BdItzjs378.png

Main class    :    org.apache.activemq.console.Main

arguments    :    start xbean:activemq2.xml


其中activemq2.xml内容一个最简单的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< beans
   xmlns = "http://www.springframework.org/schema/beans"
   xmlns:amq = "http://activemq.apache.org/schema/core"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
   http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">
   <!-- Allows us to use system properties as variables in this configuration file -->
   < bean  class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
   < broker  xmlns = "http://activemq.apache.org/schema/core"  brokerName = "localhost"  dataDirectory = "${activemq.base}/data" >
     <!-- The transport connectors ActiveMQ will listen to -->
     < transportConnectors >
        < transportConnector  name = "openwire"  uri = "tcp://localhost:61616"  />
     </ transportConnectors >
   </ broker >
</ beans >

命令列表

有很多命令,启动命令只是其中之一

1
2
3
4
5
6
7
8
9
10
11
12
Tasks:
     browse                   - Display selected messages  in  a specified destination.
     bstat                    - Performs a predefined query that displays useful statistics regarding the specified broker
     create                   - Creates a runnable broker instance  in  the specified path.
     decrypt                  - Decrypts given text
     encrypt                  - Encrypts given text
     export                    - Exports a stopped brokers data files to an archive  file
     list                     - Lists all available brokers  in  the specified JMX context
     purge                    - Delete selected destination's messages that matches the message selector
     query                    - Display selected broker component's attributes and statistics.
     start                    - Creates and starts a broker using a configuration  file , or a broker URI.
     stop                     - Stops a running broker specified by the broker name.


启动过程序列图

wKioL1eCJaripXE-AADY9Ae-ntM860.png


通过分析activemq*.jar,查看main方法入口。定位到org.apache.activemq.console.Main。

通过反射,调用ShellCommand的main方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public  static  final  String TASK_DEFAULT_CLASS =  "org.apache.activemq.console.command.ShellCommand" ;
public  void  runTaskClass(List<String> tokens)  throws  Throwable {
...
     ClassLoader cl = getClassLoader();
     Thread.currentThread().setContextClassLoader(cl);
 
     // Use reflection to run the task.
     try  {
         String[] args = tokens.toArray( new  String[tokens.size()]);
         Class<?> task = cl.loadClass(TASK_DEFAULT_CLASS);
         Method runTask = task.getMethod( "main" new  Class[] {
             String[]. class , InputStream. class , PrintStream. class
         });
         runTask.invoke(task.newInstance(), args, System.in, System.out);
     catch  (InvocationTargetException e) {
         throw  e.getCause();
     }
}


步骤2

org.apache.activemq.console.command.ShellCommand :main方法

构造运行上下文及Formatter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  static  int  main(String[] args, InputStream in, PrintStream out) {
     CommandContext context =  new  CommandContext();
     context.setFormatter( new  CommandShellOutputFormatter(out));
     // Convert arguments to list for easier management
     List<String> tokens =  new  ArrayList<String>(Arrays.asList(args));
     ShellCommand main =  new  ShellCommand();
     try  {
         main.setCommandContext(context);
         main.execute(tokens);
         return  0 ;
     catch  (Exception e) {
         context.printException(e);
         return  - 1 ;
     }
}

步骤3:

org.apache.activemq.console.command.ShellCommand  :runTask

找出适配的Command ,处理请求


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
protected  void  runTask(List<String> tokens)  throws  Exception {
     // Process task token
     if  (tokens.size() >  0 ) {
         Command command= null ;
         String taskToken = (String)tokens.remove( 0 );
 
         for ( Command c: getCommands() ) {
             if ( taskToken.equals(c.getName()) ) { //根据每个command的名称与参数比对
                 command = c;
                 break ;
             }
         }
         if ( command ==  null  ) {
             if  (taskToken.equals( "help" )) {
                 printHelp();
             else  {
                 printHelp();
             }
         }
 
         if ( command!= null  ) {
             command.setCommandContext(context);
             command.execute(tokens);
         }
     else  {
         printHelp();
     }
}

步骤5:org.apache.activemq.console.command.StartCommand : runTask

启动broker,先判断参数是否有brokerURI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
protected  void  runTask(List<String> brokerURIs)  throws  Exception {
     try  {
         // If no config uri, use default setting
         if  (brokerURIs.isEmpty()) {
             setConfigUri( new  URI(DEFAULT_CONFIG_URI));
             startBroker(getConfigUri());
             // Set configuration data, if available, which in this case
             // would be the config URI
         else  {
             String strConfigURI;
             while  (!brokerURIs.isEmpty()) {
                 strConfigURI = (String)brokerURIs.remove( 0 );
                 try  {
                     setConfigUri( new  URI(strConfigURI));
                 catch  (URISyntaxException e) {
                     context.printException(e);
                     return ;
                 }
 
                 startBroker(getConfigUri());
             }
         }
 
         // Prevent the main thread from exiting unless it is terminated
         // elsewhere
     catch  (Exception e) {
         context.printException( new  RuntimeException( "Failed to execute start task. Reason: "  + e, e));
         throw  new  Exception(e);
     }
     
     // The broker start up fine.  If this unblocks it's cause they were stopped
     // and this would occur because of an internal error (like the DB going offline)
     waitForShutdown();
}




类图

wKioL1eCKCOh9KG_AAA5MR9CoAA440.png



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1817646,如需转载请自行联系原作者

相关文章
|
消息中间件 存储 负载均衡
5 张图带你理解 RocketMQ 消费者启动过程
5 张图带你理解 RocketMQ 消费者启动过程
229 0
5 张图带你理解 RocketMQ 消费者启动过程
|
消息中间件 Java RocketMQ
【Spring常见错误】Rocketmq 闪退
 意思是JAVA_HOME环境变量未找到,可能是环境变量配置问题,有时候配置多个java版本可能会在JAVA_HOME后面加上后缀例如: JAVA_HOME8、JAVA_HOME15  而 JAVA_HOME为配置,从而导致找不到JAVA_HOME环境变量。
875 0
【Spring常见错误】Rocketmq 闪退
|
XML Java 应用服务中间件
Spring源码分析之预启动流程
Spring源码分析之预启动流程
81 0
|
Android开发
RK3399平台开发系列讲解(其他篇)1.3、U-boot启动流程分析
RK3399平台开发系列讲解(其他篇)1.3、U-boot启动流程分析
152 0
|
消息中间件 存储 Java
RocketMQ Broker启动流程梳理
RocketMQ Broker启动流程梳理
328 0
RocketMQ Broker启动流程梳理
|
监控 Dubbo Java
源码分析Dubbo服务消费端启动流程
通过前面文章详解,我们知道Dubbo服务消费者标签dubbo:reference最终会在Spring容器中创建一个对应的ReferenceBean实例,而ReferenceBean实现了Spring生命周期接口:InitializingBean,接下来应该看一下其afterPropertiesSet方法的实现。
1684 0
spring-kafka深入探秘
前言 kafka是一个消息队列产品,基于Topic partitions的设计,能达到非常高的消息发送处理性能。Spring创建了一个项目Spring-kafka,封装了Apache 的Kafka-client,用于在Spring项目里快速集成kafka。
|
消息中间件 JavaScript 网络协议
activeMq安装及原理分析
1.下载地址:activeMQ:window中的apache-activemq-5.15.8-bin.zip2.安装解压压缩包,运行bin中的activemq.bat,qizh,根据本地环境决定运行win64还是win32,结果:访问:http://127.
1902 0
|
Dubbo Java 应用服务中间件
|
消息中间件 Java Apache
SpringBoot整合ActiveMQ:专业负责MQ20年~
ACTIVEMQ ActiveMQ 下载MQ后启动一下(Windows版本) #启动信息 ... jvm 1 | INFO | Listening for connections at ws://JackWen:61614?maximumConnections=1000&wireFormat.
1457 0

热门文章

最新文章