我正在尝试向ActiveMQ队列发送消息,但我收到了java.lang.ClassCastException: java.util.UUID cannot be cast to java.lang.String
。我一直在努力寻找如何解决这个问题,但没有真正的答案在线。我用私人方法发送它:
private void sendToHosts( Map<Object, Object> msg, String[] hosts )
{
Arrays.stream( hosts )
.forEach( host -> {
ProducerTemplate template = camelContext.createProducerTemplate();
template.setDefaultEndpointUri("direct:com.example.updatehost." + host);
try {
template.sendBody( msg ); //throwing ClassCastException
}
catch( Exception e ) {
e.printStackTrace();
}
});
使用spring注入camelContext,我知道它正在运行。
我知道ActiveMQ设置正确,因为我进入此方法的唯一方法是,如果我收到来自另一个队列的消息,并且我尝试在执行上述某些方法后转发此消息,但我没有修改消息无论如何。
邮件中有一个Map<UUID,Object>
,这是我怀疑潜在问题的地方。
部分Stacktrace:
java.lang.ClassCastException:java.util.UUID无法强制转换为 java.lang.String at org.apache.activemq.util.MarshallingSupport.marshalPrimitiveMap(MarshallingSupport.java:61) 在 org.apache.activemq.util.MarshallingSupport.marshalPrimitive(MarshallingSupport.java:151) 在 org.apache.activemq.util.MarshallingSupport.marshalPrimitiveMap(MarshallingSupport.java:64) 在 org.apache.activemq.command.ActiveMQMapMessage.storeContent(ActiveMQMapMessage.java:150) 在 org.apache.activemq.command.ActiveMQMapMessage.copy(ActiveMQMapMessage.java:121) 在 org.apache.activemq.command.ActiveMQMapMessage.copy(ActiveMQMapMessage.java:116) 在 org.apache.activemq.ActiveMQSession.send(ActiveMQSession.java:1773) 在 org.apache.activemq.ActiveMQMessageProducer.send(ActiveMQMessageProducer.java:289) 在 org.apache.activemq.ActiveMQMessageProducer.send(ActiveMQMessageProducer.java:224) 在 org.apache.activemq.pool.PooledProducer.send(PooledProducer.java:79) 在 org.apache.activemq.pool.PooledProducer.send(PooledProducer.java:62) 在 org.springframework.jms.core.JmsTemplate.doSend(JmsTemplate.java:635) 在 org.apache.camel.component.jms.JmsConfiguration $ CamelJmsTemplate.doSend(JmsConfiguration.java:343)
有没有人遇到过这种情况,然后找到了绕过ClassCastException
的方法?我觉得很奇怪,当编组时,他们希望它是Map<String,Object>
并且不支持Map<Object,Object>
。
我100%也需要消息中的Map<UUID,Object>
,不想将UUID
转换为String
表示,因为我正在做一些需要Hibernate的东西它会在以后成为UUID
。
答案 0 :(得分:3)
More information can be found here: http://camel.apache.org/jms or here http://docs.oracle.com/javaee/7/api/javax/jms/Message.html
Message Bodies
The JMS API defines five types of message body:
- Stream - A StreamMessage object's message body contains a stream of primitive values in the Java programming language ("Java
primitives"). It is filled and read sequentially.Map - A MapMessage object's message body contains a set of name-value pairs, where names are
String
objects, and values are Java primitives. The entries can be accessed sequentially or randomly by name. The order of the entries is undefined.Text - A TextMessage object's message body contains a java.lang.String object. This message type can be used to transport
plain-text messages, and XML messages.Object - An ObjectMessage object's message body contains a Serializable Java object.
Bytes - A BytesMessage object's message body contains a stream of uninterpreted bytes. This message type is for literally encoding a
body to match an existing message format. In many cases, it is
possible to use one of the other body types, which are easier to use. Although the JMS API allows the use of message properties with byte
messages, they are typically not used, since the inclusion of
properties may affect the format.
I think, you have to convert the UUID
to a String
.