1、下载activeMQ安装包http://activemq.apache.org/download-archives.html
2、修改conf文件下的activemq.xml文件,添加登录验证和networkConnector,如下:
......
用户名和密码在
conf/credentials.properties中
,这个broker-2的tansportConnectors
它的端口号是61626,而broker-1的端口号是61616,由于是在一台机器上模拟两个broker,
.......
本人将activeMQ安装包复制一份,将端口号修改一下,如果在管理界面http://localhost:8161/admin/ 同时查看两broker的情况,需要在路径conf/jetty.xml中将一个端口号修改为8162(只要不是8161就行)。配置networkConnector的目的使这两台broker负载均衡,当broker-1上的消息队列有100个消息时,但是没有消费者来消费或者消费者消费的能力低,导致大量消息积压在队列中,如果配置networkConnector,它里面有其他broker的ip和端口号,本例中是broker-2,broker-2上有一个消费,但是broker-2的myQueues队列中没有消息,此时broker-2上的消费者就可以消费,broke-1中的消息,即使broker-1的服务器宕机了,也能消费。
下面写一个demo来演示
maven工程的pom依赖
4.2.5.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jms
4.2.3.RELEASE
org.apache.activemq
activemq-all
5.13.2
org.apache.commons
commons-pool2
2.4.2
junit
junit
4.12
public class Producer {
public static final int AMOUNT = 100;
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
// 连接工场,JMS用它创建连接
PooledConnectionFactory factory = (PooledConnectionFactory) context.getBean("poolConnectionFactory");
ActiveMQQueue destination = (ActiveMQQueue)context.getBean("queue");
try {
// 创建连接对象,启动连接
Connection connection = factory.createConnection();
connection.start();
// 自动确认模式,不需要反馈就将消息从队列中删除
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
System.out.println("hint:准备发送消息....");
long start = System.currentTimeMillis();
for(int i=1;i<=AMOUNT;i++){
TextMessage message = session.createTextMessage("第"+i+"条消息");
producer.send(message);
}
long time = System.currentTimeMillis() - start;
int speed = Double.valueOf(AMOUNT / (time/1000)).intValue();
System.out.print("发送 " + AMOUNT + " 条消息,耗时:" + time + "毫秒,平均" + speed + "条/秒");
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
public class Consumer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
// 连接工场,JMS用它创建连接
PooledConnectionFactory factory = (PooledConnectionFactory) context.getBean("poolConnectionFactory");
ActiveMQQueue destination = (ActiveMQQueue)context.getBean("queue");
try {
Connection connection = factory.createConnection();
connection.start();
// 选用客户端确认模式,即消息处理完成后再将消息从消息队列中删除
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new ActiveMQListener());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static public class ActiveMQListener implements MessageListener{
public void onMessage(Message message) {
try {
if(message instanceof TextMessage){
System.out.println("消费了 "+((TextMessage)message).getText());
// 收到消息后,进行确认
message.acknowledge();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
运行\bin\win64下ativemq.bat文件,同时将broker-1和broker-2启动,圈红框第一处表示broker-2到broker-1的单向network connector连接已经建立,第二处表示网址链接可以访问了,broke-1到broker-2的启动信息和这个差不多,就省略了。
先运行生产者程序,在控制台的信息
点击连接http://localhost:8161/admin/可以查看到broker-1中myQueues队列中已经有100条信息
运行消费者程序
进入broker-2的链接http://localhost:8162/admin/查看到它的myQueues队列中信息(两个broker之间建立networkConnector后相同名字的队列消息可以互相消费)
控制台信息如下
参考文章
http://www.cnblogs.com/yjmyzz/p/activemq-sample.html
http://www.cnblogs.com/yjmyzz/p/activemq-ha-using-networks-of-brokers.html
http://greemranqq.iteye.com/blog/2194051