一、开发环境
activemq官网http://activemq.apache.org/
下载版本http://activemq.apache.org/download-archives.html
最新版本:Apache ActiveMQ 5.15.0 Released posted on Jul 06, 2017
使用开发版本:apache-activemq-5.13.1
开发环境:win7 64bit eclipse Mars.2 Release (4.5.2)
下载版本解压后,直接运行/bin/win64/activemq.bat
启动后在浏览器运行:http://localhost:8161
二、生产端
使用jar包:activemq-all-5.13.1.jar
使用failover的url方式可以实现断线重连的机制
public final class ActiveMQServer {
/**
* icap使用log4j1.2-api-2.2过度到了log4j2
*/
private static Logger log = LogManager.getLogger("com.znv.icap.activemq");
private static String url;
static {
url = "failover://(tcp://10.45.152.227:61616,tcp://10.45.152.228:61616,tcp://10.45.152.229:61616)?randomize=false&initialReconnectDelay=100&timeout=2000";
// url = "failover://("
// + PropertyHelper.getInstance().getValue("activemqUrl",
// "tcp://10.45.157.91:61616,tcp://10.45.157.92:61616,tcp://10.45.157.93:61616")
// + ")?randomize=false&initialReconnectDelay=100&timeout=2000";
}
private boolean transacted = false;
private int ackMode = Session.AUTO_ACKNOWLEDGE;
private ActiveMQConnectionFactory connectionFactory;
private Connection connection;
private Session session;
private Topic topic;
private MessageProducer producer;
private static ActiveMQServer instance = null;
private ActiveMQServer() {
try {
connectionFactory = new ActiveMQConnectionFactory(url);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, ackMode);
topic = session.createTopic("change_data_device_20170330");
((ActiveMQConnection) connection).addTransportListener(new TransportListener() {
@Override
public void transportResumed() {
System.out.println(" 重连");
}
@Override
public void transportInterupted() {
System.out.println("断开");
}
@Override
public void onException(IOException error) {
System.out.println("31");
}
@Override
public void onCommand(Object command) {
System.out.println("41");
}
});
} catch (JMSException e) {
e.printStackTrace();
}
}
public static ActiveMQServer getInstance() {
if(instance == null){
synchronized (ActiveMQClient.class) {
if(instance == null){
instance = new ActiveMQServer();
}
}
}
return instance;
}
public void send(String msg) throws JMSException, InterruptedException {
producer = session.createProducer(topic);
TextMessage message = session.createTextMessage(msg);
producer.send(message);
}
public void close() {
if (producer != null) {
try {
producer.close();
} catch (JMSException e) {
log.error(e);
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
log.error(e);
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
log.error(e);
}
}
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
public static void main(String[] args) throws JMSException {
try {
JSONObject obj = new JSONObject();
obj.put("sessionId", "3D6FCF9B88AFF644848670F4DD9D0ACB");
obj.put("userId", "11000000000");
obj.put("deviceId", "11000010200080");
obj.put("type", "1");
String msg = obj.toJSONString();
ActiveMQServer.getInstance().send(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
5.9的版本不能实现断线重连,需要重写
org.apache.activemq.transport.failover.FailoverTransport.java
activemq-all-5.9.0.jar
三、消费端
public final class ActiveMQClient implements MessageListener {
/**
* icap使用log4j1.2-api-2.2过度到了log4j2
*/
private static Logger log = LogManager.getLogger("com.znv.icap.activemq");
private static String url;
static {
//10.45.146.180-182
url = "failover://(tcp://lv180.dct-znv.com:61616,tcp://lv181.dct-znv.com:61616,tcp://lv182.dct-znv.com:61616)?randomize=false&initialReconnectDelay=100&timeout=2000";
//10.45.152.227 lv03.dct-znv.com
url = "failover://(tcp://10.45.152.227:61616)?randomize=false&initialReconnectDelay=100&timeout=2000";
}
private boolean transacted = false;
private int ackMode = Session.AUTO_ACKNOWLEDGE;
private ActiveMQConnectionFactory connectionFactory;
private Connection connection;
private Session session;
private MessageConsumer consumer;
private static ActiveMQClient instance = null;
private ActiveMQClient() {
try {
connectionFactory = new ActiveMQConnectionFactory(url);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, ackMode);
Topic destination = session
.createTopic("change_report_20170330");
consumer = session.createConsumer(destination);
consumer.setMessageListener(this);
((ActiveMQConnection) connection).addTransportListener(new TransportListener() {
@Override
public void transportResumed() {
log.info("activemq resumed");
}
@Override
public void transportInterupted() {
log.info("activemq interupted");
}
@Override
public void onException(IOException error) {
log.error("activemq onException");
}
@Override
public void onCommand(Object command) {
log.debug("activemq onCommand");
}
});
} catch (JMSException e) {
log.error(e);
}
}
public static ActiveMQClient getInstance() {
if (instance == null) {
synchronized (ActiveMQClient.class) {
if (instance == null) {
instance = new ActiveMQClient();
}
}
}
return instance;
}
@Override
public void onMessage(Message message) {
try {
TextMessage txtmessage = (TextMessage) message;
log.error("=======================receive message from little v:"
+ txtmessage.getText());
// ThreadPoolUtils.exec(new DealChangeDataThread(txtmessage.getText()));
} catch (JMSException e) {
log.error(e);
}
}
public void close() {
if (consumer != null) {
try {
consumer.close();
} catch (JMSException e) {
log.error(e);
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
log.error(e);
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
log.error(e);
}
}
}
public static void main(String[] args) {
ActiveMQClient.getInstance();
}
}