main.mqtt.setHost(shift(argl)); } else if ("-k".equals(arg)) { main.mqtt.setKeepAlive(Short.parseShort(shift(argl))); } else if ("-c".equals(arg)) { main.mqtt.setCleanSession(false);
main.mqtt.setHost(shift(argl)); } else if ("-k".equals(arg)) { main.mqtt.setKeepAlive(Short.parseShort(shift(argl))); } else if ("-c".equals(arg)) { main.mqtt.setCleanSession(false);
@Override public void setKeepAlive(int keepAlive) throws Exception { mqtt.setKeepAlive((short) keepAlive); } }
@Test(timeout = 60 * 1000) public void testRepeatedLinkStealing() throws Exception { final String clientId = "duplicateClient"; final AtomicReference<BlockingConnection> oldConnection = new AtomicReference<>(); final String TOPICA = "TopicA"; for (int i = 1; i <= 10; ++i) { LOG.info("Creating MQTT Connection {}", i); MQTT mqtt = createMQTTConnection(clientId, false); mqtt.setKeepAlive((short) 2); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true); assertTrue("Client connect failed for attempt: " + i, Wait.waitFor(() -> connection.isConnected(), 3000, 200)); if (oldConnection.get() != null) { assertTrue("Old client still connected on attempt: " + i, Wait.waitFor(() -> !oldConnection.get().isConnected(), 3000, 200)); } oldConnection.set(connection); } oldConnection.get().publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true); oldConnection.get().disconnect(); }
mqtt.setKeepAlive((short) 2); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect();
@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(); }
@Test(timeout = 60 * 1000) public void testClientDisconnectedOnMaxConsumerLimitReached() throws Exception { Exception peerDisconnectedException = null; try { String clientId = "test.client"; SimpleString coreAddress = new SimpleString("foo.bar"); Topic[] mqttSubscription = new Topic[]{new Topic("foo/bar", QoS.AT_LEAST_ONCE)}; getServer().createQueue(coreAddress, RoutingType.MULTICAST, new SimpleString(clientId + "." + coreAddress), null, false, true, 0, false, true); MQTT mqtt = createMQTTConnection(); mqtt.setClientId(clientId); mqtt.setKeepAlive((short) 2); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); connection.subscribe(mqttSubscription); } catch (EOFException e) { peerDisconnectedException = e; } assertNotNull(peerDisconnectedException); assertTrue(peerDisconnectedException.getMessage().contains("Peer disconnected")); }
@Test(timeout = 60 * 1000) public void testPingKeepsInactivityMonitorAlive() throws Exception { 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(() -> connection.isConnected())); connection.disconnect(); }
@Test(timeout = 30 * 1000) public void testDefaultKeepAliveWhenClientSpecifiesZero() throws Exception { stopBroker(); protocolConfig = "transport.defaultKeepAlive=2000"; startBroker(); MQTT mqtt = createMQTTConnection(); mqtt.setClientId("foo"); mqtt.setKeepAlive((short) 0); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected())); }
@Test(timeout = 60 * 1000) public void testTurnOffInactivityMonitor() throws Exception { stopBroker(); protocolConfig = "transport.useInactivityMonitor=false"; startBroker(); 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(() -> connection.isConnected())); connection.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(); }
MQTT mqtt = createMQTTConnection(); mqtt.setClientId("foo"); mqtt.setKeepAlive((short) 20);
final String clientId = "publishDollar"; mqtt.setClientId(clientId); mqtt.setKeepAlive((short) 2); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); mqtt.setKeepAlive((short) 2); connection = mqtt.blockingConnection(); connection.connect();
mqtt.setKeepAlive((short) 2);
client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax()); client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort()); client.setKeepAlive(mqttClientConfig.getKeepAliveInterval()); if (mqttClientConfig.getPassword() != null) { client.setPassword(mqttClientConfig.getPassword());
@Test(timeout = 60 * 1000) public void testClientConnectionFailure() throws Exception { MQTT mqtt = createMQTTConnection("reconnect", false); mqtt.setKeepAlive((short) 1); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Wait.waitFor(() -> connection.isConnected()); final String TOPIC = "TopicA"; final byte[] qos = connection.subscribe(new Topic[]{new Topic(TOPIC, QoS.EXACTLY_ONCE)}); assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]); connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false); // kill transport connection.kill(); // FIXME Wait for the previous connection to timeout. This is not required in ActiveMQ. Needs investigating. Thread.sleep(10000); final BlockingConnection newConnection = mqtt.blockingConnection(); newConnection.connect(); Wait.waitFor(() -> newConnection.isConnected()); assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]); Message msg = newConnection.receive(1000, TimeUnit.MILLISECONDS); assertNotNull(msg); assertEquals(TOPIC, new String(msg.getPayload())); msg.ack(); newConnection.disconnect(); }
@Override public void setup(OperatorContext context) { try { client = new MQTT(); if (mqttClientConfig.getClientId() != null) { client.setClientId(mqttClientConfig.getClientId()); } client.setCleanSession(mqttClientConfig.isCleanSession()); client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax()); client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort()); client.setKeepAlive(mqttClientConfig.getKeepAliveInterval()); if (mqttClientConfig.getPassword() != null) { client.setPassword(mqttClientConfig.getPassword()); } if (mqttClientConfig.getUserName() != null) { client.setUserName(mqttClientConfig.getUserName()); } if (mqttClientConfig.getWillMessage() != null) { client.setWillMessage(mqttClientConfig.getWillMessage()); client.setWillQos(mqttClientConfig.getWillQos()); client.setWillRetain(mqttClientConfig.isWillRetain()); client.setWillTopic(mqttClientConfig.getWillTopic()); } connection = client.blockingConnection(); connection.connect(); } catch (Throwable t) { throw new RuntimeException(t); } }
@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())); }
public void testRetainedMessageOnVirtualTopics() throws Exception { MQTT mqtt = createMQTTConnection(); mqtt.setKeepAlive((short) 60);
@Test(timeout = 60 * 1000) public void testWillMessageIsRetained() 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); mqtt.setWillRetain(true); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Wait.waitFor(() -> connection.isConnected()); // kill transport connection.kill(); Thread.sleep(10000); MQTT mqtt2 = createMQTTConnection("2", false); BlockingConnection connection2 = mqtt2.blockingConnection(); connection2.connect(); connection2.subscribe(new Topic[]{new Topic("will", QoS.AT_LEAST_ONCE)}); Message m = connection2.receive(1000, TimeUnit.MILLISECONDS); assertNotNull(m); m.ack(); assertEquals("test message", new String(m.getPayload())); }