在ActiveMQ中使用VM transport connector

在ActiveMQ使用中,可以再VM中内嵌一个broker来作为传输的中间件,这样可以简化开发流程。具体如下:

PUBLISHER:

package com.test.vm;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Publisher {

private static String brokerURL = "tcp://192.168.0.16:6000";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
private transient MessageProducer producer;

private static int count = 10;
private static int total;
private static int id = 1000000;

private String jobs[] = new String[]{"suspend", "delete"};

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 {
Publisher publisher = new Publisher();
while (total < 1000) {
for (int i = 0; i < count; i++) {
publisher.sendMessage();
}
total += count;
System.out.println("Published '" + count + "' of '" + total + "' job messages");
try {
Thread.sleep(1000);
} catch (InterruptedException x) {
}
}
publisher.close();

}

public void sendMessage() throws JMSException {
int idx = 0;
while (true) {
idx = (int)Math.round(jobs.length * Math.random());
if (idx < jobs.length) {
break;
}
}
String job = jobs[idx];
Destination destination = session.createQueue("JOBS." + job);
Message message = session.createObjectMessage(id++);
System.out.println("Sending: id: " + ((ObjectMessage)message).getObject() + " on queue: " + destination);
producer.send(destination, message);
}

}

CONSUMER:


package com.test.vm;

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;

public class Consumer {

private static String brokerURL = "vm:broker:(tcp://192.168.0.16:6000)?brokerName=embeddedbroker&persistent=false";
//private static String brokerURL = "vm://localhost?brokerConfig=xbean:activemq.xml";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;

private String jobs[] = new String[]{"suspend", "delete"};

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, InterruptedException {
Consumer consumer = new Consumer();

for (String job : consumer.jobs) {
Destination destination = consumer.getSession().createQueue("JOBS." + job);
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
messageConsumer.setMessageListener(new Listener(job));
}
}

public Session getSession() {
return session;
}


}

LISTENER:

package com.test.vm;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

public class Listener implements MessageListener {

private String job;

public Listener(String job) {
this.job = job;
}

public void onMessage(Message message) {
try {
//do something here
System.out.println(job + " id:" + ((ObjectMessage)message).getObject());
} catch (Exception e) {
e.printStackTrace();
}
}

}

先启动Consumer,然后VM在192.168.0.16上启动了一个broker,然后publisher就可以对其进行连接。先运行Consumer,再运行Publisher,测试成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值