这篇文章适合已经搭建好了activeMQ环境的人,需要封装下activeMQ基本功能的人。封装的不好,仅作参考。
package com.quhuhu.sync.util;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* @author liang.he
*
*/
public class ActiveMqCreater {
//queue的名字,这个对应你在activeMQ上面创建的Queue的名字
public static String subject = "FirstQueue";
// ConnectionFactory :连接工厂,JMS 用它创建连接
public static ConnectionFactory connectionFactory = null;
// Connection :JMS 客户端到JMS Provider 的连接
public static Connection connection = null;
// Session: 一个发送或接收消息的线程
private Session session = null;
// Destination :消息的目的地;消息发送给谁.
public static Destination destination = null;
// MessageProducer:消息发送者
private MessageProducer producer = null;
//MessageConsumer : 消息接收者
private MessageConsumer receiver = null;
static {
connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
try {
connection = connectionFactory.createConnection();
connection.start();
} catch (JMSException e) {
e.printStackTrace();
}
}
/**
*
* @return 初始化消息发送者
* @throws Exception
*/
public MessageProducer initMessageProducer() throws Exception{
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//消息目的地
destination = session.createQueue(subject);
// 得到消息【发送者】
producer = session.createProducer(destination);
// 设置不持久化
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
return producer;
}
/**
*
* @return 初始化消息接收者
* @throws JMSException
*/
public MessageConsumer initMessageConsumer() throws JMSException {
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
//消息目的地
destination = session.createQueue(subject);
return session.createConsumer(destination);
}
/**
* 消息发送方法
* @param msg 消息
* @throws JMSException
*/
public void sendMsg(String msg) throws JMSException {
try {
System.out.println("fasongle ");
initMessageProducer().send(session.createTextMessage(msg));
session.commit();
System.out.println("发送消息:" + msg);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 消息接受方法
* @return msg 消息
* @throws JMSException
*/
public String receiveMsg(MessageConsumer consumer) throws JMSException {
TextMessage message = (TextMessage)consumer.receive();
if (null != message) {
return message.getText();
}
return null;
}
/**
* 消息接受方法
* @return msg 消息
* @throws JMSException
*/
public String receiveMsg(MessageConsumer consumer, long time) throws JMSException {
TextMessage message = (TextMessage)consumer.receive(time);
if (null != message) {
return message.getText();
}
return null;
}
/**
* 释放MessageConsumer资源
* @throws JMSException
*/
public void close(MessageConsumer receiver) throws JMSException {
System.out.println("close JMSconnection!");
if(null != receiver) {
receiver.close();
}
}
/**
* 释放MessageProducer资源
* @throws JMSException
*/
public void close(MessageProducer producer) throws JMSException {
System.out.println("close JMSconnection!");
if(null != producer) {
producer.close();
}
}
/**
* 释放Session资源
* @throws JMSException
*/
public void close(Session session) throws JMSException {
System.out.println("close JMSconnection!");
if(null != session) {
session.close();
}
}
/**
* 释放Connection资源
* @throws JMSException
*/
public void close(Connection connection) throws JMSException {
System.out.println("close JMSconnection!");
if(null != connection) {
connection.close();
}
}
}
封装的ActiveMQ类
下面是测试接受类:
import javax.jms.JMSException; import javax.jms.MessageConsumer; public class TestReceive { public static void main(String[] args) throws JMSException { JMSUtil js = new JMSUtil(); MessageConsumer consumer = js.initMessageConsumer(); while (true) { //设置接收者接收消息的时间,为了便于测试,这里谁定为100s System.out.println("收到消息" + js.receiveMsg(consumer, 50000)); } } }
下面是测试发送类:
import javax.jms.JMSException; public class TestSend { public static void main(String[] args) throws JMSException { JMSUtil js = new JMSUtil(); js.sendMsg("11111111111111ssssssssssssssssssssssassssssssssss"); js.sendMsg("2222222222sssssssssssssssssss2ssssssssss"); js.sendMsg("333333333333sssssssssssssssssssdssssssssss"); js.sendMsg("444444444sssssssssssssssssssssssssssss"); js.sendMsg("55555555555ssssssssss2sssssssssssssssssss"); js.sendMsg("666666666666sssssssssssss3ssssssssssssssss"); } }
运行的时候先运行接收类,在运行发送类;
activemq的环境自行百度,很简单,我这里是使用的activeMQ的QUEUE消息队列,activeMQ还提供topic,感兴趣的可以自己研究下他们的区别与用法。