Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
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 ...
Prochain SlideShare
Chargement dans…5
×

MQTT and Java - Client and Broker Examples

3 532 vues

Publié le

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

Publié dans : Technologie
  • I am getting below error while running mvn exec:java command Let me know if you have any specific steps for end-to-end demo execution.:- [WARNING] java.lang.ClassNotFoundException: Main at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:436) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:270) at java.base/java.lang.Thread.run(Thread.java:835) [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.714 s [INFO] Finished at: 2019-05-10T12:52:05+05:30 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project mqtt-java-tutorial: An exception occured while executing the Java class. Main -&gt; [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
       Répondre 
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici

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!

×