Search in sources :

Example 36 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 37 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project quickstarts by jboss-switchyard.

the class CamelMQTTBindingTest method testServiceBinding.

@Test
public void testServiceBinding() throws Exception {
    MockHandler mock = _testKit.replaceService("StoreReference");
    MQTT mqtt = new MQTT();
    BlockingConnection connection = mqtt.blockingConnection();
    try {
        connection.connect();
        connection.publish(TOPIC_INPUT, MESSAGE_INPUT.getBytes(), QoS.AT_LEAST_ONCE, false);
        Thread.sleep(1000);
        LinkedBlockingQueue<Exchange> received = mock.getMessages();
        Assert.assertNotNull(received);
        Exchange exchange = received.iterator().next();
        Assert.assertEquals(MESSAGE_OUTPUT, exchange.getMessage().getContent(String.class));
    } finally {
        connection.disconnect();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Exchange(org.switchyard.Exchange) MockHandler(org.switchyard.test.MockHandler) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 38 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project quickstarts by jboss-switchyard.

the class CamelMQTTBindingTest method before.

@Before
public void before() {
    MQTT mqtt = new MQTT();
    Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
    BlockingConnection connection = mqtt.blockingConnection();
    try {
        connection.connect();
        connection.subscribe(new Topic[] { outputTopic });
        while (connection.receive(1000, TimeUnit.MILLISECONDS) != null) ;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Before(org.junit.Before)

Example 39 with BlockingConnection

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

the class MQTTProducerReconnectTest method testProduce.

@Test
public void testProduce() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    final BlockingConnection subscribeConnection = mqtt.blockingConnection();
    subscribeConnection.connect();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic[] topics = { topic };
    subscribeConnection.subscribe(topics);
    final CountDownLatch latch = new CountDownLatch(numberOfMessages);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            for (int i = 0; i < numberOfMessages; i++) {
                try {
                    Message message = subscribeConnection.receive();
                    message.ack();
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    });
    thread.start();
    for (int i = 0; i < numberOfMessages; i++) {
        if (i == 5) {
            LOG.info("Stopping MQTT transport");
            brokerService.getTransportConnectorByScheme("mqtt").stop();
            Thread starter = new Thread(new Runnable() {

                public void run() {
                    try {
                        Thread.sleep(3000);
                        LOG.info("Starting MQTT transport again");
                        brokerService.getTransportConnectorByScheme("mqtt").start();
                    } catch (Exception e) {
                    // ignore
                    }
                }
            });
            starter.start();
        }
        try {
            template.sendBody("direct:foo", "test message " + i);
        } catch (Exception e) {
        // ignore
        }
    }
    latch.await(20, TimeUnit.SECONDS);
    assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 40 with BlockingConnection

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

the class MQTTProducerTest method testProduce.

@Test
public void testProduce() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    final BlockingConnection subscribeConnection = mqtt.blockingConnection();
    subscribeConnection.connect();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic[] topics = { topic };
    subscribeConnection.subscribe(topics);
    final CountDownLatch latch = new CountDownLatch(numberOfMessages);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            for (int i = 0; i < numberOfMessages; i++) {
                try {
                    Message message = subscribeConnection.receive();
                    message.ack();
                    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);
        producer.process(exchange);
    }
    latch.await(20, TimeUnit.SECONDS);
    assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Exchange(org.apache.camel.Exchange) Message(org.fusesource.mqtt.client.Message) Producer(org.apache.camel.Producer) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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