Search in sources :

Example 1 with Topic

use of org.fusesource.mqtt.client.Topic 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 Topic

use of org.fusesource.mqtt.client.Topic 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 Topic

use of org.fusesource.mqtt.client.Topic 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 4 with Topic

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

the class MqttCrlEnabledExample method callBroker.

private static void callBroker(String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
    BlockingConnection connection = null;
    try {
        connection = retrieveMQTTConnection("ssl://localhost:1883", truststorePath, truststorePass, keystorePath, keystorePass);
        // Subscribe to topics
        Topic[] topics = { new Topic("test/+/some/#", QoS.AT_MOST_ONCE) };
        connection.subscribe(topics);
        // Publish Messages
        String payload = "This is message 1";
        connection.publish("test/1/some/la", payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        Message message = connection.receive(5, TimeUnit.SECONDS);
        System.out.println("Message received: " + new String(message.getPayload()));
    } catch (Exception e) {
        throw e;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
Also used : Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) SSLException(javax.net.ssl.SSLException)

Example 5 with Topic

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

the class MQTTPublishSubscribeExample method main.

public static void main(final String[] args) throws Exception {
    // Create a new MQTT connection to the broker.  We are not setting the client ID.  The broker will pick one for us.
    System.out.println("Connecting to Artemis using MQTT");
    MQTT mqtt = new MQTT();
    mqtt.setHost("tcp://localhost:1883");
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    System.out.println("Connected to Artemis");
    // Subscribe to topics
    Topic[] topics = { new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE) };
    connection.subscribe(topics);
    System.out.println("Subscribed to topics.");
    // Publish Messages
    String payload1 = "This is message 1";
    String payload2 = "This is message 2";
    String payload3 = "This is message 3";
    connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
    connection.publish("test/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
    connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
    System.out.println("Sent messages.");
    Message message1 = connection.receive(5, TimeUnit.SECONDS);
    Message message2 = connection.receive(5, TimeUnit.SECONDS);
    Message message3 = connection.receive(5, TimeUnit.SECONDS);
    System.out.println("Received messages.");
    System.out.println(new String(message1.getPayload()));
    System.out.println(new String(message2.getPayload()));
    System.out.println(new String(message3.getPayload()));
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic)

Aggregations

Topic (org.fusesource.mqtt.client.Topic)52 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)48 Test (org.junit.Test)41 MQTT (org.fusesource.mqtt.client.MQTT)39 Message (org.fusesource.mqtt.client.Message)36 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)21 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)18 BytesMessage (javax.jms.BytesMessage)17 Tracer (org.fusesource.mqtt.client.Tracer)6 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)6 ProtocolException (java.net.ProtocolException)5 QoS (org.fusesource.mqtt.client.QoS)4 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 Ignore (org.junit.Ignore)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)3 URI (java.net.URI)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 WildcardConfiguration (org.apache.activemq.artemis.core.config.WildcardConfiguration)2