An example of a Java mqtt client. I checked the operation on Arch Linux.
Publish example
MqttPublishSample.java
// -----------------------------------------------------------------------
/*
MqttPublishSample.java
Dec/04/2017
*/
// -----------------------------------------------------------------------
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 = "topic_1";
String content = "Hello 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://localhost:1883";
String broker = "tcp://192.168.8.100:1883";
String clientId = "JavaSample";
MemoryPersistence persistence = new MemoryPersistence();
System.err.println ("***start***");
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 ("***End***");
}
// -----------------------------------------------------------------------
}
// -----------------------------------------------------------------------
compile
javac -cp mqtt-client-0.4.0.jar MqttPublishSample.java
Run
export CLASSPATH=.:mqtt-client-0.4.0.jar
java MqttPublishSample
Subscribing example
PahoSubscribe.java
// -----------------------------------------------------------------------
/*
PahoSubscribe.java
Dec/04/2017
*/
// -----------------------------------------------------------------------
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 ("***start***");
new PahoSubscribe().doDemo();
}
// -----------------------------------------------------------------------
public void doDemo() {
try {
String broker = "tcp://192.168.8.100:1883";
String topic = "topic_1";
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
}
}
// -----------------------------------------------------------------------
compile
javac -cp mqtt-client-0.4.0.jar PahoSubscribe.java
Run
export CLASSPATH=.:mqtt-client-0.4.0.jar
java PahoSubscribe
Recommended Posts