Search in sources :

Example 1 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project camel by apache.

the class MQTTConsumerMultipleTopicsTest method testConsumeMultipleTopics.

@Test
public void testConsumeMultipleTopics() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    BlockingConnection publisherConnection = mqtt.blockingConnection();
    Topic topic1 = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic topic2 = new Topic(TEST_TOPIC_2, QoS.AT_MOST_ONCE);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages * 2);
    publisherConnection.connect();
    String payload;
    for (int i = 0; i < numberOfMessages; i++) {
        payload = "Topic 1, Message " + i;
        publisherConnection.publish(topic1.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        payload = "Topic 2, Message " + i;
        publisherConnection.publish(topic2.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
    }
    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 2 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project camel by apache.

the class MQTTConsumerTest method testConsume.

@Test
public void testConsume() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    BlockingConnection publisherConnection = mqtt.blockingConnection();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages);
    publisherConnection.connect();
    for (int i = 0; i < numberOfMessages; i++) {
        String payload = "Message " + i;
        publisherConnection.publish(topic.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
    }
    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 3 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project camel by apache.

the class MQTTConsumerWildcardTopicsTest method testConsumeMultipleTopicsWithWildcards.

@Test
public void testConsumeMultipleTopicsWithWildcards() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    BlockingConnection publisherConnection = mqtt.blockingConnection();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages * (PUBLISH_TOPICS.length - 1));
    publisherConnection.connect();
    String payload;
    for (String topic : PUBLISH_TOPICS) {
        for (int i = 0; i < numberOfMessages; i++) {
            payload = "Topic " + topic + ", Message " + i;
            publisherConnection.publish(topic, payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        }
    }
    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 4 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project wildfly-camel by wildfly-extras.

the class MQTTIntegrationTest method testMQTTConsumer.

@Test
public void testMQTTConsumer() throws Exception {
    String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("mqtt:bar?subscribeTopicName=" + BrokerSetup.TEST_TOPIC + "&host=" + conUrl).transform(body().prepend("Hello ")).to("seda:end");
        }
    });
    camelctx.start();
    PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer();
    consumer.start();
    try {
        MQTT mqtt = new MQTT();
        mqtt.setHost(conUrl);
        BlockingConnection connection = mqtt.blockingConnection();
        Topic topic = new Topic(BrokerSetup.TEST_TOPIC, QoS.AT_MOST_ONCE);
        connection.connect();
        try {
            connection.publish(topic.name().toString(), "Kermit".getBytes(), QoS.AT_LEAST_ONCE, false);
        } finally {
            connection.disconnect();
        }
        String result = consumer.receive(3000).getIn().getBody(String.class);
        Assert.assertEquals("Hello Kermit", result);
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) MQTT(org.fusesource.mqtt.client.MQTT) PollingConsumer(org.apache.camel.PollingConsumer) RouteBuilder(org.apache.camel.builder.RouteBuilder) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 5 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.

the class ClusteredQueueMQTTExample method retrieveMQTTConnection.

private static BlockingConnection retrieveMQTTConnection(String host) throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(host);
    mqtt.setUserName("admin");
    mqtt.setPassword("admin");
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    return connection;
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection)

Aggregations

BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)71 MQTT (org.fusesource.mqtt.client.MQTT)61 Test (org.junit.Test)58 Topic (org.fusesource.mqtt.client.Topic)48 Message (org.fusesource.mqtt.client.Message)36 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 BytesMessage (javax.jms.BytesMessage)18 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)18 ProtocolException (java.net.ProtocolException)6 Tracer (org.fusesource.mqtt.client.Tracer)6 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)6 Ignore (org.junit.Ignore)6 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)3 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 EOFException (java.io.EOFException)2 URI (java.net.URI)2