How can i connect a Java mqtt client with username

2020-07-23 08:48发布

I am able to subscribe to the mosquitto broker with this Java code, without username and password. Now, i would like to subscribe to an emqttd broker which requires some dummy username and password. How can i do this?. Thanks.

http://tgrall.github.io/blog/2017/01/02/getting-started-with-mqtt/#disqus_thread

https://github.com/emqtt/emqttd

package com.mapr.demo.mqtt.simple;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;

public class Subscriber {

  public static void main(String[] args) throws MqttException {

    System.out.println("== START SUBSCRIBER ==");

    MqttClient client=new MqttClient("tcp://localhost:1883", MqttClient.generateClientId());
    client.setCallback( new SimpleMqttCallBack() );
    client.connect();

    client.subscribe("iot_data");

  }

}

2条回答
Animai°情兽
2楼-- · 2020-07-23 09:03

You could use the MqttConnectOptions:

 public class Subscriber {

    private static final String CONNECTION_URL = "tcp://localhost:1883";
    private static final String SUBSCRIPTION = "iot_data";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "top-secret";


    public static void main(String[] args) throws MqttException {

       System.out.println("== START SUBSCRIBER ==");

       MqttClient client = new MqttClient(CONNECTION_URL, 
       MqttClient.generateClientId());

       MqttConnectOptions connOpts = setUpConnectionOptions(USERNAME, PASSWORD);
       client.connect(connOpts);

       client.subscribe(SUBSCRIPTION);

    }

   private static MqttConnectOptions setUpConnectionOptions(String username, String password) {
       MqttConnectOptions connOpts = new MqttConnectOptions();
       connOpts.setCleanSession(true);
       connOpts.setUserName(username);
       connOpts.setPassword(password.toCharArray());
       return connOpts;
   }  

  }
查看更多
一纸荒年 Trace。
3楼-- · 2020-07-23 09:14

This is my final working code:

Without this line, client.setCallback(new SimpleMqttCallBack()); i can't print the message. Not sure why?.

package com.mapr.demo.mqtt.simple;

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;

public class Subscriber {

//    private static final String CONNECTION_URL = "tcp://localhost:1883";
    private static final String CONNECTION_URL = "tcp://192.168.1.102:1883";
    private static final String SUBSCRIPTION = "Area1/#";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "top-secret";

    public static void main(String[] args) throws MqttException {

    System.out.println("== START SUBSCRIBER ==");

    MqttClient client = new MqttClient(CONNECTION_URL, MqttClient.generateClientId());

    MqttConnectOptions connOpts = setUpConnectionOptions(USERNAME, PASSWORD);

// This callback is required to receive the message
    client.setCallback(new SimpleMqttCallBack());
    client.connect(connOpts);

    client.subscribe(SUBSCRIPTION);
    }

    public void messageArrived(String topic, MqttMessage message) throws MqttException {
    System.out.println(String.format("[%s] %s", topic, new String(message.getPayload())));
    System.out.println("\tMessage published on topic 'Area1'");
    }

    private static MqttConnectOptions setUpConnectionOptions(String username, String password) {
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setUserName(username);
    connOpts.setPassword(password.toCharArray());
    return connOpts;
    }

}
查看更多
登录 后发表回答