ActiveMQ初学入门

消息发送类

package com.wq.activeMq;


import java.io.Serializable;


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


import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


/**
 * 消息发送类.
 * 
 * @author wq
 * 
 */
public class MsgSender {


/** tcp地址. */
public static String tcpUrl = "tcp://localhost:61616";


/** ConnectionFactory :连接工厂,JMS 用它创建连接. */
public static ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, tcpUrl);

/**
* 发送文本消息.
* @param session
* @param producer
* @param msgType
* @param msg
*/
private static void sendTextMsg(Session session, MessageProducer producer,
String msgType, String msg) {
try {
// 构造消息.
TextMessage message;
// 获得文本消息.
message = session.createTextMessage(msg);
// 设置消息类型.
message.setJMSType(msgType);
// 发送消息到目的地方.
producer.send(message);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* 发送对象消息.
* @param session
* @param producer
* @param msgType
* @param msg
*/
private static void sendObjMsg(Session session, MessageProducer producer,
String msgType, Serializable msg) {
try {
// 构造消息.
ObjectMessage message;
// 获得文本消息.
message = session.createObjectMessage(msg);
// 设置消息类型.
message.setJMSType(msgType);
// 发送消息到目的地方.
producer.send(message);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* 发送消息.
* 
* @param destinationName
* @param msgType
* @param msg
*/
public static void send(String destinationName, String msgType, Object msg) {
// Connection :JMS 客户端到JMS Provider 的连接.
Connection connection = null;
// Session: 一个发送或接收消息的线程.
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者.
MessageProducer producer;
try {
// 构造从工厂得到连接对象.
connection = connectionFactory.createConnection();
// 启动.
connection.start();
// 获取操作连接.
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// destinationName需要在admin界面创建.
destination = session.createQueue(destinationName);
// 得到消息生成者.
producer = session.createProducer(destination);
// 设置持久化.
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
// 发送消息
if(msg instanceof String){// 文本类型
sendTextMsg(session,producer,msgType,msg.toString());
}else if(msg instanceof Serializable){// 序列化对象类型
sendObjMsg(session,producer,msgType,(Serializable)msg);
}else{// 默认是文本类型
sendTextMsg(session,producer,msgType,msg.toString());
}
// 提交事务.
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}




接收消息处理类

package com.wq.activeMq;


import java.util.HashMap;
import java.util.Map;


import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;


import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


import com.wq.activeMq.receive.ReceiveObject;
import com.wq.activeMq.receive.ReceiveText;


/**
 * 接收消息处理.
 * 
 * @author wq
 * 
 */
public class MsgReceive {


/** tcp地址. */
public static String tcpUrl = "tcp://localhost:61616";


/** ConnectionFactory :连接工厂,JMS 用它创建连接. */
public static ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, tcpUrl);


/** 消息处理类. */
public static Map<String, MsgDeal> msgDealMap = new HashMap<String, MsgDeal>();


static {
msgDealMap.put("TextType", new ReceiveText());
msgDealMap.put("ObjType", new ReceiveObject());
}


public static void Reveive(String destinationName) {
// Connection :JMS 客户端到JMS Provider 的连接.
Connection connection = null;
// Session: 一个发送或接收消息的线程.
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者.
MessageConsumer consumer;


try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue(destinationName);
// 获取消费者
consumer = session.createConsumer(destination);
while (true) {
Message msg = consumer.receive(1000);
if (null != msg) {
String msgType = msg.getJMSType();
MsgDeal msgDeal = msgDealMap.get(msgType);
if(null != msgDeal){
msgDeal.onMessage(msg);
}else{
System.err.println("没有配置msgDealMap:[" + msgType + "]");
}
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection) {
connection.close();
}
} catch (Throwable ignore) {
}
}
}


}



消息处理接口

package com.wq.activeMq;


import javax.jms.Message;


/**
 * 消息处理接口.
 * @author qingwu
 *
 */
public interface MsgDeal {
void onMessage(Message msg);
}



文本消息处理测试接口实现

package com.wq.activeMq.receive;


import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;


import com.wq.activeMq.MsgDeal;


/**
 * 文本消息接收处理测试.
 * 
 * @author wq
 * 
 */
public class ReceiveText implements MsgDeal {


@Override
public void onMessage(Message msg) {
// TODO Auto-generated method stub
try {
TextMessage message = (TextMessage) msg;
if (null != message) {
System.out.println("收到TEXT消息:" + message.getText());
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}




对象消息处理测试接口实现

package com.wq.activeMq.receive;


import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;


import com.wq.activeMq.MsgDeal;
import com.wq.test.TestVO;


/**
 * 对象消息接收测试.
 * 
 * @author wq
 * 
 */
public class ReceiveObject implements MsgDeal {


@Override
public void onMessage(Message msg) {
// TODO Auto-generated method stub
try {
ObjectMessage message = (ObjectMessage) msg;
if (null != message) {
TestVO vo = (TestVO) message.getObject();
System.out.println("收到OBJECT消息:" + vo.getName() + " " + vo.getSex() + " " + vo.getAge());
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}



测试VO对象

package com.wq.test;


import java.io.Serializable;


public class TestVO implements Serializable {


private static final long serialVersionUID = -9118138350208832294L;
String name;
String sex;
int age;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getSex() {
return sex;
}


public void setSex(String sex) {
this.sex = sex;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


}



发送测试

package com.wq.test;


import com.wq.activeMq.MsgSender;


public class SendTest {


public static void sendText(){
String destinationName = "queue1";
for(int i=0;i<5;i++){
String msgType = "TextType";
String msg = "text_" + i;
MsgSender.send(destinationName, msgType, msg);
}
}

public static void sendObj(){
String destinationName = "queue1";
for(int i=0;i<5;i++){
String msgType = "ObjType";
TestVO vo = new TestVO();
vo.setName("name_"+i);
vo.setSex("sex_"+i);
vo.setAge(i);
MsgSender.send(destinationName, msgType, vo);
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
sendText();
sendObj();
}


}




接收测试

package com.wq.test;


import com.wq.activeMq.MsgReceive;


public class ReceiveTest {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MsgReceive.Reveive("queue1");
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值