トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

MQTT

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
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
56
57
58
59
60
61
62
// 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
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
56
57
58
59
60
61
62
// 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);
    }
  }
}
[カテゴリ: ネットワーク]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2019年11月11日 22時43分26秒