Package org.fusesource.mqtt.client

Examples of org.fusesource.mqtt.client.BlockingConnection


    public void testSendAndReceiveAtMostOnce() throws Exception {
        addMQTTConnector(brokerService);
        brokerService.start();
        MQTT mqtt = createMQTTConnection();
        mqtt.setKeepAlive(Short.MAX_VALUE);
        BlockingConnection connection = mqtt.blockingConnection();

        connection.connect();


        Topic[] topics = {new Topic(utf8("foo"), QoS.AT_MOST_ONCE)};
        connection.subscribe(topics);
        for (int i = 0; i < numberOfMessages; i++) {
            String payload = "Test Message: " + i;
            connection.publish("foo", payload.getBytes(), QoS.AT_MOST_ONCE, false);
            Message message = connection.receive();
            assertEquals(payload, new String(message.getPayload()));
        }
        connection.disconnect();
    }
View Full Code Here


    public void testSendAndReceiveAtLeastOnce() throws Exception {
        addMQTTConnector(brokerService);
        brokerService.start();
        MQTT mqtt = createMQTTConnection();
        mqtt.setKeepAlive(Short.MAX_VALUE);
        BlockingConnection connection = mqtt.blockingConnection();

        connection.connect();

        Topic[] topics = {new Topic(utf8("foo"), QoS.AT_LEAST_ONCE)};
        connection.subscribe(topics);
        for (int i = 0; i < numberOfMessages; i++) {
            String payload = "Test Message: " + i;
            connection.publish("foo", payload.getBytes(), QoS.AT_LEAST_ONCE, false);
            Message message = connection.receive();
            message.ack();
            assertEquals(payload, new String(message.getPayload()));
        }
        connection.disconnect();
    }
View Full Code Here

    @Test
    public void testSendAndReceiveExactlyOnce() throws Exception {
        addMQTTConnector(brokerService);
        brokerService.start();
        MQTT publisher = createMQTTConnection();
        BlockingConnection pubConnection = publisher.blockingConnection();

        pubConnection.connect();

        MQTT subscriber = createMQTTConnection();
        BlockingConnection subConnection = subscriber.blockingConnection();

        subConnection.connect();

        Topic[] topics = {new Topic(utf8("foo"), QoS.EXACTLY_ONCE)};
        subConnection.subscribe(topics);
        for (int i = 0; i < numberOfMessages; i++) {
            String payload = "Test Message: " + i;
            pubConnection.publish("foo", payload.getBytes(), QoS.EXACTLY_ONCE, false);
            Message message = subConnection.receive();
            message.ack();
            assertEquals(payload, new String(message.getPayload()));
        }
        subConnection.disconnect();
        pubConnection.disconnect();
    }
View Full Code Here

        }
        addMQTTConnector(brokerService);
        brokerService.start();

        MQTT publisher = createMQTTConnection();
        BlockingConnection pubConnection = publisher.blockingConnection();

        pubConnection.connect();

        MQTT subscriber = createMQTTConnection();
        BlockingConnection subConnection = subscriber.blockingConnection();

        subConnection.connect();

        Topic[] topics = {new Topic(utf8("foo"), QoS.AT_LEAST_ONCE)};
        subConnection.subscribe(topics);
        for (int i = 0; i < 10; i++) {
            pubConnection.publish("foo", payload, QoS.AT_LEAST_ONCE, false);
            Message message = subConnection.receive();
            message.ack();
            assertArrayEquals(payload, message.getPayload());
        }
        subConnection.disconnect();
        pubConnection.disconnect();
    }
View Full Code Here

    public void testSendMQTTReceiveJMS() throws Exception {
        addMQTTConnector(brokerService);
        brokerService.addConnector(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
        brokerService.start();
        MQTT mqtt = createMQTTConnection();
        BlockingConnection connection = mqtt.blockingConnection();
        final String DESTINATION_NAME = "foo";
        connection.connect();

        ActiveMQConnection activeMQConnection = (ActiveMQConnection) new ActiveMQConnectionFactory().createConnection();
        activeMQConnection.start();
        Session s = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        javax.jms.Topic jmsTopic = s.createTopic(DESTINATION_NAME);
        MessageConsumer consumer = s.createConsumer(jmsTopic);

        for (int i = 0; i < numberOfMessages; i++) {
            String payload = "Test Message: " + i;
            connection.publish("foo", payload.getBytes(), QoS.AT_LEAST_ONCE, false);
            ActiveMQMessage message = (ActiveMQMessage) consumer.receive();
            ByteSequence bs = message.getContent();
            assertEquals(payload, new String(bs.data, bs.offset, bs.length));
        }


        activeMQConnection.close();
        connection.disconnect();
    }
View Full Code Here

    public void testFromMqttToAmqp() throws Exception {
        Connection amqp = createAmqpConnection();
        Session session = amqp.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(session.createTopic("topic://FOO"));

        final BlockingConnection mqtt = createMQTTConnection().blockingConnection();
        mqtt.connect();
        byte[] payload = bytes("Hello World");
        mqtt.publish("FOO", payload, QoS.AT_LEAST_ONCE, false);
        mqtt.disconnect();

        Message msg = consumer.receive(1000 * 5);
        assertNotNull(msg);
        assertTrue(msg instanceof BytesMessage);

View Full Code Here

        addMQTTConnector("maxInactivityDuration=-1");
        brokerService.start();
        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(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                return connection.isConnected();
            }
        }));

        connection.disconnect();
    }
View Full Code Here

        brokerService.waitUntilStarted();
        MQTT mqtt = createMQTTConnection();
        mqtt.setCleanSession(true);
        mqtt.setUserName((String)null);
        mqtt.setPassword((String)null);
        final BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();

        System.out.println("Connected!");

        connection.disconnect();

    }
View Full Code Here

        addMQTTConnector();
        brokerService.start();
        MQTT mqtt = createMQTTConnection();
        mqtt.setClientId("foo");
        mqtt.setKeepAlive((short)2);
        final BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();

        assertTrue("KeepAlive didn't work properly", Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                return connection.isConnected();
            }
        }));

        connection.disconnect();
    }
View Full Code Here

        addMQTTConnector("transport.useInactivityMonitor=false");
        brokerService.start();
        MQTT mqtt = createMQTTConnection();
        mqtt.setClientId("foo3");
        mqtt.setKeepAlive((short)2);
        final BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();

        assertTrue("KeepAlive didn't work properly", Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                return connection.isConnected();
            }
        }));

        connection.disconnect();
    }
View Full Code Here

TOP

Related Classes of org.fusesource.mqtt.client.BlockingConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.