如何使用Apche Camel将消息发送到WebSphere MQ并从MQ Queue接收消息

时间:2019-06-11 13:18:24

标签: spring apache-camel ibm-mq

我在Web上没有看到足够的示例,该示例使用带有Websphere MQ的Apache骆驼来发送和接收消息。我有一个示例代码,但是我对代码的中间感到震惊。谁能帮上忙。.

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()发送消息吗?以及如何使用字符串构造消息并向其添加标头。

1 个答案:

答案 0 :(得分:1)

欢迎来到stackoverflow! 我仍然不确定您所坚持的问题到底是什么,它使我(可能还有其他人)无法帮助您解决障碍。

也许您需要更多地了解骆驼是什么以及它如何工作。 Camel in Action是一本很棒的书,可以帮助您。

如果您目前无法获得副本,则可以在线获取本书前几章的preview,它应能为您提供更好的杠杆作用。 chapter 2的源代码存储库应为您提供有关如何处理JMS消息的更多思路。

除此之外。请不要期望StackOverflow提供完整的解决方案。您可以在how to ask a good question

上阅读此页面