ActiveMQ发消息成功,控制台报错。
javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: com.ceair.shopping.handler.transform.User
问题重现:参考 http://blog.csdn.net/adam_wzs/article/details/51250177中代码
package com.wangzs.t3;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* @Title 消息消费者工具类
* @Description 消息消费者是由会话创建的一个对象,它用于接收发送到目的地的消息。
* @author wangzs
* @date 2016-4-26
*/
public class ConsumerTool implements MessageListener {
private String user;
private String password;
private String url;
private String queueName; // queueName是一个服务器的queue
private ConnectionFactory connectionFactory = null; // 连接工厂,客户用来创建连接的对象
private Destination destination = null; // 指定它生产的消息的目标和它消费的消息的来源的对象
private Connection connection = null; // 封装了客户与 JMS 提供者之间的一个虚拟的连接
private Session session = null; // 生产和消费消息的一个单线程上下文
private MessageConsumer consumer = null; // 消息消费者
public ConsumerTool() {
super();
}
public ConsumerTool(String user, String password, String url, String queueName) {
super();
this.user = user;
this.password = password;
this.url = url;
this.queueName = queueName;
}
public void initialize() {
connectionFactory = new ActiveMQConnectionFactory(this.getUser(), this.getPassword(), this.getUrl());
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(this.getQueueName());
consumer = session.createConsumer(destination);
} catch (JMSException e) {
e.printStackTrace();
}
}
// 消费消息
public void consumeMessage() {
System.out.println("Consumer:->Begin listening...");
try {
consumer.setMessageListener(this);
} catch (JMSException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Consumer:->Received: " + msg);
}
} catch (JMSException e) {
e.printStackTrace();
}
}
// 关闭连接
public void close() {
try {
System.out.println("Consumer:->Closing connection");
if (consumer != null) {
consumer.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getQueueName() {
return queueName;
}
public void setQueueName(String queueName) {
this.queueName = queueName;
}
}
package com.wangzs.t3;
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.ActiveMQConnectionFactory;
/**
* @Title 消息生产者工具类
* @Description 消息生产者是由会话创建的一个对象,用于把消息发送到一个目的地
* @author wangzs
* @date 2016-4-26
*/
public class ProducerTool {
private String user;
private String password;
private String url;
private String queueName; // queueName是一个服务器的queue
private ConnectionFactory connectionFactory = null; // 连接工厂,客户用来创建连接的对象
private Destination destination = null; // 指定它生产的消息的目标和它消费的消息的来源的对象
private Connection connection = null; // 封装了客户与 JMS 提供者之间的一个虚拟的连接
private Session session = null; // 生产和消费消息的一个单线程上下文
private MessageProducer producer = null; // 消息生产者
public ProducerTool() {
super();
}
public ProducerTool(String user, String password, String url, String queueName) {
super();
this.user = user;
this.password = password;
this.url = url;
this.queueName = queueName;
}
public void initialize() {
connectionFactory = new ActiveMQConnectionFactory(this.getUser(), this.getPassword(), this.getUrl());
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(this.getQueueName());
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// NON_PERSISTENT:不要求 JMS Producer 持久保存消息。
} catch (JMSException e) {
e.printStackTrace();
}
}
// 发送消息 String message
public void produceMessage(String message) {
try {
TextMessage msg = session.createTextMessage(message);
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
} catch (JMSException e) {
e.printStackTrace();
}
}
<strong><span style="color:#ff0000;"> // 发送消息Serializable message
public void produceMessage(Serializable message) {
try {
ObjectMessage msg = session.createObjectMessage(message);
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
} catch (JMSException e) {
e.printStackTrace();
}
}</span></strong>
// 关闭连接
public void close() {
try {
System.out.println("Producer:->Closing connection");
if (producer != null) {
producer.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getQueueName() {
return queueName;
}
public void setQueueName(String queueName) {
this.queueName = queueName;
}
}
package com.wangzs.t3;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -2194417922311787497L;
private String name;
private String age;
public User() {
super();
}
public User(String name, String age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
package com.wangzs.t3;
import org.apache.activemq.ActiveMQConnection;
import org.junit.Test;
/**
* @Title ActiveMQ工具类测试
* @Description
* @author wangzs
* @date 2016-4-26
*/
public class JunitTest {
@Test
public void test1() throws InterruptedException {
ConsumerTool consumer = new ConsumerTool(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,
ActiveMQConnection.DEFAULT_BROKER_URL, "WangzsQueue");
ProducerTool producer = new ProducerTool(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,
ActiveMQConnection.DEFAULT_BROKER_URL, "WangzsQueue");
System.out.println(ActiveMQConnection.DEFAULT_BROKER_URL + "------------");
// 消费者
consumer.initialize();
consumer.consumeMessage();
<strong><span style="color:#ff0000;"> User user1 = new User("wangzs1", "1");
User user2 = new User("wangzs2", "2");</span></strong>
// 生产者
producer.initialize();
producer.produceMessage(user1);
producer.produceMessage(user2);
}
}
问题分析:
java.lang.ClassNotFoundException: com.ceair.shopping.handler.transform.User
是因为在D:\apache-activemq-5.5.1\lib里面没有user的代码,activemq无法识别
解决办法:
把本地User.java打成user.jar包放入D:\apache-activemq-5.5.1\lib路径下,重启activemq,
再次发消息,就可在activemq控制台看到正确的结果。