How to use sango - Java

I will explain how to use sango from Java.

Follow the procedure below.

  1. Get Java library
  2. Implement client

Confirmed environment

  • Java 1.8 SE
  • Mqtt-client 0.4.0

1. Get Java library

Pahoのリリースディレクトリ to download the mqtt-client-0.4.0.jar from.

Alternatively, describe pom.xml as follows.

<project>
<repositories>
    <repository>
        <id>Eclipse Paho Repo</id>
        <url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>mqtt-client</artifactId>
        <version>0.4.0</version>
    </dependency>
</dependencies>
</project>

2. Client implementation

Connect

The Connect part is common to both Pub / Sub.

In Java, a class that implements the MqttCallback interface and message arrival setCallback specified in. This time, MQTT class implemented MqttCallback, but it is also possible to implement it in another class.

The MqttCallback interface must implement the following three methods.

  • ConnectionLost
  • DeliveryComplete
  • MessageArrived

In other languages, ClientId is randomly set in some languages, but in the Java language library you need to explicitly specify ClientId. In the sample of this time we made it a fixed value, but I think whether it is better to actually set a random character string.

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
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.MqttPersistenceException;

public class MQTT implements MqttCallback{
    // MQTT Clientをクラスに持たせています
    MqttClient client;

    public void Connect(String hostname, int port, String userName, String password) throws MqttException{
        // 接続先をURIとして設定します
        String brokerURI = "tcp://" + hostname + ":" + port;

        // クライアントIDを設定します。
        String clientId = "test-client";

        // MQTT Clientのインスタンスを作成します。
        client = new MqttClient(brokerURI, clientId);

        // 接続設定をします
        MqttConnectOptions opts = new MqttConnectOptions();
        opts.setUserName(userName);
        opts.setPassword(password.toCharArray());

        // MqttCallbackを設定します。今回はこのクラス自身が実装しています
        client.setCallback(this);

        // Connectします
        client.connect(opts);
    }

Subscribe

subscribe calls.

// メッセージが到着するとこのCallbackメソッドが呼ばれます
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    System.out.println(message);
}

public void Subscribe(String topic, int qos) throws MqttException{
     client.subscribe(topic, qos);
}

Since you have to specify the MqttCallback at the location of the Connect at setCallback, when a message arrives in the Subscribe Topic, messageArrived method is called.

Publish

publish call the.

public void Publish(String topic, String payload, int qos) throws MqttPersistenceException, MqttException{
    MqttMessage message = new MqttMessage(payload.getBytes());
    message.setQos(qos);
    client.publish(topic, message);
}