Code Monkey home page Code Monkey logo

Comments (3)

qzhuyan avatar qzhuyan commented on July 4, 2024

Yes.

from emqx.

LvChenhui avatar LvChenhui commented on July 4, 2024

Hi
我们使用消息重传有一些疑惑,麻烦帮忙一起看下

使用的是java版本的client,mqtt v3.1.1。

当我们的客户端(这个客户端不是首次连接)初始化connect后,延迟30s再suscribe一个topic,会收不到消息,控台上看到卡在飞行窗口中,并且队列里是有积压数据的。

我们注意到客户端刚connect后会直接把消息推送过来,但是此时我们的listener还没有subscribe,所以这部分消息时丢弃的。我们不确定这部分消息是不是触发mqtt的重传机制?

from emqx.

fhyw avatar fhyw commented on July 4, 2024

代码逻辑如下,client在连接的时候,延迟注册listener

public static void main(String[] args) throws Exception {
MqttSubscribe subscribe = new MqttSubscribe();

    subscribe.connect("emqx_test_subscribe_10");

    new Thread(() -> {
        try {
           //延迟 20s
            Thread.sleep(10 * 1000);
            // 订阅
            MqttSubscription mqttSubscription = new MqttSubscription("testtopic10/10/#", 1);
            System.out.println(simpleDateFormat.format(System.currentTimeMillis()) + "subscribe");
            subscribe.client.subscribe(new MqttSubscription[]{mqttSubscription}, new IMqttMessageListener[]{new DefaultIMqttMessageListener()});
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }).start();
}

void connect(String clientId) throws Exception {
    MemoryPersistence persistence = new MemoryPersistence();

    String broker = "xxxxx";
    client = new MqttClient(broker, clientId, persistence);
    // MQTT 连接选项
    MqttConnectionOptions connOpts = new MqttConnectionOptions();
    connOpts.setUserName("test");
    connOpts.setPassword("test".getBytes());
    // 保留会话
    connOpts.setCleanStart(false);
    connOpts.setSessionExpiryInterval(7200L);
    //自动重连
    connOpts.setAutomaticReconnect(true);

    // 建立连接
    System.out.println("Connecting to broker: " + broker);
    client.connect(connOpts);
}

from emqx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.