MQTT Implementation of MQTT Example of publish/subscriber in java

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

create Dynamic web project in sts/eclipse download the eclipse paho jar from click here to download and paste jar file in webcontent->webinf->folder->lib

Publish Example

 String broker = "tcp://localhost:1883"; 
String topicName = "test/topic";
int qos = 1;

 MqttClient mqttClient = new MqttClient(broker,String.valueOf(System.nanoTime()));
//Mqtt ConnectOptions is used to set the additional features to mqtt message

  MqttConnectOptions connOpts = new MqttConnectOptions();

    connOpts.setCleanSession(true); //no persistent session 
    connOpts.setKeepAliveInterval(1000);


MqttMessage message = new MqttMessage("Ed Sheeran".getBytes());

//here ed sheeran is a message

    message.setQos(qos);     //sets qos level 1
    message.setRetained(true); //sets retained message 

MqttTopic topic2 = mqttClient.getTopic(topicName);

    mqttClient.connect(connOpts); //connects the broker with connect options
    topic2.publish(message);    // publishes the message to the topic(test/topic)

Subscribe Example

//We're using eclipse paho library  so we've to go with MqttCallback 
 MqttClient client = new MqttClient("tcp://localhost:1883","clientid");
     client.setCallback(this);
MqttConnectOptions mqOptions=new MqttConnectOptions();
     mqOptions.setCleanSession(true);
     client.connect(mqOptions);      //connecting to broker 
        client.subscribe("test/topic"); //subscribing to the topic name  test/topic

//Override methods from MqttCallback interface
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("message is : "+message);
    }
.
.//other override methods 
.


Got any MQTT Question?