Search in sources :

Example 71 with BlockingConnection

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

the class MQTTClient method main.

/**
 * Only execution point for this application.
 * @param args command line args.
 * @throws Exception if something goes wrong.
 */
public static void main(final String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(MESSAGE_PAYLOAD)));
    String payload = reader.readLine();
    reader.close();
    BlockingConnection publishConnection = null;
    BlockingConnection subscribeConnection = null;
    try {
        Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
        MQTT mqtt = new MQTT();
        mqtt.setUserName(USER_NAME);
        mqtt.setPassword(PASSWORD);
        subscribeConnection = mqtt.blockingConnection();
        subscribeConnection.connect();
        subscribeConnection.subscribe(new Topic[] { outputTopic });
        publishConnection = mqtt.blockingConnection();
        publishConnection.connect();
        publishConnection.publish(TOPIC_INPUT, payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        System.out.println("Published a message to " + TOPIC_INPUT + ": " + payload);
        Message msg = subscribeConnection.receive(60000, TimeUnit.MILLISECONDS);
        if (msg != null) {
            System.out.println("Received a message from " + TOPIC_OUTPUT + ": " + new String(msg.getPayload()));
        } else {
            System.out.println("No message was received from " + TOPIC_OUTPUT);
        }
    } finally {
        if (publishConnection != null && publishConnection.isConnected()) {
            publishConnection.disconnect();
        }
        if (subscribeConnection != null && subscribeConnection.isConnected()) {
            subscribeConnection.disconnect();
        }
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) InputStreamReader(java.io.InputStreamReader) Message(org.fusesource.mqtt.client.Message) BufferedReader(java.io.BufferedReader) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic)

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