gpt4 book ai didi

java - 无法将 类型的对象转换为 JMS 消息。支持的消息有效负载为 : String, 字节数组、Map、可序列化对象

转载 作者:行者123 更新时间:2023-11-30 02:41:04 25 4
gpt4 key购买 nike

我正在开发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;
}
}

最佳答案

改用 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());

就是这样。

关于java - 无法将 类型的对象转换为 JMS 消息。支持的消息有效负载为 : String, 字节数组、Map<String,?>、可序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41676959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com