public class SimpleMQTTClient {
public static void main(String[] args) {
String broker = tcp://localhost:1883;
String clientId = System.currentTimeMillis() + -client;
String topic = test/topic;
try {
MqttClient sampleClient = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
// Connect to the broker
sampleClient.connect(connOpts);
System.out.println(Connected);
// Publish a message
MqttMessage message = new MqttMessage(Hello World.getBytes());
message.setQos(1); // Quality of Service
sampleClient.publish(topic, message);
// Subscribe to a topic
sampleClient.subscribe(topic, 1); // QoS level
// Wait for messages
sampleClient.setCallback(new SampleMessageListener());