gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:40:51 26 4
gpt4 key购买 nike

我正在尝试向 ActiveMQ 队列发送消息,但我收到了 java.lang.ClassCastException: java.util.UUID cannot be cast to java.lang.String .我一直在尝试查找如何解决这个问题,但网上没有真正的答案。我用这样的私有(private)方法发送它:

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 注入(inject) 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>同样在消息中,不想转换 UUIDString表示,因为我正在用 Hibernate 做一些事情,要求它是 UUID稍后在路上。

最佳答案

可在此处找到更多信息:http://camel.apache.org/jms或这里 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

关于java - 使用 Camel 发送 ActiveMQ 消息时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39254824/

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