Search in sources :

Example 21 with Topic

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

the class MQTTTest method testAmbiguousRoutingWithMQTT.

@Test(timeout = 60 * 1000)
public void testAmbiguousRoutingWithMQTT() throws Exception {
    String anycastAddress = "foo/bar";
    EnumSet<RoutingType> routingTypeSet = EnumSet.of(RoutingType.ANYCAST, RoutingType.MULTICAST);
    getServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("foo.bar"), routingTypeSet));
    String clientId = "testMqtt";
    Topic[] mqttSubscription = new Topic[] { new Topic(anycastAddress, QoS.AT_LEAST_ONCE) };
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId(clientId);
    BlockingConnection connection1 = mqtt.blockingConnection();
    connection1.connect();
    connection1.subscribe(mqttSubscription);
    MQTT mqtt2 = createMQTTConnection();
    mqtt2.setClientId(clientId + "2");
    BlockingConnection connection2 = mqtt2.blockingConnection();
    connection2.connect();
    connection2.subscribe(mqttSubscription);
    String message1 = "TestMessage1";
    String message2 = "TestMessage2";
    connection1.publish(anycastAddress, message1.getBytes(), QoS.AT_LEAST_ONCE, false);
    connection2.publish(anycastAddress, message2.getBytes(), QoS.AT_LEAST_ONCE, false);
    assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
    assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
    assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
    assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Example 22 with Topic

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

the class MQTTTest method testCleanSession.

@Test(timeout = 60 * 1000)
public void testCleanSession() throws Exception {
    final String CLIENTID = "cleansession";
    final MQTT mqttNotClean = createMQTTConnection(CLIENTID, false);
    BlockingConnection notClean = mqttNotClean.blockingConnection();
    final String TOPIC = "TopicA";
    notClean.connect();
    notClean.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
    notClean.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    notClean.disconnect();
    // MUST receive message from previous not clean session
    notClean = mqttNotClean.blockingConnection();
    notClean.connect();
    Message msg = notClean.receive(10000, TimeUnit.MILLISECONDS);
    assertNotNull(msg);
    assertEquals(TOPIC, new String(msg.getPayload()));
    msg.ack();
    notClean.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    notClean.disconnect();
    // MUST NOT receive message from previous not clean session
    final MQTT mqttClean = createMQTTConnection(CLIENTID, true);
    final BlockingConnection clean = mqttClean.blockingConnection();
    clean.connect();
    msg = clean.receive(10000, TimeUnit.MILLISECONDS);
    assertNull(msg);
    clean.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
    clean.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    clean.disconnect();
    // MUST NOT receive message from previous clean session
    notClean = mqttNotClean.blockingConnection();
    notClean.connect();
    msg = notClean.receive(1000, TimeUnit.MILLISECONDS);
    assertNull(msg);
    notClean.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 23 with Topic

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

the class MQTTTest method testRetainedMessagesAreCorrectlyFormedAfterRestart.

@Test
public void testRetainedMessagesAreCorrectlyFormedAfterRestart() throws Exception {
    String clientId = "testMqtt";
    String address = "testAddress";
    String payload = "This is a test message";
    // Create address
    getServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString(address), RoutingType.MULTICAST));
    // Send MQTT Retain Message
    Topic[] mqttTopic = new Topic[] { new Topic(address, QoS.AT_LEAST_ONCE) };
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId(clientId);
    BlockingConnection connection1 = mqtt.blockingConnection();
    connection1.connect();
    connection1.publish(address, payload.getBytes(), QoS.AT_LEAST_ONCE, true);
    getServer().fail(false);
    getServer().start();
    waitForServerToStart(getServer());
    MQTT mqtt2 = createMQTTConnection();
    mqtt2.setClientId(clientId + "2");
    BlockingConnection connection2 = mqtt2.blockingConnection();
    connection2.connect();
    connection2.subscribe(mqttTopic);
    Message message = connection2.receive(5000, TimeUnit.MILLISECONDS);
    assertEquals(payload, new String(message.getPayload()));
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Example 24 with Topic

use of org.fusesource.mqtt.client.Topic in project storm by apache.

the class MqttSpout method connectMqtt.

private void connectMqtt() throws Exception {
    String clientId = this.topologyName + "-" + this.context.getThisComponentId() + "-" + this.context.getThisTaskId();
    MQTT client = MqttUtils.configureClient(this.options, clientId, this.keyStoreLoader);
    this.connection = client.callbackConnection();
    this.connection.listener(this);
    this.connection.connect(new ConnectCallback());
    while (!this.mqttConnected && !this.mqttConnectFailed) {
        LOG.info("Waiting for connection...");
        Thread.sleep(500);
    }
    if (this.mqttConnected) {
        List<String> topicList = this.options.getTopics();
        Topic[] topics = new Topic[topicList.size()];
        QoS qos = MqttUtils.qosFromInt(this.options.getQos());
        for (int i = 0; i < topicList.size(); i++) {
            topics[i] = new Topic(topicList.get(i), qos);
        }
        connection.subscribe(topics, new SubscribeCallback());
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) QoS(org.fusesource.mqtt.client.QoS) Topic(org.fusesource.mqtt.client.Topic)

Example 25 with Topic

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

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