Search in sources :

Example 21 with BlockingConnection

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

the class MQTTTest method doTestJmsMapping.

// TODO As with other tests, this should be enabled as part of the cross protocol support with MQTT.
public void doTestJmsMapping(String destinationName) throws Exception {
    // start up jms consumer
    Connection jmsConn = cf.createConnection();
    Session session = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = session.createQueue(destinationName);
    MessageConsumer consumer = session.createConsumer(dest);
    jmsConn.start();
    // set up mqtt producer
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo3");
    mqtt.setKeepAlive((short) 2);
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    int messagesToSend = 5;
    // publish
    for (int i = 0; i < messagesToSend; ++i) {
        connection.publish("test/foo", "hello world".getBytes(), QoS.AT_LEAST_ONCE, false);
    }
    connection.disconnect();
    for (int i = 0; i < messagesToSend; i++) {
        javax.jms.Message message = consumer.receive(2 * 1000);
        assertNotNull(message);
        assertTrue(message instanceof BytesMessage);
        BytesMessage bytesMessage = (BytesMessage) message;
        int length = (int) bytesMessage.getBodyLength();
        byte[] buffer = new byte[length];
        bytesMessage.readBytes(buffer);
        assertEquals("hello world", new String(buffer));
    }
    jmsConn.close();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Session(javax.jms.Session) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) MQTTSession(org.apache.activemq.artemis.core.protocol.mqtt.MQTTSession)

Example 22 with BlockingConnection

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

the class MQTTTest method testPingOnMQTT.

// TODO This should be reworked to align with Artemis recovery.
// @Test(timeout = 60 * 1000)
// public void testActiveMQRecoveryPolicy() throws Exception {
// // test with ActiveMQ LastImageSubscriptionRecoveryPolicy
// final PolicyMap policyMap = new PolicyMap();
// final PolicyEntry policyEntry = new PolicyEntry();
// policyEntry.setSubscriptionRecoveryPolicy(new LastImageSubscriptionRecoveryPolicy());
// policyMap.put(new ActiveMQTopic(">"), policyEntry);
// brokerService.setDestinationPolicy(policyMap);
// 
// MQTT mqtt = createMQTTConnection("pub-sub", true);
// final int[] retain = new int[1];
// final int[] nonretain  = new int[1];
// mqtt.setTracer(new Tracer() {
// @Override
// public void onReceive(MQTTFrame frame) {
// if (frame.messageType() == PUBLISH.TYPE) {
// LOG.info("Received message with retain=" + frame.retain());
// if (frame.retain()) {
// retain[0]++;
// } else {
// nonretain[0]++;
// }
// }
// }
// });
// 
// BlockingConnection connection = mqtt.blockingConnection();
// connection.connect();
// final String RETAINED = "RETAINED";
// connection.publish("one", RETAINED.getBytes(), QoS.AT_LEAST_ONCE, true);
// connection.publish("two", RETAINED.getBytes(), QoS.AT_LEAST_ONCE, true);
// 
// final String NONRETAINED = "NONRETAINED";
// connection.publish("one", NONRETAINED.getBytes(), QoS.AT_LEAST_ONCE, false);
// connection.publish("two", NONRETAINED.getBytes(), QoS.AT_LEAST_ONCE, false);
// 
// connection.subscribe(new Topic[]{new Topic("#", QoS.AT_LEAST_ONCE)});
// for (int i = 0; i < 4; i++) {
// final Message message = connection.receive(30, TimeUnit.SECONDS);
// assertNotNull("Should receive 4 messages", message);
// message.ack();
// }
// assertEquals("Should receive 2 retained messages", 2, retain[0]);
// assertEquals("Should receive 2 non-retained messages", 2, nonretain[0]);
// }
// TODO As with other tests, this should be enabled as part of the cross protocol support with MQTT.
// @Test(timeout = 60 * 1000)
// public void testSendMQTTReceiveJMSVirtualTopic() throws Exception {
// 
// final MQTTClientProvider provider = getMQTTClientProvider();
// initializeConnection(provider);
// final String DESTINATION_NAME = "Consumer.jms.VirtualTopic.TopicA";
// 
// // send retained message
// final String RETAINED = "RETAINED";
// final String MQTT_DESTINATION_NAME = "VirtualTopic/TopicA";
// provider.publish(MQTT_DESTINATION_NAME, RETAINED.getBytes(), AT_LEAST_ONCE, true);
// 
// ActiveMQConnection activeMQConnection = (ActiveMQConnection) new ActiveMQConnectionFactory(jmsUri).createConnection();
// // MUST set to true to receive retained messages
// activeMQConnection.setUseRetroactiveConsumer(true);
// activeMQConnection.start();
// Session s = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Queue jmsQueue = s.createQueue(DESTINATION_NAME);
// MessageConsumer consumer = s.createConsumer(jmsQueue);
// 
// // check whether we received retained message on JMS subscribe
// ActiveMQMessage message = (ActiveMQMessage) consumer.receive(5000);
// assertNotNull("Should get retained message", message);
// ByteSequence bs = message.getContent();
// assertEquals(RETAINED, new String(bs.data, bs.offset, bs.length));
// assertTrue(message.getBooleanProperty(RetainedMessageSubscriptionRecoveryPolicy.RETAINED_PROPERTY));
// 
// for (int i = 0; i < NUM_MESSAGES; i++) {
// String payload = "Test Message: " + i;
// provider.publish(MQTT_DESTINATION_NAME, payload.getBytes(), AT_LEAST_ONCE);
// message = (ActiveMQMessage) consumer.receive(5000);
// assertNotNull("Should get a message", message);
// bs = message.getContent();
// assertEquals(payload, new String(bs.data, bs.offset, bs.length));
// }
// 
// // re-create consumer and check we received retained message again
// consumer.close();
// consumer = s.createConsumer(jmsQueue);
// message = (ActiveMQMessage) consumer.receive(5000);
// assertNotNull("Should get retained message", message);
// bs = message.getContent();
// assertEquals(RETAINED, new String(bs.data, bs.offset, bs.length));
// assertTrue(message.getBooleanProperty(RetainedMessageSubscriptionRecoveryPolicy.RETAINED_PROPERTY));
// 
// activeMQConnection.close();
// provider.disconnect();
// }
@Test(timeout = 60 * 1000)
public void testPingOnMQTT() throws Exception {
    stopBroker();
    protocolConfig = "maxInactivityDuration=-1";
    startBroker();
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("test-mqtt");
    mqtt.setKeepAlive((short) 2);
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 23 with BlockingConnection

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

the class MQTTTest method testDuplicateClientId.

@Test(timeout = 60 * 1000)
public void testDuplicateClientId() throws Exception {
    final String clientId = "duplicateClient";
    MQTT mqtt = createMQTTConnection(clientId, false);
    mqtt.setKeepAlive((short) 2);
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String TOPICA = "TopicA";
    connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
    MQTT mqtt1 = createMQTTConnection(clientId, false);
    mqtt1.setKeepAlive((short) 2);
    final BlockingConnection connection1 = mqtt1.blockingConnection();
    connection1.connect();
    assertTrue("Duplicate client disconnected", Wait.waitFor(() -> connection1.isConnected()));
    assertTrue("Old client still connected", Wait.waitFor(() -> !connection.isConnected()));
    connection1.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
    connection1.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 24 with BlockingConnection

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

the class MQTTTest method testPacketIdGeneratorNonCleanSession.

@Test(timeout = 90 * 1000)
public void testPacketIdGeneratorNonCleanSession() throws Exception {
    final MQTT mqtt = createMQTTConnection("nonclean-packetid", false);
    mqtt.setKeepAlive((short) 15);
    final Map<Short, PUBLISH> publishMap = new ConcurrentHashMap<>();
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            LOG.info("Client received:\n" + frame);
            if (frame.messageType() == PUBLISH.TYPE) {
                PUBLISH publish = new PUBLISH();
                try {
                    publish.decode(frame);
                    LOG.info("PUBLISH " + publish);
                } catch (ProtocolException e) {
                    fail("Error decoding publish " + e.getMessage());
                }
                if (publishMap.get(publish.messageId()) != null) {
                    assertTrue(publish.dup());
                }
                publishMap.put(publish.messageId(), publish);
            }
        }

        @Override
        public void onSend(MQTTFrame frame) {
            LOG.info("Client sent:\n" + frame);
        }
    });
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String TOPIC = "TopicA/";
    connection.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
    // publish non-retained messages
    final int TOTAL_MESSAGES = 10;
    for (int i = 0; i < TOTAL_MESSAGES; i++) {
        connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    }
    // receive half the messages in this session
    for (int i = 0; i < TOTAL_MESSAGES / 2; i++) {
        final Message msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        assertNotNull(msg);
        assertEquals(TOPIC, new String(msg.getPayload()));
        msg.ack();
    }
    connection.disconnect();
    // resume session
    connection = mqtt.blockingConnection();
    connection.connect();
    // receive rest of the messages
    Message msg = null;
    do {
        msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        if (msg != null) {
            assertEquals(TOPIC, new String(msg.getPayload()));
            msg.ack();
        }
    } while (msg != null);
    // make sure we received all message ids
    for (short id = 1; id <= TOTAL_MESSAGES; id++) {
        assertNotNull("No message for id " + id, publishMap.get(id));
    }
    connection.unsubscribe(new String[] { TOPIC });
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) PUBLISH(org.fusesource.mqtt.codec.PUBLISH) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 25 with BlockingConnection

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

the class MQTTTest method testValidZeroLengthClientId.

@Test(timeout = 30 * 1000)
public void testValidZeroLengthClientId() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("");
    mqtt.setCleanSession(true);
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) 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