我想用Camel将消息发送到JMS队列。正文是字节数组,我希望Camel将其转换为javax.jms.BytesMessage
并发送它没有问题,但我收到以下警告。
Warning
[org.apache.camel.component.jms.JmsBinding] (Camel (camel) thread #0 -
JmsConsumer[devrechTransactionsQueue@z4smq_4001)
Cannot determine specific JmsMessage type to use from body class.
Will use generic JmsMessage. Body class: org.apache.camel.impl.DefaultMessage.
If you want to send a POJO then your class might need to implement java.io.Serializable,
or you can force a specific type by setting the jmsMessageType option on the JMS endpoint.
如果我尝试手动设置类型(exchange.getIn().setHeader("CamelJmsMessageType", JmsMessageType.Bytes);
),我会得到NullPointerException。
Exception
[org.apache.camel.processor.DefaultErrorHandler] (Camel (camel) thread #0 - JmsConsumer[devrechTransactionsQueue@z4smq_4001) Failed delivery for exchangeId: ID-madansportapp02-5
1560-1313509081079-0-1. Exhausted after delivery attempt: 1 caught: java.lang.NullPointerException
java.lang.NullPointerException
at com.swiftmq.tools.util.DataByteArrayOutputStream.write(Unknown Source)
at com.swiftmq.jms.BytesMessageImpl.writeBytes(Unknown Source)
at org.apache.camel.component.jms.JmsBinding.createJmsMessageForType(JmsBinding.java:506)
at org.apache.camel.component.jms.JmsBinding.createJmsMessage(JmsBinding.java:443)
at org.apache.camel.component.jms.JmsBinding.makeJmsMessage(JmsBinding.java:267)
at org.apache.camel.component.jms.JmsProducer$2.createMessage(JmsProducer.java:225)
at org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.doSendToDestination(JmsConfiguration.java:198)
at org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.access$100(JmsConfiguration.java:141)
at org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate$3.doInJms(JmsConfiguration.java:175)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:466)
at org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.send(JmsConfiguration.java:172)
at org.apache.camel.component.jms.JmsProducer.doSend(JmsProducer.java:347)
at org.apache.camel.component.jms.JmsProducer.processInOnly(JmsProducer.java:303)
at org.apache.camel.component.jms.JmsProducer.process(JmsProducer.java:101)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)
at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:114)
at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:286)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:109)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:69)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)
at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:99)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)
at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:318)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:209)
at org.apache.camel.processor.DefaultChannel.process(DefaultChannel.java:305)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:116)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:79)
at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:102)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)
at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:69)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:104)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85)
at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:91)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:560)
...
Code
public class MessageTranslator implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
RechargeType transaction = (RechargeType) exchange.getIn().getBody();
exchange.getIn().setBody(createMessage(transaction));
}
private Message createMessage(RechargeType transaction) throws DataException, IOException {
Message message = new DefaultMessage(); // camel message
Request request = new Request(transaction); // our request
final byte[] serializedPayload = RequestSerializationHelper.getSerializedPayload(request); // serialized request
message.setBody(serializedPayload);
return message;
}
}
答案 0 :(得分:0)
你应该不将主体设置为Camel消息,而只是将您想要用作有效负载的普通字节[]。
因此createMessage方法应该返回byte []。
答案 1 :(得分:0)
或者如果你想发送可序列化的对象:
import org.apache.camel.ProducerTemplate;
@Produce(uri = "activemq:queue:RechargeType")
private ProducerTemplate producerTemplate;
RechargeType rechargeType = new RechargeType();
producerTemplate.sendBody(rechargeType);
并在处理器类
中接收RechargeType request = (RechargeType) exchange.getIn().getBody();
但RechargeType和所有子类必须实现Serializable