本文为network connectors的static connector学习笔记。
broker网络能够创建多个相互连接的ActiveMq实例组成的簇,以应对更加复杂的消息场景。Network connectors提供了broker之间的通信。
默认情况下,network connector是单向通道,它只会把收到的消息投递给与之建立连接的另一个broker。这通常称为forwarding bridge。ActiveMQ也支持双向通道,即duplex connector。下图是一个包含了这两者的复杂网络。
Network connector的XML配置如下:
用来创建网络中多个broker的静态配置。协议使用组合URI,即URI中包含其他URI。格式如下:
static:(uri1,uri2,uri3,...) ?key=value
XML中配置示例:
/**
* XXX.com Inc.
* Copyright (c) 2004-2015 All Rights Reserved.
*/
package com.test.SpringTest.activemqinaction.ch4;
import java.util.Hashtable;
import java.util.Map;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMapMessage;
/**
* 消息产生者
*
* @author jiangnan
* @version $Id: Publisher.java, v 0.1 2015年7月4日 下午4:48:48 jiangnan Exp $
*/
public class Publisher {
protected int MAX_DELTA_PERCENT = 1;
protected Map LAST_PRICES = new Hashtable();
protected static int count = 10;
protected static int total;
protected static String brokerURL = "tcp://localhost:61616";
protected static transient ConnectionFactory factory;
protected transient Connection connection;
protected transient Session session;
protected transient MessageProducer producer;
public Publisher() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
String[] topics = { "topic1", "topic2" };
Publisher publisher = new Publisher();
while (total < 1000) {
for (int i = 0; i < count; i++) {
publisher.sendMessage(topics);
}
total += count;
System.out.println("Published '" + count + "' of '" + total + "' price messages");
try {
Thread.sleep(1000);
} catch (InterruptedException x) {
}
}
publisher.close();
}
protected void sendMessage(String[] stocks) throws JMSException {
int idx = 0;
while (true) {
idx = (int) Math.round(stocks.length * Math.random());
if (idx < stocks.length) {
break;
}
}
String stock = stocks[idx];
Destination destination = session.createTopic("STOCKS." + stock);
Message message = createStockMessage(stock, session);
System.out.println("Sending: " + ((ActiveMQMapMessage) message).getContentMap()
+ " on destination: " + destination);
producer.send(destination, message);
}
protected Message createStockMessage(String stock, Session session) throws JMSException {
Double value = LAST_PRICES.get(stock);
if (value == null) {
value = new Double(Math.random() * 100);
}
// lets mutate the value by some percentage
double oldPrice = value.doubleValue();
value = new Double(mutatePrice(oldPrice));
LAST_PRICES.put(stock, value);
double price = value.doubleValue();
double offer = price * 1.001;
boolean up = (price > oldPrice);
MapMessage message = session.createMapMessage();
message.setStringProperty("stock", stock);//设置消息的属性
message.setString("stock", stock);
message.setDouble("price", price);
message.setDouble("offer", offer);
message.setBoolean("up", up);
return message;
}
protected double mutatePrice(double price) {
double percentChange = (2 * Math.random() * MAX_DELTA_PERCENT) - MAX_DELTA_PERCENT;
return price * (100 + percentChange) / 100;
}
}
/**
* XXX.com Inc.
* Copyright (c) 2004-2015 All Rights Reserved.
*/
package com.test.SpringTest.activemqinaction.ch4;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者
*
* @author jiangnan
* @version $Id: Consumer.java, v 0.1 2015年7月4日 下午4:37:48 jiangnan Exp $
*/
public class Consumer {
private static String brokerURL = "tcp://localhost:61617";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
public Consumer() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
String[] topics = { "topic1", "topic2" };
Consumer consumer = new Consumer();
for (String stock : topics) {
Destination destination = consumer.getSession().createTopic("STOCKS." + stock);
//只接收部分消息的选择器
String selector = "stock = 'topic1'";
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination,
selector);
messageConsumer.setMessageListener(new Listener());
}
}
public Session getSession() {
return session;
}
}
/**
* XXX.com Inc.
* Copyright (c) 2004-2015 All Rights Reserved.
*/
package com.test.SpringTest.activemqinaction.ch4;
import java.text.DecimalFormat;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
* 消息监听器
*
* @author jiangnan
* @version $Id: Listener.java, v 0.1 2015年7月4日 下午4:40:25 jiangnan Exp $
*/
public class Listener implements MessageListener {
public void onMessage(Message message) {
try {
MapMessage map = (MapMessage) message;
String stock = map.getString("stock");
double price = map.getDouble("price");
double offer = map.getDouble("offer");
boolean up = map.getBoolean("up");
DecimalFormat df = new DecimalFormat("#,###,###,##0.00");
System.out.println(stock + "\t" + df.format(price) + "\t" + df.format(offer) + "\t"
+ (up ? "up" : "down"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
再启动brokerA:
D:\apache-activemq-5.8.0\bin>activemq xbean:file:D:/code/test/SpringTest/src/main/resource/META-INF/spring/activemqinaction/ch4/brokerA.xml
可以看到brokerA和brokerB建立连接成功。
然后启动消费者和生产者。在控制台可以观察到消息发送和接收的日志。
在之前的例子中,客户端仅仅连接到一个特定的broker。如果连接失败或中断,怎么办?有两个选择:客户端会消亡,或者是重新连接到这个broker或者其他broker然后恢复工作。failover可以实现自动重连。有两种方式可以为客户端提供可以连接的broker,一是提供一个静态列表,二是使用动态发现机制。
静态列表配置格式如下:《ActiveMQ in Action》
4.0.0
com.test
SpringTest
0.0.1-SNAPSHOT
jar
SpringTest
http://maven.apache.org
UTF-8
3.1.2.RELEASE
junit
junit
4.11
test
com.sun.jdmk
jmxtools
1.2.1
org.apache.activemq
activemq-all
5.6.0
org.apache.activemq
activemq-core
5.5.0
org.apache.xbean
xbean-spring
3.7
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-expression
${org.springframework.version}
org.springframework
spring-beans
${org.springframework.version}
org.springframework
spring-aop
${org.springframework.version}
org.springframework
spring-context
${org.springframework.version}
org.springframework
spring-context-support
${org.springframework.version}
org.springframework
spring-tx
${org.springframework.version}
org.springframework
spring-jdbc
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-oxm
${org.springframework.version}
org.springframework
spring-web
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.springframework
spring-webmvc-portlet
${org.springframework.version}
org.springframework
spring-test
${org.springframework.version}
test
org.springframework
spring-jms
3.1.1.RELEASE