Search in sources :

Example 31 with BlockingConnection

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

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

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

the class MqttClusterRemoteSubscribeTest method retrieveMQTTConnection.

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

Example 34 with BlockingConnection

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

the class MQTTSecurityTest method testConnectionWithNullPassword.

@Test(timeout = 30000, expected = EOFException.class)
public void testConnectionWithNullPassword() throws Exception {
    for (String version : Arrays.asList("3.1", "3.1.1")) {
        BlockingConnection connection = null;
        try {
            MQTT mqtt = createMQTTConnection("test-" + version, true);
            mqtt.setUserName(fullUser);
            mqtt.setPassword((String) null);
            mqtt.setConnectAttemptsMax(1);
            mqtt.setVersion(version);
            connection = mqtt.blockingConnection();
            connection.connect();
            fail("Connect should fail");
        } finally {
            if (connection != null && connection.isConnected())
                connection.disconnect();
        }
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 35 with BlockingConnection

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

the class MqttClusterWildcardTest method retrieveMQTTConnection.

private static BlockingConnection retrieveMQTTConnection(String host) throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(host);
    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