ActiveMQ实战篇之 java和spring xml创建Broker(一)

本文探讨如何使用纯Java代码和Spring XML配置来创建ActiveMQ Broker。通过这种方式,可以深入了解ActiveMQ的工作原理。在Java实现中,启动Broker后需额外代码以确保Jetty服务器运行。而在Spring XML实现中,需配置Factory并利用XBeanBrokerFactory来创建Broker。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActivityMQ创建broker是直接通过配置IOC注入的,所以了解如何用纯JAVA代码和spring xml 创建broker可以让我们对AcitivtyMQ有一个更深入的了解

原本的配置
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="myBroker" dataDirectory="${activemq.base}/data">
          <transportConnectors>
           <transportConnector name="openwire"
            uri="tcp://localhost:61616" />
          </transportConnectors>
         <plugins>
            <simpleAuthenticationPlugin>
             <users>
               <authenticationUser username="admin"
                                   password="password"
                                   groups="admins,publishers,consumers"/>
               <authenticationUser username="publisher"
                                   password="password"
                                   groups="publishers,consumers"/>
               <authenticationUser username="consumer"
                                   password="password"
                                   groups="consumers"/>
               <authenticationUser username="guest"
                                   password="password"
                                   groups="guests"/>
              </users>
            </simpleAuthenticationPlugin>
          </plugins>
</broker>

用纯JAVA实现

BrokerService broker =
                new BrokerService();
        broker.setBrokerName("myBroker");
        broker.setDataDirectory("data/");
        SimpleAuthenticationPlugin authentication = new SimpleAuthenticationPlugin();
        List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
        users.add(new AuthenticationUser("admin", "password", "admins,publishers,consumers"));
        users.add(new AuthenticationUser("publisher", "password", "publishers,consumers"));
        users.add(new AuthenticationUser("consumer", "password", "consumers"));
        users.add(new AuthenticationUser("guest", "password", "guests"));
        authentication.setUsers(users);
        broker.setPlugins(new BrokerPlugin[] { authentication });
        broker.addConnector("tcp://localhost:61616");
        broker.start();
        System.out.println();
        System.out.println("Press any key to stop the broker");
        System.out.println();
        System.in.read();
可以实现同样的功能,但是运行之后我们可以看到tcp端口61616开启用于监听,但是,和配置实现不同的是,纯java实现并没有开启服务器,于是我们再模仿开启服务器操作。
HTTPDiscoveryAgent httpDiscoveryAgent = new HTTPDiscoveryAgent();
httpDiscoveryAgent.setStartEmbeddRegistry(true);
httpDiscoveryAgent.setRegistryURL("http://localhost:8161");
System.out.println(httpDiscoveryAgent.getRegistryURL());
httpDiscoveryAgent.start();
broker.addService(httpDiscoveryAgent);
只需要在broker.start前面加上这段代码即可,这段具体怎么做?需要进行源码搜索源码分析才能知道为什么这么做,当然有时候需要打断点查看哪一段代码有错误或者缺了什么包。

需要这些包
这里写图片描述
这样jetty服务器能够正常开启了
这里写图片描述


用spring xml实现

在项目的根目录上加上spring配置文件
<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">
<amq:broker
brokerName="localhost" dataDirectory="${activemq.base}/data">
  <amq:transportConnectors>
   <amq:transportConnector name="openwire"
    uri="tcp://localhost:61616" />
  </amq:transportConnectors>
  <amq:plugins>
   <amq:simpleAuthenticationPlugin>
    <amq:users>
     <amq:authenticationUser username="admin"
                             password="password"
                               groups="admins,publishers,consumers"/>
     <amq:authenticationUser username="publisher"
                             password="password"
                             groups="publishers,consumers"/>
     <amq:authenticationUser username="consumer"
                             password="password"
                             groups="consumers"/>
     <amq:authenticationUser username="guest"
                             password="password"
                             groups="guests"/>
    </amq:users>
   </amq:simpleAuthenticationPlugin>
  </amq:plugins>
 </amq:broker>
</beans>
添加一个Factory,负责创建Broker
package com.tgb.activemq;

import java.net.URI;

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.xbean.XBeanBrokerFactory;

public class Factory {
    public static void main(String[] args) throws Exception {
        System.setProperty("activemq.base", System.getProperty("user.dir"));
        String configUri = "xbean:Spring-simgle.xml";
        URI brokerUri = new URI(configUri);
        BrokerService broker = BrokerFactory.createBroker(brokerUri);
        broker.start();
        System.out.println();
        System.out.println("Press any key to stop the broker");
        System.out.println();
        System.in.read();
    }
}
xbean:前缀是告诉Factory使用 XBeanBrokerFactory创建Broker

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值