简体   繁体   English

如何使用Java客户端同时发布MQTT消息?

[英]how to publish MQTT messages concurrently using java client?

i am trying to publish MQTT messages concurrently using 5 java client, such that each java client publish 1000 messages on a particular topic concurrently to a MQTT broker(HIVEMQ) 我试图使用5个Java客户端同时发布MQTT消息,以便每个Java客户端同时将特定主题上的1000条消息同时发布到MQTT代理(HIVEMQ)

i have opened multiple threads, each threads create a mqtt client and connect to broker using ssl and try to publish 1000 messages concurrently,messages are being sent but all the connections are not getting successful to broker and i keep on getting exception 我打开了多个线程,每个线程创建一个mqtt客户端并使用ssl连接到代理,并尝试同时发布1000条消息,正在发送消息,但所有连接都无法成功代理,并且我一直在获取异常

Client is not connected (32104)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:199)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:1355)
    at org.eclipse.paho.client.mqttv3.MqttClient.publish(MqttClient.java:583)
    at org.eclipse.paho.client.mqttv3.MqttClient.publish(MqttClient.java:575)
    at com.test.MqttPublishSample.publishMessages(MqttPublishSample.java:122)
    at com.test.MqttPublishSample.lambda$start$0(MqttPublishSample.java:74)
    at java.base/java.lang.Thread.run(Thread.java:834)
public class MqttPublishSample {

    public static void main(String... args) throws InterruptedException {

        new MqttPublishSample().start();

    }

  public void start() throws InterruptedException {


        for(int i=0;i<5;i++){

            new Thread(()->{
                MqttClient client = null;
                try {
                    client = obtainConnection();//code to obtain connection using MqttClient
                    publishMessages(client);//code to publish message using simple for loop 

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

            }).start();
        }
    }
public MqttClient obtainConnection() throws MqttException {
        String clientId = "sslTestClient"+ThreadLocalRandom.current().nextInt(0,5);
        MqttClient client = null;
        try {
            client = new MqttClient("ssl://localhost:8883", clientId, new MemoryPersistence());
        } catch (MqttException e) {
            e.printStackTrace();
        }

        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName("user1");
        mqttConnectOptions.setPassword("pass1".toCharArray());
        try {
            mqttConnectOptions.setSocketFactory(getTruststoreFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("connecting...");
        client.connect(mqttConnectOptions);
        return client;
    }

i am expecting all clients gets successfully connected to broker and publish message without the exception 我期望所有客户端都能成功连接到代理并发布消息,无一例外

It might be that you are using the same clientID over you thread, thus, the server will disconnect duplicate. 可能是您在线程上使用了相同的clientID,因此服务器将断开重复的连接。 As you are using LocalThreadRandom, there is a chance of collision (big enough as there are only 5 choices). 当您使用LocalThreadRandom时,有可能发生冲突(因为只有5个选择,所以冲突很大)。 You might use a unique identifier provided by generateClientId() or share a method between thread that keep trace of them. 您可以使用generateClientId()提供的唯一标识符,也可以在线程之间共享一个跟踪它们的方法。

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

相关问题 如何使用Eclipse Paho使用Java MQTT客户端仅发布一条消息 - How to publish only one message using a Java MQTT client using Eclipse Paho 如何使用Eclipse Paho在Java MQTT客户端上接收时发布消息 - How to publish a message while receiving on a Java MQTT client using Eclipse Paho MQTT客户端重新用于发布/订阅 - MQTT client re-using for publish / subscribe 如何使用java在IoT中发布MQTT主题? - How do you publish to a MQTT topic in IoT using java? 如何使用Java将消息发布到EMS主题 - How to publish messages to EMS Topic using Java 使用可由Javascript订阅者使用的CometD Java客户端发布消息 - Publish messages using CometD Java client that can be consumed by Javascript subscribers 使用Java WebSocket客户端将消息发布到Apache Apollo主题 - Publish messages using Java WebSocket client to Apache Apollo topic 如何使用依赖项通过 JAVA 客户端将消息发布到 Cloud Pub/Sub? - How to publish messages to Cloud Pub/Sub via the JAVA Client using a dependency? 如何在HiveMQ客户端中接收多条消息? (MQTT) - How to receive multiple messages in HiveMQ Client? (MQTT) 使用Android在AWS IoT服务上发布MQTT消息 - Using Android To Publish MQTT Messages on AWS IoT Services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM