Search in sources :

Example 1 with Stomp

use of org.fusesource.stomp.client.Stomp in project camel by apache.

the class StompProducerTest method testProduce.

@Test
public void testProduce() throws Exception {
    if (!canTest()) {
        return;
    }
    context.addRoutes(createRouteBuilder());
    context.start();
    Stomp stomp = createStompClient();
    final BlockingConnection subscribeConnection = stomp.connectBlocking();
    StompFrame frame = new StompFrame(SUBSCRIBE);
    frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test"));
    frame.addHeader(ID, subscribeConnection.nextId());
    StompFrame response = subscribeConnection.request(frame);
    final CountDownLatch latch = new CountDownLatch(numberOfMessages);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            for (int i = 0; i < numberOfMessages; i++) {
                try {
                    StompFrame frame = subscribeConnection.receive();
                    frame.contentAsString().startsWith("test message ");
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    });
    thread.start();
    Producer producer = context.getEndpoint("direct:foo").createProducer();
    for (int i = 0; i < numberOfMessages; i++) {
        Exchange exchange = producer.createExchange();
        exchange.getIn().setBody(("test message " + i).getBytes("UTF-8"));
        producer.process(exchange);
    }
    latch.await(20, TimeUnit.SECONDS);
    assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
Also used : Exchange(org.apache.camel.Exchange) Producer(org.apache.camel.Producer) StompFrame(org.fusesource.stomp.codec.StompFrame) Stomp(org.fusesource.stomp.client.Stomp) BlockingConnection(org.fusesource.stomp.client.BlockingConnection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Stomp

use of org.fusesource.stomp.client.Stomp in project camel by apache.

the class StompEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    final Promise<CallbackConnection> promise = new Promise<>();
    stomp = new Stomp(configuration.getBrokerURL());
    stomp.setLogin(configuration.getLogin());
    stomp.setPasscode(configuration.getPasscode());
    if (configuration.getSslContextParameters() != null) {
        stomp.setSslContext(configuration.getSslContextParameters().createSSLContext(getCamelContext()));
    }
    stomp.connectCallback(promise);
    if (configuration.getHost() != null && !configuration.getHost().isEmpty()) {
        stomp.setHost(configuration.getHost());
    }
    connection = promise.await();
    connection.getDispatchQueue().execute(new Task() {

        @Override
        public void run() {
            connection.receive(new Callback<StompFrame>() {

                @Override
                public void onFailure(Throwable value) {
                    if (started.get()) {
                        connection.close(null);
                    }
                }

                @Override
                public void onSuccess(StompFrame value) {
                    if (!consumers.isEmpty()) {
                        Exchange exchange = createExchange();
                        exchange.getIn().setBody(value.content());
                        for (StompConsumer consumer : consumers) {
                            consumer.processExchange(exchange);
                        }
                    }
                }
            });
            connection.resume();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Promise(org.fusesource.stomp.client.Promise) CallbackConnection(org.fusesource.stomp.client.CallbackConnection) Task(org.fusesource.hawtdispatch.Task) Callback(org.fusesource.stomp.client.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StompFrame(org.fusesource.stomp.codec.StompFrame) Stomp(org.fusesource.stomp.client.Stomp)

Example 3 with Stomp

use of org.fusesource.stomp.client.Stomp in project camel by apache.

the class StompBaseTest method createStompClient.

protected Stomp createStompClient() throws Exception {
    Stomp stomp;
    if (isUseSsl()) {
        stomp = new Stomp("ssl://localhost:" + getPort());
        stomp.setSslContext(getClientSSLContext());
    } else {
        stomp = new Stomp("tcp://localhost:" + getPort());
    }
    return stomp;
}
Also used : Stomp(org.fusesource.stomp.client.Stomp)

Example 4 with Stomp

use of org.fusesource.stomp.client.Stomp in project camel by apache.

the class StompConsumerTest method testConsume.

@Test
public void testConsume() throws Exception {
    if (!canTest()) {
        return;
    }
    context.addRoutes(createRouteBuilder());
    context.start();
    Stomp stomp = createStompClient();
    final BlockingConnection producerConnection = stomp.connectBlocking();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages);
    for (int i = 0; i < numberOfMessages; i++) {
        StompFrame frame = new StompFrame(SEND);
        frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test"));
        frame.addHeader(MESSAGE_ID, StompFrame.encodeHeader("msg:" + i));
        frame.content(utf8("Important Message " + i));
        producerConnection.send(frame);
    }
    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StompFrame(org.fusesource.stomp.codec.StompFrame) Stomp(org.fusesource.stomp.client.Stomp) BlockingConnection(org.fusesource.stomp.client.BlockingConnection) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 5 with Stomp

use of org.fusesource.stomp.client.Stomp in project camel by apache.

the class StompConsumerUriTest method testConsume.

@Test
public void testConsume() throws Exception {
    if (!canTest()) {
        return;
    }
    context.addRoutes(createRouteBuilder());
    context.start();
    Stomp stomp = createStompClient();
    final BlockingConnection producerConnection = stomp.connectBlocking();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages);
    for (int i = 0; i < numberOfMessages; i++) {
        StompFrame frame = new StompFrame(SEND);
        frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test"));
        frame.addHeader(MESSAGE_ID, StompFrame.encodeHeader("msg:" + i));
        frame.content(utf8("Important Message " + i));
        producerConnection.send(frame);
    }
    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StompFrame(org.fusesource.stomp.codec.StompFrame) Stomp(org.fusesource.stomp.client.Stomp) BlockingConnection(org.fusesource.stomp.client.BlockingConnection) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

Stomp (org.fusesource.stomp.client.Stomp)5 StompFrame (org.fusesource.stomp.codec.StompFrame)4 BlockingConnection (org.fusesource.stomp.client.BlockingConnection)3 Test (org.junit.Test)3 Exchange (org.apache.camel.Exchange)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AsyncCallback (org.apache.camel.AsyncCallback)1 Producer (org.apache.camel.Producer)1 Task (org.fusesource.hawtdispatch.Task)1 Callback (org.fusesource.stomp.client.Callback)1 CallbackConnection (org.fusesource.stomp.client.CallbackConnection)1 Promise (org.fusesource.stomp.client.Promise)1