|
| 1 | +package com.baeldung.mqtt; |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.UUID; |
| 8 | +import java.util.concurrent.Callable; |
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.Executors; |
| 11 | +import java.util.concurrent.ScheduledExecutorService; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +import org.eclipse.paho.client.mqttv3.MqttClient; |
| 15 | +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| 16 | +import org.junit.Test; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +public class EngineTemperatureSensorLiveTest { |
| 21 | + |
| 22 | + private static Logger log = LoggerFactory.getLogger(EngineTemperatureSensorLiveTest.class); |
| 23 | + |
| 24 | + @Test |
| 25 | + public void whenSendSingleMessage_thenSuccess() throws Exception { |
| 26 | + |
| 27 | + String publisherId = UUID.randomUUID().toString(); |
| 28 | + MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId); |
| 29 | + |
| 30 | + String subscriberId = UUID.randomUUID().toString(); |
| 31 | + MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId); |
| 32 | + |
| 33 | + MqttConnectOptions options = new MqttConnectOptions(); |
| 34 | + options.setAutomaticReconnect(true); |
| 35 | + options.setCleanSession(true); |
| 36 | + options.setConnectionTimeout(10); |
| 37 | + |
| 38 | + |
| 39 | + subscriber.connect(options); |
| 40 | + publisher.connect(options); |
| 41 | + |
| 42 | + CountDownLatch receivedSignal = new CountDownLatch(1); |
| 43 | + |
| 44 | + subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> { |
| 45 | + byte[] payload = msg.getPayload(); |
| 46 | + log.info("[I46] Message received: topic={}, payload={}", topic, new String(payload)); |
| 47 | + receivedSignal.countDown(); |
| 48 | + }); |
| 49 | + |
| 50 | + |
| 51 | + Callable<Void> target = new EngineTemperatureSensor(publisher); |
| 52 | + target.call(); |
| 53 | + |
| 54 | + receivedSignal.await(1, TimeUnit.MINUTES); |
| 55 | + |
| 56 | + log.info("[I56] Success !"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void whenSendMultipleMessages_thenSuccess() throws Exception { |
| 61 | + |
| 62 | + String publisherId = UUID.randomUUID().toString(); |
| 63 | + MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId); |
| 64 | + |
| 65 | + String subscriberId = UUID.randomUUID().toString(); |
| 66 | + MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId); |
| 67 | + |
| 68 | + |
| 69 | + MqttConnectOptions options = new MqttConnectOptions(); |
| 70 | + options.setAutomaticReconnect(true); |
| 71 | + options.setCleanSession(true); |
| 72 | + options.setConnectionTimeout(10); |
| 73 | + |
| 74 | + |
| 75 | + publisher.connect(options); |
| 76 | + subscriber.connect(options); |
| 77 | + |
| 78 | + CountDownLatch receivedSignal = new CountDownLatch(10); |
| 79 | + |
| 80 | + subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> { |
| 81 | + byte[] payload = msg.getPayload(); |
| 82 | + log.info("[I82] Message received: topic={}, payload={}", topic, new String(payload)); |
| 83 | + receivedSignal.countDown(); |
| 84 | + }); |
| 85 | + |
| 86 | + |
| 87 | + Callable<Void> target = new EngineTemperatureSensor(publisher); |
| 88 | + |
| 89 | + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); |
| 90 | + executor.scheduleAtFixedRate(() -> { |
| 91 | + try { |
| 92 | + target.call(); |
| 93 | + } |
| 94 | + catch(Exception ex) { |
| 95 | + throw new RuntimeException(ex); |
| 96 | + } |
| 97 | + }, 1, 1, TimeUnit.SECONDS); |
| 98 | + |
| 99 | + |
| 100 | + receivedSignal.await(1, TimeUnit.MINUTES); |
| 101 | + executor.shutdown(); |
| 102 | + |
| 103 | + assertTrue(receivedSignal.getCount() == 0 , "Countdown should be zero"); |
| 104 | + |
| 105 | + log.info("[I105] Success !"); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +} |
0 commit comments