简体   繁体   English

当订阅者在Mqtt主题Java中处于非活动状态时,如何获取生产者发送的所有数据(未保留或最后一条消息)

[英]How to get all the data sent by producer when subscriber is inactive in Mqtt topic Java(not retained or last message)

I've been looking for all over internet. 我一直在寻找整个互联网。 I have tried setting cleansession "false" and qos 1 and 2 yet subscriber is not getting all the content when he comes online. 我尝试将cleansession设置为“ false”,并且设置了qos 1和2,但订阅者上线时未获得所有内容。 Please help... my code is 请帮忙...我的代码是

Example.java(Producer) Example.java(生产者)

public class Example extends PersonBean {
public  void hey(){
String clientId = MqttClient.generateClientId();
 MemoryPersistence persistence = new MemoryPersistence();

    PersonBean pb=new PersonBean();
    for(int i=1;i<=5;i++){
        Gson gson = new Gson();

        Date dt=new Date();
         DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
         String currentTime = df.format(dt);
    pb.setId(i);
    pb.setName("sai");
    pb.setEmail("s@g.com");
    pb.setAddress("hyderabad");
    pb.setCreatedOn(currentTime);


    String jsonInString = gson.toJson(pb);


        try {
            String broker = "tcp://localhost:1883";
            String topicName = "test/mqtt";
            int qos = 2;

    MqttClient mqttClient = new MqttClient(broker,clientId);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(false);
            mqttClient.connect(connOpts);

            MqttMessage message = new MqttMessage(jsonInString.getBytes());

            message.setQos(qos);
    message.setRetained(true);


            MqttTopic topic2 = mqttClient.getTopic(topicName);
topic2.publish(message);

        mqttClient.disconnect();
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode() + " - msg "
                    + me.getMessage() + "- loc " + me.getLocalizedMessage()
                    + " - cause " + me.getCause() + "- exception " + me);

        }


    }}

    public static void main(String[] args) {
    Example ex=new Example();
    ex.hey();
}}

and my 和我的

Subscriber.java Subscriber.java

public class SubcriberExample implements MqttCallback{

MqttClient client;
public void doDemo() {
    try {
        client = new MqttClient("tcp://192.168.4.189:1883", "Sending");
        client.connect();
        client.setCallback(this);
        client.subscribe("test/mqtt");



    } catch (MqttException e) {
        e.printStackTrace();
    }
}

public static void main(String args[]){
    SubcriberExample se=new SubcriberExample();
    se.doDemo();
}

@Override
public void connectionLost(Throwable arg0) {
    // TODO Auto-generated method stub
    System.out.println("connection lost....");
}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
    // TODO Auto-generated method stub

}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("message is : "+message);
}}

You're on the right track, the published messages are required to be QoS1/2 to be eligible for being held in an offline queue (for an offline subscriber). 您处在正确的轨道上,发布的消息必须为QoS1 / 2,才有资格保留在脱机队列中(对于脱机订户)。

However, from the code above it seems the problem is in the subscriber. 但是,从上面的代码看来,问题出在订阅者中。 For an MQTT subscriber to be able to receive offline messages, then it needs to have a persistent session. 为了使MQTT订阅者能够接收脱机消息,则它需要具有持久会话。 Ie the subscriber needs to connect with clean session = false. 即订户需要使用干净会话= false连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM