当前配置是允许接收所有的对象序列化,更多设置详情参考官网:
http://activemq.apache.org/objectmessage.html
1、保证当前MQ能正常接收发送消息
2、定义一个实体类Member继承Serializable
package com.activemq.test;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
public class Member implements Serializable{
private String name;
private double score;
private int age;
private Date brithday;
public Member(){}
public Member(String name, double score, int age, Date brithday) {
super();
this.name = name;
this.score = score;
this.age = age;
this.brithday = brithday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
@Override
public String toString() {
return "Member [name=" + name + ", score=" + score + ", age=" + age + ", brithday=" + brithday + "]";
}
}
4、发送端测试代码
package com.activemq.test;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class TestSendMq {
private static final int SEND_NUMBER = 5;
/**
* @param args
*/
public static void main(String[] args) {
// ConnectionFactory:连接工厂,JMS用它创建连接
ConnectionFactory connectionFactory;
// Connection:JMS客户端到JMS Provider的连接
Connection connection = null;
// Session:一个发送或接收消息的线程
Session session;
// Destination:消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// TextMessage message;
//构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"failover:(tcp://0.0.0.0:61618)");
try {
//构造从工厂得到连接对象
connection =connectionFactory.createConnection();
//启动
connection.start();
//获取操作连接
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//获取session
destination = session.createQueue("FirstQueue");
//得到消息生成者【发送者】
producer =session.createProducer(destination);
//设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//构造消息,此处写死,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
public static void sendMessage(Session session,MessageProducer producer)
throws Exception {
for (int i = 1; i <=SEND_NUMBER; i++) {
Member a = new Member("测试账号"+i,11.1,15+i,new Date());
// HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
// map.put(i, i);
ObjectMessage message = session
.createObjectMessage(a);
//发送消息到目的地方
System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
}
}
}
3、接收端测试代码
package com.activemq.test;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class TestNewCounsumerMq {
public static void main(String[] args) {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://0.0.0.0:61618");
// factory.setTrustedPackages(new ArrayList<String>(Arrays.asList("com.activemq.test")));
factory.setTrustAllPackages(true); //允许接收所有的数据~~~
org.apache.camel.component.jms.JmsConfiguration conf = new org.apache.camel.component.jms.JmsConfiguration();
conf.setConnectionFactory(factory);
org.apache.activemq.camel.component.ActiveMQComponent comp= new org.apache.activemq.camel.component.ActiveMQComponent();
comp.setConfiguration(conf);
Connection connection = null ;
try {
connection = factory.createConnection();
//启动
connection.start();
//获取操作连接
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
//获取session
Destination destination = session.createQueue("FirstQueue");
MessageConsumer consumer =session.createConsumer(destination);
System.out.println(consumer);
while (true) {
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
ObjectMessage message =(ObjectMessage) consumer.receive(100000);
if (null != message) {
Member a = (Member)message.getObject();
System.out.println("收到消息" +a.getName());
} else {
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}finally{
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
备注:以上纯属测试,更多安全详情配置请参考官网
http://activemq.apache.org/objectmessage.html