https://qiita.com/hilucky/items/42492e7ef5706b718bbb
http://wizmy.net/blog/archives/871
https://qiita.com/shohei1913/items/b355ad7d1bb27141176b
API DOC
https://www.eclipse.org/paho/files/javadoc/index.html
MqttPublisher.java
1 |
// javac -cp org.eclipse.paho.client.mqttv3-1.2.2.jar MqttPublisher.java // java -cp org.eclipse.paho.client.mqttv3-1.2.2.jar:. MqttPublisher import org.eclipse.paho.client.mqttv3.MqttClient; 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; // import org.slf4j.Logger; // import org.slf4j.LoggerFactory; // import commons.ConfigManager; public class MqttPublisher { // private final static Logger L = LoggerFactory.getLogger(MqttPublisher.class); public static void main(String[] args) { //Publish設定 // final String broker = ConfigManager.getBrokerConfig(); final String broker = "tcp://localhost:1883"; // final String topic = String.valueOf(ConfigManager.getPubSubConfig("publish", "topic")); final String topic = "top/second/third"; // final int qos = (int) ConfigManager.getPubSubConfig("publish", "qos"); final int qos = 2; // final String clientId = String.valueOf(ConfigManager.getPubSubConfig("publish", "clientId")); final String clientId = "Publisher01"; //Publishするメッセージ内容 String content = args[0]; try { MqttClient mqttClient = new MqttClient(broker, clientId, new MemoryPersistence()); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(false); // L.info("Connecting to broker: {}", broker); System.out.println("Connecting to broker: " + broker); mqttClient.connect(connOpts); // L.info("Connected and publishing message: qos -> {}, message -> {}", qos, content); System.out.println("Connected and publishing message: qos -> " + qos + ", message -> " + content); MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); mqttClient.publish(topic, message); // L.info("Message published and Disconneting broker"); System.out.println("Message published and Disconneting broker"); mqttClient.disconnect(); // L.info("Disconnected"); System.out.println("Disconnected"); System.exit(0); } catch(MqttException me) { me.printStackTrace(); // L.error("reason: {} ", me.getReasonCode()); // L.error("message: {} ", me.getMessage()); // L.error("localize: {}", me.getLocalizedMessage()); // L.error("cause: {} ", me.getCause()); // L.error("exception: {}", me); } } } |
MqttPublisher.java
1 |
// javac -cp org.eclipse.paho.client.mqttv3-1.2.2.jar MqttPublisher.java // java -cp org.eclipse.paho.client.mqttv3-1.2.2.jar:. MqttPublisher import org.eclipse.paho.client.mqttv3.MqttClient; 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; // import org.slf4j.Logger; // import org.slf4j.LoggerFactory; // import commons.ConfigManager; public class MqttPublisher { // private final static Logger L = LoggerFactory.getLogger(MqttPublisher.class); public static void main(String[] args) { //Publish設定 // final String broker = ConfigManager.getBrokerConfig(); final String broker = "tcp://localhost:1883"; // final String topic = String.valueOf(ConfigManager.getPubSubConfig("publish", "topic")); final String topic = "top/second/third"; // final int qos = (int) ConfigManager.getPubSubConfig("publish", "qos"); final int qos = 2; // final String clientId = String.valueOf(ConfigManager.getPubSubConfig("publish", "clientId")); final String clientId = "Publisher01"; //Publishするメッセージ内容 String content = args[0]; try { MqttClient mqttClient = new MqttClient(broker, clientId, new MemoryPersistence()); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(false); // L.info("Connecting to broker: {}", broker); System.out.println("Connecting to broker: " + broker); mqttClient.connect(connOpts); // L.info("Connected and publishing message: qos -> {}, message -> {}", qos, content); System.out.println("Connected and publishing message: qos -> " + qos + ", message -> " + content); MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); mqttClient.publish(topic, message); // L.info("Message published and Disconneting broker"); System.out.println("Message published and Disconneting broker"); mqttClient.disconnect(); // L.info("Disconnected"); System.out.println("Disconnected"); System.exit(0); } catch(MqttException me) { me.printStackTrace(); // L.error("reason: {} ", me.getReasonCode()); // L.error("message: {} ", me.getMessage()); // L.error("localize: {}", me.getLocalizedMessage()); // L.error("cause: {} ", me.getCause()); // L.error("exception: {}", me); } } } |
[通知用URL]
Tweet
最終更新時間:2019年11月11日 22時43分26秒