Java 的 MQTT 客户端

这是一个Java MQTT客户端的示例。

经纪人:example.com
主题:M5Stack

下载 jar 文件

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

出版的例子

// -----------------------------------------------------------------------
/*
	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 ("*** 終了 ***");
}

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

// -----------------------------------------------------------------------
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

编译

make

运行脚本

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

订阅的例子

// -----------------------------------------------------------------------
/*
	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

}

}
// -----------------------------------------------------------------------
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

编译

make

执行脚本 (shí suī pǐ)

export JAR=../jar
export CLASSPATH=.:$JAR/org.eclipse.paho.client.mqttv3-1.2.5.jar
java PahoSubscribe
广告
将在 10 秒后关闭
bannerAds