介绍activemq点对点消息实例。
生产者代码:
package com.xxx.activemq.send;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* Hello world!
*
*/
public class Producer
{
private static String userName = "admin";
private static String password = "admin";
private static String brokerUrl = "tcp://localhost:61616";
public static void main( String[] args )
{
ConnectionFactory factory;
Connection connection = null;
Session session;
Destination dest;
MessageProducer producer;
factory = new ActiveMQConnectionFactory(userName, password, brokerUrl);
try {
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
dest = session.createQueue("brokerQueue");
producer = session.createProducer(dest);
for(int i=0;i<5;i++){
producer.send(session.createTextMessage("i say ->" + i ));
}
producer.close();
session.close();
} catch (JMSException e) {
e.printStackTrace();
}finally{
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
代码很直观,先由activemq实现的ConnectionFactory直接构建一个连接工厂,这里需要传入连接broker的用户名,密码,url,可以直接使用系统默认的值。
接着用工厂创建连接。然后利用连接创建会话,会话创建消息目的(Queue),生产者。利用生产发送会话创建的消息。
运行生产者代码,然后查看队列,产生了五个消息,等待消费者来消费:
消费者代码:
package com.xxx.activemq.receive;
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 javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
private static String userName = ActiveMQConnection.DEFAULT_USER;//"admin";
private static String password = ActiveMQConnection.DEFAULT_PASSWORD;//"admin";
private static String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;//"tcp://localhost:61616";
public static void main(String[] args) {
ConnectionFactory factory;
Connection connection = null;
Session session;
Destination dest;
MessageConsumer consumer;
factory = new ActiveMQConnectionFactory(userName, password, brokerUrl);
try {
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
dest = session.createQueue("brokerQueue");
consumer = session.createConsumer(dest);
while(true){
TextMessage message = (TextMessage)consumer.receive(100);
if(message != null){
System.out.println("received: "+message.getText());
}else {
break;
}
}
session.close();
} catch (JMSException e) {
e.printStackTrace();
}finally{
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
消费者代码和生产者基本类似,到了消息接收这里稍微区别一下,这里用的是通过循环去broker上读取,直到读取完成,将收到的五个消息都打印出来。Eclipse控制台打印的消息如下:
消费了生产者产生的消息之后,再次查看队列,发现五个消息已经被消费了(dequeue)。
补充:实例中提到的brokerQueue,无须登录http://localhost:8161/admin,然后创建一个brokerQueue,这个在生产者运行时没有会自动创建。