Как отправить сообщение в WebSphere MQ с помощью Apche Camel и получить сообщение из очереди MQ - PullRequest
0 голосов
/ 11 июня 2019

Я не видел достаточного количества примеров в Интернете, использующих 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() для отправки сообщения? и Как создать сообщение, используя строку и добавить к нему заголовки.

1 Ответ

1 голос
/ 12 июня 2019

Добро пожаловать в stackoverflow!Я до сих пор не уверен, в чем именно заключается проблема, с которой вы застряли, и она мешает мне (и, возможно, другим) помочь вам в преодолении препятствий.

Возможно, вам нужно немного больше узнать о том, что такое верблюд и как он работает.Camel in Action - отличная книга, которая поможет вам в этом.

Если вы не можете получить копию на этом этапе, предварительный просмотр первых нескольких глав книги доступен онлайни это должно дать вам гораздо лучший рычаг.Хранилище исходного кода для глава 2 должно дать вам больше идей о том, как обрабатывать сообщения JMS.

В дополнение к этому.Пожалуйста, не ждите полномасштабных решений от StackOverflow.Вы можете прочитать эту страницу на , как задать хороший вопрос

...