-
-
Save bgloh/051d48960633957cb27dd09f365379b7 to your computer and use it in GitHub Desktop.
MQTT Asynchronous Subscriber Client Sample based on https://www.eclipse.org/paho/clients/java/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mqtt.demo; | |
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | |
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; | |
import org.eclipse.paho.client.mqttv3.MqttCallback; | |
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | |
import org.eclipse.paho.client.mqttv3.MqttException; | |
import org.eclipse.paho.client.mqttv3.MqttMessage; | |
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; | |
public class MqttSubscribeSample implements MqttCallback { | |
public static void main(String[] args) { | |
String topic = "MQTT Examples"; | |
int qos = 2; | |
String broker = "tcp://localhost:1883"; | |
String clientId = "JavaAsyncSample"; | |
MemoryPersistence persistence = new MemoryPersistence(); | |
try { | |
MqttAsyncClient sampleClient = new MqttAsyncClient(broker, clientId, persistence); | |
MqttConnectOptions connOpts = new MqttConnectOptions(); | |
connOpts.setCleanSession(true); | |
sampleClient.setCallback(new MqttSubscribeSample()); | |
System.out.println("Connecting to broker: " + broker); | |
sampleClient.connect(connOpts); | |
System.out.println("Connected"); | |
Thread.sleep(1000); | |
sampleClient.subscribe(topic, qos); | |
System.out.println("Subscribed"); | |
} catch (Exception me) { | |
if (me instanceof MqttException) { | |
System.out.println("reason " + ((MqttException) me).getReasonCode()); | |
} | |
System.out.println("msg " + me.getMessage()); | |
System.out.println("loc " + me.getLocalizedMessage()); | |
System.out.println("cause " + me.getCause()); | |
System.out.println("excep " + me); | |
me.printStackTrace(); | |
} | |
} | |
public void connectionLost(Throwable arg0) { | |
System.err.println("connection lost"); | |
} | |
public void deliveryComplete(IMqttDeliveryToken arg0) { | |
System.err.println("delivery complete"); | |
} | |
public void messageArrived(String topic, MqttMessage message) throws Exception { | |
System.out.println("topic: " + topic); | |
System.out.println("message: " + new String(message.getPayload())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment