Apache camel ProducerTemplate asyncSendBody 设置 JMS headers 和属性

Apache camel ProducerTemplate asyncSendBody set JMS headers and properties

Apache 骆驼 api 有 ProducerTemplate.asyncSendBody(Endpoint endpoint, Object body)。 我使用上面的方法将消息发送到远程端点。

我想知道如何设置其他 JMS 属性和 headers。

我看到有一个 api sendBodyAndHeader() 允许这样做。它在异步 api 中的等价物是什么?

您可以使用 asyncRequestBodyAndHeader 方法异步发送 Camel Exchange。骆驼交换将在 Exchange.InOut MEP 中发送。

这是一个您可以遵循的测试用例,

protected Object expectedBody = "<time>" + new Date() + "";
protected ActiveMQQueue replyQueue = new ActiveMQQueue("test.reply.queue");
protected String correlationID = "ABC-123";
protected String messageType = getClass().getName();

public void testForwardingAMessageAcrossJMSKeepingCustomJMSHeaders() throws Exception {
    MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedBodiesReceived(expectedBody);
    AssertionClause firstMessageExpectations = resultEndpoint.message(0);
    firstMessageExpectations.header("cheese").isEqualTo(123);        firstMessageExpectations.header("JMSReplyTo").isEqualTo(replyQueue);
    firstMessageExpectations.header("JMSCorrelationID").isEqualTo(correlationID);
    firstMessageExpectations.header("JMSType").isEqualTo(messageType);
    template.asyncRequestBodyAndHeader("activemq:test.a", expectedBody, "cheese", 123);
    resultEndpoint.assertIsSatisfied();
    List<Exchange> list = resultEndpoint.getReceivedExchanges();
    Exchange exchange = list.get(0);
    Object replyTo = exchange.getIn().getHeader("JMSReplyTo");
    LOG.info("Reply to is: " + replyTo);
    Destination destination = assertIsInstanceOf(Destination.class, replyTo);
    assertEquals("ReplyTo", replyQueue.toString(), destination.toString());
}