Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
MQTT for Java
...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Dependencies
<...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Moquette Confi...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Message Interc...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Shutdown hook
...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Eclipse Paho M...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Running Exampl...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Running the Ex...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Resources
● So...
Micha Kops
http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/
Le Fin
Thanks ...
Upcoming SlideShare
Loading in …5
×

MQTT and Java - Client and Broker Examples

1,745 views

Published on

Demonstration of MQTT client and broker communication using Eclipse Paho and Moquette.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

MQTT and Java - Client and Broker Examples

  1. 1. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ MQTT for Java Client and Broker examples using Moquette and Apache Paho
  2. 2. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Dependencies <dependency> <groupId>io.moquette</groupId> <artifactId>moquette-broker</artifactId> <version>0.8</version> </dependency> <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.0.2</version> </dependency>
  3. 3. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Moquette Configuration port 1883 host 0.0.0.0 allow_anonymous true File moquette.conf
  4. 4. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Message Interceptor class PublisherListener extends AbstractInterceptHandler { @Override public void onPublish(InterceptPublishMessage message) { System.out.println("moquette mqtt broker message intercepted, topic: " + message.getTopicName() + ", content: " + new String(message.getPayload().array())); } }
  5. 5. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Shutdown hook final IConfig classPathConfig = new ClasspathConfig(); final Server mqttBroker = new Server(); final List<? extends InterceptHandler> userHandlers = Arrays.asList(new PublisherListener()); mqttBroker.startServer(classPathConfig, userHandlers); System.out.println("moquette mqtt broker started, press ctrl-c to shutdown.."); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.out.println("stopping moquette mqtt broker.."); mqttBroker.stopServer(); System.out.println("moquette mqtt broker stopped"); } });
  6. 6. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Eclipse Paho MQTT Client String topic = "news"; String content = "Visit www.hascode.com! :D"; int qos = 2; String broker = "tcp://0.0.0.0:1883"; String clientId = "paho-java-client"; try { MqttClient sampleClient = new MqttClient(broker, clientId, new MemoryPersistence()); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); System.out.println("paho-client connecting to broker: " + broker); sampleClient.connect(connOpts); System.out.println("paho-client connected to broker"); System.out.println("paho-client publishing message: " + content); MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); sampleClient.publish(topic, message); System.out.println("paho-client message published"); sampleClient.disconnect(); System.out.println("paho-client disconnected"); } catch (MqttException me) {}
  7. 7. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Running Example final IConfig classPathConfig = new ClasspathConfig(); final Server mqttBroker = new Server(); final List<? extends InterceptHandler> userHandlers = Arrays.asList(new PublisherListener()); mqttBroker.startServer(classPathConfig, userHandlers); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { mqttBroker.stopServer(); } }); String topic = "news"; String content = "Visit www.hascode.com! :D"; int qos = 2; String broker = "tcp://0.0.0.0:1883"; String clientId = "paho-java-client"; MqttClient sampleClient = new MqttClient(broker, clientId, new MemoryPersistence()); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); sampleClient.connect(connOpts); MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); sampleClient.publish(topic, message); sampleClient.disconnect();
  8. 8. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Running the Example $ mvn exec:java -Dexec.mainClass=com.hascode.tutorial.mqtt.Main [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building mqtt-java-tutorial 1.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- exec-maven-plugin:1.3.1:java (default-cli) @ mqtt-java-tutorial --- moquette mqtt broker started, press ctrl-c to shutdown.. paho-client connecting to broker: tcp://0.0.0.0:1883 paho-client connected to broker paho-client publishing message: Visit www.hascode.com! :D moquette mqtt broker message intercepted, topic: news, content: Visit www.hascode.com! :D paho-client message published paho-client disconnected ^Cstopping moquette mqtt broker.. moquette mqtt broker stopped
  9. 9. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Resources ● Sources: https://bitbucket.org/hascode/mqtt-java-tutorial ● Tutorial: http://www.hascode.com/2016/06/playing-around-wi ● MQTT Website: http://mqtt.org/ ● Eclipse Paho: https://eclipse.org/paho/clients/java/ ● Moquette: http://andsel.github.io/moquette/
  10. 10. Micha Kops http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Le Fin Thanks for your audience!

×