使用 Camel 发送 ActiveMQ 消息时出现 ClassCastException

ClassCastException when sending ActiveMQ message with Camel

我正在尝试向 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>,这是我怀疑潜在问题的地方。 部分堆栈跟踪:

java.lang.ClassCastException: java.util.UUID cannot be cast to java.lang.String at org.apache.activemq.util.MarshallingSupport.marshalPrimitiveMap(MarshallingSupport.java:61) at org.apache.activemq.util.MarshallingSupport.marshalPrimitive(MarshallingSupport.java:151) at org.apache.activemq.util.MarshallingSupport.marshalPrimitiveMap(MarshallingSupport.java:64) at org.apache.activemq.command.ActiveMQMapMessage.storeContent(ActiveMQMapMessage.java:150) at org.apache.activemq.command.ActiveMQMapMessage.copy(ActiveMQMapMessage.java:121) at org.apache.activemq.command.ActiveMQMapMessage.copy(ActiveMQMapMessage.java:116) at org.apache.activemq.ActiveMQSession.send(ActiveMQSession.java:1773) at org.apache.activemq.ActiveMQMessageProducer.send(ActiveMQMessageProducer.java:289) at org.apache.activemq.ActiveMQMessageProducer.send(ActiveMQMessageProducer.java:224) at org.apache.activemq.pool.PooledProducer.send(PooledProducer.java:79) at org.apache.activemq.pool.PooledProducer.send(PooledProducer.java:62) at org.springframework.jms.core.JmsTemplate.doSend(JmsTemplate.java:635) at 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 以后的道路。

可在此处找到更多信息: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.

我认为,您必须将 UUID 转换为 String