ActiveMQ实战篇之 java和spring xml创建Broker(一)
来源:互联网 发布:苹果变音软件 编辑:程序博客网 时间:2024/06/14 23:03
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:brokerbrokerName="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 0
- ActiveMQ实战篇之 java和spring xml创建Broker(一)
- ActiveMQ之三:启动ActiveMQ的Broker
- Java中间件JMS(二)之ActiveMQ整合spring(一)
- ActiveMQ源码解析(一):聊聊broker
- activemq--MASTER SLAVE+BROKER CLUSTER 实践(一)
- ActiveMQ实战(一)--ActiveMQ简介
- ActiveMQ实战(一)-入门篇
- ActiveMQ实战1:ActiveMQ Java
- ActiveMQ实战篇之ActiveMQ实现request/reply模型(二)
- 消息中间件-activemq实战之整合Spring(四)
- 消息中间件-activemq实战整合Spring之Topic模式(五)
- ActiveMQ实战之 安装
- Java消息中间件之Spring中的ActiveMQ
- activemq在一台服务器上启动多个Broker
- java与xml之DOM创建和解析XML文档
- java与xml之DOM创建和解析XML文档
- Java中间件JMS(一)之ActiveMQ入门
- Java中间件JMS(一)之ActiveMQ入门
- Jquery插件之validator
- 电脑的远程桌面连接
- jcenter,mavenCenter仓库创建、发布等完整流程详解
- zookeeper-3.4.6的安装和简单使用
- 7-两个栈实现一个队列
- ActiveMQ实战篇之 java和spring xml创建Broker(一)
- 测试博客
- javascript之apply()和call()用法浅析
- PHP 正则匹配篇
- Crashing Robots POJ
- hdu1506直方图中的最大长方形(dp)
- find the mincost route HDU
- secureCRT用串口连接tiny4412方法
- box-shadow