我正在开发 Spring + ActiveMQ + JMS 示例。在这个例子中,我面临以下错误:我尝试了很多选项,但根本没有工作。
我希望实施以下内容:
1)队列应该继续阅读消息(使用转换器或监听器)
2)根据InstructionMessage类型,我必须决定发送或不发送的位置。
上传的代码为:https://github.com/test512/spring-mvc-jms-tutorials
Sending person InstructionMessage [instructionType=10, productCode=10, quantity=10, uOM=10, timeStamp=10]
Exception in thread "main" org.springframework.jms.support.converter.MessageConversionException: Cannot convert object of type [com.jms.testing.spring.InstructionMessage] to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object.
at org.springframework.jms.support.converter.SimpleMessageConverter.toMessage(SimpleMessageConverter.java:78)
at org.springframework.jms.core.JmsTemplate$5.createMessage(JmsTemplate.java:651)
at org.springframework.jms.core.JmsTemplate.doSend(JmsTemplate.java:593)
at org.springframework.jms.core.JmsTemplate$3.doInJms(JmsTemplate.java:562)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:484)
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:559)
at org.springframework.jms.core.JmsTemplate.convertAndSend(JmsTemplate.java:648)
at org.springframework.jms.core.JmsTemplate.convertAndSend(JmsTemplate.java:639)
at com.jms.testing.spring.SpringJmsPersonProducer.sendMessage(SpringJmsPersonProducer.java:18)
at com.jms.testing.spring.SpringJmsMessageConverterExample.main(SpringJmsMessageConverterExample.java:16)
appContextWithMessageConverter.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jms.properties" />
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${jms.brokerURL}" />
</bean>
<bean id="pooledJmsConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="maxConnections" value="50" />
</bean>
<jms:listener-container container-type="default" connection-factory="pooledJmsConnectionFactory" acknowledge="auto" >
<!-- <jms:listener destination="messageDestination" ref="messageDestination" /> -->
<jms:listener destination="messageDestination" ref="myListener" />
</jms:listener-container>
<bean id="instructionMessageConverter" class="com.jms.testing.spring.InstructionMessageConverter" />
<bean id="myListener" class="com.jms.testing.spring.MyListener" />
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="pooledJmsConnectionFactory" />
<property name="defaultDestination" ref="messageDestination" />
<property name="receiveTimeout" value="${jms.receiveTimeout}" />
<!-- <property name="messageConverter" ref="instructionMessageConverter" /> -->
</bean>
<bean id="springJmsPersonProducer" class="com.jms.testing.spring.SpringJmsPersonProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<bean id="springJmsPersonConsumer" class="com.jms.testing.spring.SpringJmsPersonConsumer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<bean id="messageDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="messageQueue1" />
</bean>
</beans>
MyListener.java
public class MyListener implements MessageListener {
@Override
public void onMessage(Message message) {
MapMessage mapMessage = (MapMessage) message;
try {
int instructionType = Integer.parseInt(mapMessage.getString("instructionType"));
int productCode = Integer.parseInt(mapMessage.getString("productCode"));
int quantity = Integer.parseInt(mapMessage.getString("quantity"));
int timeStamp = Integer.parseInt(mapMessage.getString("timeStamp"));
int uOM = Integer.parseInt(mapMessage.getString("uOM"));
InstructionMessage instructionMessage = new InstructionMessage(instructionType, productCode, quantity, uOM,
timeStamp);
System.out.println(instructionMessage.toString());
} catch (NumberFormatException | JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
InstructionMessage.java
public class InstructionMessage implements Serializable{
private static final long serialVersionUID = 1L;
private int instructionType;
private int productCode;
private int quantity;
private int uOM;
private int timeStamp;
public InstructionMessage(int instructionType, int productCode, int quantity, int uOM, int timeStamp) {
super();
this.instructionType = instructionType;
this.productCode = productCode;
this.quantity = quantity;
this.uOM = uOM;
this.timeStamp = timeStamp;
}
public int getInstructionType() {
return instructionType;
}
public void setInstructionType(int instructionType) {
this.instructionType = instructionType;
}
public int getProductCode() {
return productCode;
}
public void setProductCode(int productCode) {
this.productCode = productCode;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getuOM() {
return uOM;
}
public void setuOM(int uOM) {
this.uOM = uOM;
}
public int getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(int timeStamp) {
this.timeStamp = timeStamp;
}
@Override
public String toString() {
return "InstructionMessage [instructionType=" + instructionType + ", productCode=" + productCode + ", quantity="
+ quantity + ", uOM=" + uOM + ", timeStamp=" + timeStamp + "]";
}
}
SpringJmsMessageConverterExample.java
public class SpringJmsMessageConverterExample {
public static void main(String[] args) throws URISyntaxException, Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"appContextWithMessageConverter.xml");
try {
SpringJmsPersonProducer springJmsProducer = (SpringJmsPersonProducer) context.getBean("springJmsPersonProducer");
InstructionMessage m1 = new InstructionMessage(10,10,10,10,10);
System.out.println("Sending person " + m1);
springJmsProducer.sendMessage(m1);
InstructionMessage m2 = new InstructionMessage(5,5,5,5,5);
System.out.println("Sending person " + m2);
springJmsProducer.sendMessage(m2);
InstructionMessage m3 = new InstructionMessage(0,0,0,0,0);
System.out.println("Sending person " + m3);
springJmsProducer.sendMessage(m3);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
SpringJmsPersonConsumer springJmsConsumer = (SpringJmsPersonConsumer) context.getBean("springJmsPersonConsumer");
System.out.println("Consumer receives " + springJmsConsumer.receiveMessage());
} finally {
context.close();
}
}
}
SpringJmsPersonProducer.java
public class SpringJmsPersonProducer {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(final InstructionMessage instructionMessage) {
getJmsTemplate().convertAndSend(instructionMessage);
}
}
SpringJmsPersonConsumer.java
public class SpringJmsPersonConsumer {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public InstructionMessage receiveMessage() throws JMSException {
InstructionMessage instructionMessage = (InstructionMessage) getJmsTemplate().receiveAndConvert();
return instructionMessage;
}
}
答案 0 :(得分:2)
只需添加生成的序列版本ID,而不是默认的1L!
public class InstructionMessage implements Serializable{
private static final long serialVersionUID = -295422703255886286L;
更新:
<强> 1)强> 为了让您的听众正常工作,您需要更新 destination =“messageQueue1 ,因为 messageDestination 容器会创建此队列,并且您的邮件会发送到 messageQueue1 :
<jms:listener-container container-type="default"
connection-factory="pooledJmsConnectionFactory" acknowledge="auto">
<jms:listener destination="messageQueue1" ref="myListener" />
</jms:listener-container>
并更新 MyListener
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
public class MyListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
ObjectMessage mapMessage = (ObjectMessage) message;
InstructionMessage instructionMessage = (InstructionMessage) mapMessage.getObject();
System.out.println(instructionMessage.toString());
} catch (NumberFormatException | JMSException e) {
e.printStackTrace();
}
}
}
2)有条件发送
import org.springframework.jms.core.JmsTemplate;
public class SpringJmsPersonProducer {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(final InstructionMessage instructionMessage) {
if (canSend(instructionMessage)) {
getJmsTemplate().convertAndSend(instructionMessage);
} else {
throw new IllegalArgumentException("message");
}
}
private boolean canSend(InstructionMessage instructionMessage) {
return instructionMessage.getQuantity() > 0;
}
}
答案 1 :(得分:0)
JMS将数据移动到不同的JVM。不能用普通物体做。必须序列化,因此需要实现Serializable接口 假设可以这样做,请问主建筑师
import java.io.Serializable;
...
public class InstructionMessage implements Serializable {
private static final long serialVersionUID = 7526472295622776147L;
....
}
背景:网络不了解Java对象,只能传输字节。 是Java开发的基础,任何书都可以提供帮助(Eckel用Java思考)
编辑:部分答案(尝试回答)您已更改(使用Serializable
)代码。
现在ClassCastException
例外说:发送和接收部分互不兼容。
框架或服务器转换InstructionMessage - &gt; HashMap,我想早期阶段。
我猜也是,但在这里我几乎可以肯定,HashMap内容在功能上是相同的。
最简单但不优雅的方法是离开InstructionMessage并双方使用HashMap(更改发件人)或调试问题。
如果我记得,你删除了Converter类java,但保留在XML中。我不是一个大的春天粉丝(个人讨厌&#34; xml编程&#34;),我想用XML进行一些修正可以帮助
答案 2 :(得分:0)
请改用Jackson转换,在需要触发多个事件对象的事件驱动系统中方便使用
@Bean
public JmsTemplate jmsTemplate() {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory());
template.setMessageConverter(jacksonJmsMessageConverter());
template.setPubSubDomain(true);
return template;
}
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
然后设置
containerFactory.setMessageConverter(jacksonJmsMessageConverter());
就是这样。