Search in sources :

Example 6 with Topic

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

the class MQTTNetworkOfBrokersFailoverTest method testNoStaleSubscriptionAcrossNetwork.

@Test
public void testNoStaleSubscriptionAcrossNetwork() throws Exception {
    // before we get started, we want an async way to be able to know when
    // the durable consumer has been networked so we can assert that it indeed
    // would have a durable subscriber. for example, when we subscribe on remote broker,
    // a network-sub would be created on local broker and we want to listen for when that
    // even happens. we do that with advisory messages and a latch:
    CountDownLatch consumerNetworked = listenForConsumersOn(broker);
    // create a subscription with Clean == 0 (durable sub for QoS==1 && QoS==2)
    // on the remote broker. this sub should still be there after we disconnect
    MQTT remoteMqtt = createMQTTTcpConnection("foo", false, remoteBrokerMQTTPort);
    BlockingConnection remoteConn = remoteMqtt.blockingConnection();
    remoteConn.connect();
    remoteConn.subscribe(new Topic[] { new Topic("foo/bar", QoS.AT_LEAST_ONCE) });
    assertTrue("No destination detected!", consumerNetworked.await(1, TimeUnit.SECONDS));
    assertQueueExistsOn(remoteBroker, "Consumer.foo_AT_LEAST_ONCE.VirtualTopic.foo.bar");
    assertQueueExistsOn(broker, "Consumer.foo_AT_LEAST_ONCE.VirtualTopic.foo.bar");
    remoteConn.disconnect();
    // now we reconnect the same sub on the local broker, again with clean==0
    MQTT localMqtt = createMQTTTcpConnection("foo", false, localBrokerMQTTPort);
    BlockingConnection localConn = localMqtt.blockingConnection();
    localConn.connect();
    localConn.subscribe(new Topic[] { new Topic("foo/bar", QoS.AT_LEAST_ONCE) });
    // now let's connect back up to remote broker and send a message
    remoteConn = remoteMqtt.blockingConnection();
    remoteConn.connect();
    remoteConn.publish("foo/bar", "Hello, World!".getBytes(), QoS.AT_LEAST_ONCE, false);
    // now we should see that message on the local broker because the subscription
    // should have been properly networked... we'll give a sec of grace for the
    // networking and forwarding to have happened properly
    org.fusesource.mqtt.client.Message msg = localConn.receive(100, TimeUnit.SECONDS);
    assertNotNull(msg);
    msg.ack();
    String response = new String(msg.getPayload());
    assertEquals("Hello, World!", response);
    assertEquals("foo/bar", msg.getTopic());
    // Now... we SHOULD NOT see a message on the remote broker because we already
    // consumed it on the local broker... having the same message on the remote broker
    // would effectively give us duplicates in a distributed topic scenario:
    remoteConn.subscribe(new Topic[] { new Topic("foo/bar", QoS.AT_LEAST_ONCE) });
    msg = remoteConn.receive(500, TimeUnit.MILLISECONDS);
    assertNull("We have duplicate messages across the cluster for a distributed topic", msg);
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 7 with Topic

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

the class MQTTSecurityCRLTest method crlRevokedTest.

/**
 * These artifacts are required for testing mqtt with CRL
 * <p>
 * openssl genrsa -out ca.key 2048
 * openssl req -new -x509 -days 1826 -key ca.key -out ca.crt
 * touch certindex
 * echo 01 > certserial
 * echo 01 > crlnumber
 * <p>
 * Create ca.conf file with
 * <p>
 * [ ca ]
 * default_ca = myca
 * <p>
 * [ crl_ext ]
 * # issuerAltName=issuer:copy #this would copy the issuer name to altname
 * authorityKeyIdentifier=keyid:always
 * <p>
 * [ myca ]
 * dir = ./
 * new_certs_dir = $dir
 * unique_subject = no
 * certificate = $dir/ca.crt
 * database = $dir/certindex
 * private_key = $dir/ca.key
 * serial = $dir/certserial
 * default_days = 730
 * default_md = sha1
 * policy = myca_policy
 * x509_extensions = myca_extensions
 * crlnumber = $dir/crlnumber
 * default_crl_days = 730
 * <p>
 * [ myca_policy ]
 * commonName = supplied
 * stateOrProvinceName = supplied
 * countryName = optional
 * emailAddress = optional
 * organizationName = supplied
 * organizationalUnitName = optional
 * <p>
 * [ myca_extensions ]
 * basicConstraints = CA:false
 * subjectKeyIdentifier = hash
 * authorityKeyIdentifier = keyid:always
 * keyUsage = digitalSignature,keyEncipherment
 * extendedKeyUsage = serverAuth, clientAuth
 * crlDistributionPoints = URI:http://example.com/root.crl
 * subjectAltName = @alt_names
 * <p>
 * [alt_names]
 * DNS.1 = example.com
 * DNS.2 = *.example.com
 * <p>
 * Continue executing the commands:
 * <p>
 * openssl genrsa -out keystore1.key 2048
 * openssl req -new -key keystore1.key -out keystore1.csr
 * openssl ca -batch -config ca.conf -notext -in keystore1.csr -out keystore1.crt
 * openssl genrsa -out client_revoked.key 2048
 * openssl req -new -key client_revoked.key -out client_revoked.csr
 * openssl ca -batch -config ca.conf -notext -in client_revoked.csr -out client_revoked.crt
 * openssl genrsa -out client_not_revoked.key 2048
 * openssl req -new -key client_not_revoked.key -out client_not_revoked.csr
 * openssl ca -batch -config ca.conf -notext -in client_not_revoked.csr -out client_not_revoked.crt
 * openssl ca -config ca.conf -gencrl -keyfile ca.key -cert ca.crt -out root.crl.pem
 * openssl ca -config ca.conf -revoke client_revoked.crt -keyfile ca.key -cert ca.crt
 * openssl ca -config ca.conf -gencrl -keyfile ca.key -cert ca.crt -out root.crl.pem
 * <p>
 * openssl pkcs12 -export -name client_revoked -in client_revoked.crt -inkey client_revoked.key -out client_revoked.p12
 * keytool -importkeystore -destkeystore client_revoked.jks -srckeystore client_revoked.p12 -srcstoretype pkcs12 -alias client_revoked
 * <p>
 * openssl pkcs12 -export -name client_not_revoked -in client_not_revoked.crt -inkey client_not_revoked.key -out client_not_revoked.p12
 * keytool -importkeystore -destkeystore client_not_revoked.jks -srckeystore client_not_revoked.p12 -srcstoretype pkcs12 -alias client_not_revoked
 * <p>
 * openssl pkcs12 -export -name keystore1 -in keystore1.crt -inkey keystore1.key -out keystore1.p12
 * keytool -importkeystore -destkeystore keystore1.jks -srckeystore keystore1.p12 -srcstoretype pkcs12 -alias keystore1
 * <p>
 * keytool -import -trustcacerts -alias trust_key -file ca.crt -keystore truststore.jks
 */
@Test(expected = SSLException.class)
public void crlRevokedTest() throws Exception {
    ActiveMQServer server1 = initServer();
    BlockingConnection connection1 = null;
    try {
        server1.start();
        while (!server1.isStarted()) {
            Thread.sleep(50);
        }
        connection1 = retrieveMQTTConnection("ssl://localhost:1883", "truststore.jks", "changeit", "client_revoked.jks", "changeit");
        // Subscribe to topics
        Topic[] topics = { new Topic("test/+/some/#", QoS.AT_MOST_ONCE) };
        connection1.subscribe(topics);
        // Publish Messages
        String payload1 = "This is message 1";
        connection1.publish("test/1/some/la", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
        Message message1 = connection1.receive(5, TimeUnit.SECONDS);
        assertEquals(payload1, new String(message1.getPayload()));
    } finally {
        if (connection1 != null) {
            connection1.disconnect();
        }
        if (server1.isStarted()) {
            server1.stop();
        }
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 8 with Topic

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

the class MQTTSecurityCRLTest method crlNotRevokedTest.

@Test
public void crlNotRevokedTest() throws Exception {
    ActiveMQServer server1 = initServer();
    BlockingConnection connection1 = null;
    try {
        server1.start();
        while (!server1.isStarted()) {
            Thread.sleep(50);
        }
        connection1 = retrieveMQTTConnection("ssl://localhost:1883", "truststore.jks", "changeit", "client_not_revoked.jks", "changeit");
        // Subscribe to topics
        Topic[] topics = { new Topic("test/+/some/#", QoS.AT_MOST_ONCE) };
        connection1.subscribe(topics);
        // Publish Messages
        String payload1 = "This is message 1";
        connection1.publish("test/1/some/la", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
        Message message1 = connection1.receive(5, TimeUnit.SECONDS);
        assertEquals(payload1, new String(message1.getPayload()));
    } finally {
        if (connection1 != null) {
            connection1.disconnect();
        }
        if (server1.isStarted()) {
            server1.stop();
        }
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 9 with Topic

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

the class MQTTTest method testClientConnectionFailureSendsWillMessage.

@Test(timeout = 60 * 1000)
public void testClientConnectionFailureSendsWillMessage() throws Exception {
    getServer().createQueue(SimpleString.toSimpleString("will"), RoutingType.MULTICAST, SimpleString.toSimpleString("will"), null, true, false);
    MQTT mqtt = createMQTTConnection("1", false);
    mqtt.setKeepAlive((short) 1);
    mqtt.setWillMessage("test message");
    mqtt.setWillTopic("will");
    mqtt.setWillQos(QoS.AT_LEAST_ONCE);
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    Wait.waitFor(() -> connection.isConnected());
    MQTT mqtt2 = createMQTTConnection("2", false);
    BlockingConnection connection2 = mqtt2.blockingConnection();
    connection2.connect();
    connection2.subscribe(new Topic[] { new Topic("will", QoS.AT_LEAST_ONCE) });
    // kill transport
    connection.kill();
    // FIXME Wait for the previous connection to timeout.  This is not required in ActiveMQ.  Needs investigating.
    Thread.sleep(10000);
    Message m = connection2.receive(1000, TimeUnit.MILLISECONDS);
    assertEquals("test message", new String(m.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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 10 with Topic

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

the class MQTTTest method testRetainedMessage.

@Test(timeout = 120 * 1000)
public void testRetainedMessage() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setKeepAlive((short) 60);
    final String RETAIN = "RETAIN";
    final String TOPICA = "TopicA";
    final String[] clientIds = { null, "foo", "durable" };
    for (String clientId : clientIds) {
        LOG.info("Testing now with Client ID: {}", clientId);
        mqtt.setClientId(clientId);
        mqtt.setCleanSession(!"durable".equals(clientId));
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        // set retained message and check
        connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No retained message for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        // test duplicate subscription
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(15000, TimeUnit.MILLISECONDS);
        assertNotNull("No retained message on duplicate subscription for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        connection.unsubscribe(new String[] { TOPICA });
        // clear retained message and check that we don't receive it
        connection.publish(TOPICA, "".getBytes(), QoS.AT_MOST_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(500, TimeUnit.MILLISECONDS);
        assertNull("Retained message not cleared for " + clientId, msg);
        connection.unsubscribe(new String[] { TOPICA });
        // set retained message again and check
        connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No reset retained message for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        // re-connect and check
        connection.disconnect();
        connection = mqtt.blockingConnection();
        connection.connect();
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No reset retained message for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        connection.unsubscribe(new String[] { TOPICA });
        connection.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)

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