如何使用 Apche Camel 向 WebSphere MQ 发送消息并从 MQ 队列接收消息

问题描述 投票:0回答:1

我在网络上没有看到足够的使用 apache Camel 和 websphere mq 来发送和接收消息的示例。我有一个示例代码,但我在代码中间遇到了困难。任何人都可以帮忙解决这个问题吗..

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Producer;
import org.apache.camel.util.IOHelper;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Client that uses the <a href="http://camel.apache.org/message-endpoint.html">Mesage Endpoint</a>
 * pattern to easily exchange messages with the Server.
 * <p/>
 * Notice this very same API can use for all components in Camel, so if we were using TCP communication instead
 * of JMS messaging we could just use <code>camel.getEndpoint("mina:tcp://someserver:port")</code>.
 * <p/>
 * Requires that the JMS broker is running, as well as CamelServer
 */
public final class CamelClientEndpoint {
    private CamelClientEndpoint() {
        //Helper class
    }

    // START SNIPPET: e1
    public static void main(final String[] args) throws Exception {
        System.out.println("Notice this client requires that the CamelServer is already running!");

        AbstractApplicationContext context = new ClassPathXmlApplicationContext("camel-client.xml");
        CamelContext camel = context.getBean("camel-client", CamelContext.class);

        // get the endpoint from the camel context
        Endpoint endpoint = camel.getEndpoint("jms:queue:numbers");

        // create the exchange used for the communication
        // we use the in out pattern for a synchronized exchange where we expect a response
        Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
        // set the input on the in body
        // must be correct type to match the expected type of an Integer object
        exchange.getIn().setBody(11);

        // to send the exchange we need an producer to do it for us
        Producer producer = endpoint.createProducer();
        // start the producer so it can operate
        producer.start();

        // let the producer process the exchange where it does all the work in this oneline of code
        System.out.println("Invoking the multiply with 11");
        producer.process(exchange);

        // get the response from the out body and cast it to an integer
        int response = exchange.getOut().getBody(Integer.class);
        System.out.println("... the result is: " + response);

        // stopping the JMS producer has the side effect of the "ReplyTo Queue" being properly
        // closed, making this client not to try any further reads for the replies from the server
        producer.stop();

        // we're done so let's properly close the application context
        IOHelper.close(context);
    }

}

我在代码的这一点上受到了打击..

exchange.getIn()

我必须使用

exchange.getOut()
来发送消息吗?以及如何使用字符串构造消息并向其添加标头。

spring apache-camel ibm-mq
1个回答
1
投票

也许您需要更多地熟悉什么是 Camel 以及它是如何工作的。

Camel in Action
是一本可以帮助你的好书。

如果您此时无法获得副本,可以在线获取本书前几章的“预览”,它应该会给您带来更好的影响力。 第 2 章 的源代码存储库应该为您提供有关如何处理 JMS 消息的更多想法。

© www.soinside.com 2019 - 2024. All rights reserved.