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();
}
}