Java の mqtt クライアント

Java の mqtt クライアントの例です。
>ブローカー: example.com
>トピック: M5Stack

## jar ファイルのダウンロード

```bash
wget https://repo1.maven.org/maven2/org/eclipse/paho/org.eclipse.paho.client.mqttv3/1.2.5/org.eclipse.paho.client.mqttv3-1.2.5.jar
```

## パブリッシュの例

```java:MqttPublishSample.java
// -----------------------------------------------------------------------
/*
	MqttPublishSample.java

						Feb/10/2023
*/
// -----------------------------------------------------------------------
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  java.util.Date;
import  java.text.DateFormat;
import  java.text.SimpleDateFormat;
// -----------------------------------------------------------------------
public class MqttPublishSample
{

// -----------------------------------------------------------------------
public static void main(String[] args)
{
	String topic		= "M5Stack";
	String content	  = "こんにちは from MqttPublishSample\n";

	Date ddx = new Date ();
	DateFormat dfm = new SimpleDateFormat ("yyyy-MM-dd");
	String  str_ddx = dfm.format (ddx);

	content += str_ddx;

	int qos			 = 2;
	String broker	   = "tcp://example.com:1883";
	String clientId	 = "JavaSample";
	MemoryPersistence persistence = new MemoryPersistence();

	System.err.println ("*** 開始 ***");

	try {
		MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
		MqttConnectOptions connOpts = new MqttConnectOptions();
		connOpts.setCleanSession(true);
		System.out.println("Connecting to broker: "+broker);
		sampleClient.connect(connOpts);
		System.out.println("Connected");
		System.out.println("Publishing message: "+content);
		MqttMessage message = new MqttMessage(content.getBytes());
		message.setQos(qos);
		sampleClient.publish(topic, message);
		System.out.println("Message published");
		sampleClient.disconnect();
		System.out.println("Disconnected");
//	  System.exit(0);
		} catch(MqttException me) {
		System.out.println("reason "+me.getReasonCode());
		System.out.println("msg "+me.getMessage());
		System.out.println("loc "+me.getLocalizedMessage());
		System.out.println("cause "+me.getCause());
		System.out.println("excep "+me);
		me.printStackTrace();
		}

	System.err.println ("*** 終了 ***");
}

// -----------------------------------------------------------------------
}

// -----------------------------------------------------------------------
```

```text:Makefile
export JAR=../jar
MqttPublishSample.class: MqttPublishSample.java
	javac -cp $(JAR)/org.eclipse.paho.client.mqttv3-1.2.5.jar MqttPublishSample.java
clean:
	rm -f *.class
```

コンパイル

```bash
make
```

実行スクリプト

```bash
export JAR=../jar
export CLASSPATH=.:$JAR/org.eclipse.paho.client.mqttv3-1.2.5.jar
java MqttPublishSample
```


## サブスクライブの例

```java:PahoSubscribe.java
// -----------------------------------------------------------------------
/*
	PahoSubscribe.java

						Feb/10/2023
*/
// -----------------------------------------------------------------------
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.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

// -----------------------------------------------------------------------
public class PahoSubscribe implements MqttCallback {

MqttClient client;

// -----------------------------------------------------------------------
public PahoSubscribe() {
}

// -----------------------------------------------------------------------
public static void main(String[] args)
{
	System.err.println ("*** 開始 ***");
	new PahoSubscribe().doDemo();
}

// -----------------------------------------------------------------------
public void doDemo() {
    try {
	String broker = "tcp://example.com:1883";
	String topic = "M5Stack";
	client = new MqttClient(broker, "Sending");
	client.connect();
	client.setCallback(this);
	client.subscribe(topic);
	} catch (MqttException ee) {
		ee.printStackTrace();
		System.err.println ("*** error ***");
	}
}

// -----------------------------------------------------------------------
@Override
public void connectionLost(Throwable cause) {
    // TODO Auto-generated method stub

}

@Override
public void messageArrived(String topic, MqttMessage message)
        throws Exception {
	System.out.println(message);   
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
    // TODO Auto-generated method stub

}

}
// -----------------------------------------------------------------------
```

```text:Makefile
export JAR=../jar
PahoSubscribe.class: PahoSubscribe.java
	javac -cp $(JAR)/org.eclipse.paho.client.mqttv3-1.2.5.jar PahoSubscribe.java
clean:
	rm -f *.class
```

コンパイル

```bash
make
```

実行スクリプト

```bash
export JAR=../jar
export CLASSPATH=.:$JAR/org.eclipse.paho.client.mqttv3-1.2.5.jar
java PahoSubscribe
```