-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleConsumer.java
55 lines (37 loc) · 1.65 KB
/
SimpleConsumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.iot.coap.demo;
//import com.iot.demo.mqtt.sensors.EngineTemperatureSensor;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class SimpleConsumer {
private final static Logger logger = LoggerFactory.getLogger(SimpleConsumer.class);
//private static String SERVER_URI = "tcp://iot.eclipse.org:1883";
private static String SERVER_URI = "tcp://127.0.0.1:1883";
public static void main(String [ ] args) {
logger.info("Consumer started ...");
try{
String publisherId = UUID.randomUUID().toString();
IMqttClient subscriber = new MqttClient(SERVER_URI,publisherId);
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
subscriber.connect(options);
logger.info("Connected !");
CountDownLatch receivedSignal = new CountDownLatch(10);
subscriber.subscribe(CoapObservingClientProcess.TOPIC2, (topic, msg) -> {
byte[] payload = msg.getPayload();
logger.info("New Message Received: {}", new String(payload));
receivedSignal.countDown();
});
receivedSignal.await(1, TimeUnit.MINUTES);
}catch (Exception e){
e.printStackTrace();
}
}
}