我想将问题扩展到问题的另一个查询:无法将类型的对象转换为 JMS 消息。支持的消息有效负载包括:字符串、字节数组、映射<字符串、?>、可序列化对象
我收到以下错误
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.jms.testing.spring.InstructionMessage
at com.jms.testing.spring.SpringJmsInstructionMessageConsumer.receiveMessage(SpringJmsInstructionMessageConsumer.java:20)
at com.jms.testing.spring.InstructionMessageTest.testActriveMQMessages(InstructionMessageTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
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="springJmsInstProducer" class="com.jms.testing.spring.SpringJmsInstructionMessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<bean id="springJmsInstConsumer" class="com.jms.testing.spring.SpringJmsInstructionMessageConsumer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<bean id="messageDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="messageQueue1" />
</bean>
</beans>
SpringJmsInstruction MessageConsumer.java
public class SpringJmsInstructionMessageConsumer {
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(); //line-20, its breaking here
return instructionMessage;
}
}
在第 20 行,代码正在破坏上述代码
@Test
public void test() throws JMSException{
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("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Consumer receives " + springJmsConsumer.receiveMessage());
}
如
前所述,由于使用默认的1L,只需添加一个生成的序列号版本ID!
public class InstructionMessage implements Serializable{
private static final long serialVersionUID = -295422703255886286L;