Java mqtt client

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

Java mqtt client
[Java] Server Client Communication 1 (Unfinished)
Java
Which Java HTTP client is better?
[Java EE] Implement Client with WebSocket
Java High Level REST Client Tips
Java HTTP Client API timeout setting
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
java beginner 4
API integration from Java with Jersey Client
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
[Java] Inheritance
FastScanner Java
java beginner 3
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics
Java methods
Java inheritance
[Java] enum
[Java] FizzBuzzCounter
Studying Java ―― 8