diff --git a/mqtt/README.md b/mqtt/README.md new file mode 100644 index 0000000000..5a388aab4c --- /dev/null +++ b/mqtt/README.md @@ -0,0 +1,4 @@ +### Relevant Articles: +================================ + +- [MQTT Client in Java](http://www.baeldung.com/mqtt-client) diff --git a/mqtt/src/test/java/com/baeldung/mqtt/EngineTemperatureSensorLiveTest.java b/mqtt/src/test/java/com/baeldung/mqtt/EngineTemperatureSensorLiveTest.java index 94031b5415..b1c0002888 100644 --- a/mqtt/src/test/java/com/baeldung/mqtt/EngineTemperatureSensorLiveTest.java +++ b/mqtt/src/test/java/com/baeldung/mqtt/EngineTemperatureSensorLiveTest.java @@ -24,12 +24,11 @@ public class EngineTemperatureSensorLiveTest { @Test public void whenSendSingleMessage_thenSuccess() throws Exception { - String senderId = UUID.randomUUID().toString(); - MqttClient sender = new MqttClient("tcp://iot.eclipse.org:1883",senderId); - - String receiverId = UUID.randomUUID().toString(); - MqttClient receiver = new MqttClient("tcp://iot.eclipse.org:1883",receiverId); + String publisherId = UUID.randomUUID().toString(); + MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId); + String subscriberId = UUID.randomUUID().toString(); + MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId); MqttConnectOptions options = new MqttConnectOptions(); options.setAutomaticReconnect(true); @@ -37,33 +36,34 @@ public class EngineTemperatureSensorLiveTest { options.setConnectionTimeout(10); - receiver.connect(options); - sender.connect(options); + subscriber.connect(options); + publisher.connect(options); CountDownLatch receivedSignal = new CountDownLatch(1); - receiver.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> { - log.info("[I41] Message received: topic={}, payload={}", topic, new String(msg.getPayload())); + subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> { + byte[] payload = msg.getPayload(); + log.info("[I46] Message received: topic={}, payload={}", topic, new String(payload)); receivedSignal.countDown(); }); - Callable target = new EngineTemperatureSensor(sender); + Callable target = new EngineTemperatureSensor(publisher); target.call(); receivedSignal.await(1, TimeUnit.MINUTES); - log.info("[I51] Success !"); + log.info("[I56] Success !"); } @Test public void whenSendMultipleMessages_thenSuccess() throws Exception { - String senderId = UUID.randomUUID().toString(); - MqttClient sender = new MqttClient("tcp://iot.eclipse.org:1883",senderId); + String publisherId = UUID.randomUUID().toString(); + MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId); - String receiverId = UUID.randomUUID().toString(); - MqttClient receiver = new MqttClient("tcp://iot.eclipse.org:1883",receiverId); + String subscriberId = UUID.randomUUID().toString(); + MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId); MqttConnectOptions options = new MqttConnectOptions(); @@ -72,18 +72,19 @@ public class EngineTemperatureSensorLiveTest { options.setConnectionTimeout(10); - sender.connect(options); - receiver.connect(options); + publisher.connect(options); + subscriber.connect(options); CountDownLatch receivedSignal = new CountDownLatch(10); - receiver.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> { - log.info("[I41] Message received: topic={}, payload={}", topic, new String(msg.getPayload())); + subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> { + byte[] payload = msg.getPayload(); + log.info("[I82] Message received: topic={}, payload={}", topic, new String(payload)); receivedSignal.countDown(); }); - Callable target = new EngineTemperatureSensor(sender); + Callable target = new EngineTemperatureSensor(publisher); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); executor.scheduleAtFixedRate(() -> { @@ -96,12 +97,12 @@ public class EngineTemperatureSensorLiveTest { }, 1, 1, TimeUnit.SECONDS); - receivedSignal.await(1, TimeUnit.DAYS); + receivedSignal.await(1, TimeUnit.MINUTES); executor.shutdown(); assertTrue(receivedSignal.getCount() == 0 , "Countdown should be zero"); - log.info("[I51] Success !"); + log.info("[I105] Success !"); }