简体   繁体   中英

Java mqtt concurrent connections is taking time

I am trying to make the concurrent session to MQTT server and all clients will disconnect once all the connections are made.

In the below publisher code I am trying to make concurrents sessions each sending 50 messages. And like this 500 threads will create and each will send 50 messages. But for creating 100 connections it is taking 10 minutes. Is there any mistake in coding and is it possible to decrease the rate of connection speed changing in below code, because the same thing I have written in Golang the rate of connection is high there.

Below is the publisher code:

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;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
import org.eclipse.paho.client.mqttv3.MqttTopic;

public class Publisher extends Thread{

    public static final String test_topic = "test";
    private MqttClient client;
    public static final String BROKER_URL = "tcp://192.168.1.129:1883";
    CountDownLatch latch;

    public Publisher(CountDownLatch latch) {
      super();
      this.latch = latch;
     }

    public void Publisher() {
        String clientid=Thread.currentThread().getName();
        System.out.println("=========== "+clientid);
        MqttConnectOptions options = null;
        try {

             client = new MqttClient(BROKER_URL, clientid);
             options = new MqttConnectOptions();
            options.setCleanSession(true);
            options.setMaxInflight(50);
           client.connect(options);
        } catch (MqttException e) {
            try {
                client.connect(options);
            } catch (MqttSecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (MqttException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            e.printStackTrace();
            //System.exit(1);
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Publisher();
        System.out.println(Thread.currentThread().getName());
        try {
            for(int i=0;i<50;i++)
            {
            //Thread.sleep(20);
            publishTemperature();
            }


        } catch (MqttPersistenceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } /*catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }
    public void publishTemperature() throws MqttPersistenceException, MqttException {
        final MqttTopic test = client.getTopic(test_topic);
         final String temperature=""{\"test\":\"test\"}"";
         test.publish(new MqttMessage(temperature.getBytes()));

         //System.out.println("Published data. Topic: " + "test" + "  Message: " + temperature);

    }

    public MqttClient getClient() {
        return client;
    }

    public void setClient(MqttClient client) {
        this.client = client;
    }
}

Below is the main method:

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

    public class test {
        static Publisher[] Publisher=null;

        public static void main(String[] args) throws MqttPersistenceException, MqttException, InterruptedException {
            final CountDownLatch latch = new CountDownLatch(50);
            Publisher = new Publisher[500];
            for(int i=0;i<500;i++)
            {
                Thread.sleep(10);
                Publisher[i]=new Publisher(latch);
                Publisher[i].start();
            }
            latch.await();
            for(int i=0;i<500;i++)
            {
                //Thread.sleep(10);
                Publisher[i].getClient().disconnectForcibly(25);


            }

        }

    }

Here all connections will connect and make persistent connection up to reaching 500 connections. After that, all connections will disconnect once.

Remove the following line:

Publisher[i].join();

The docs for Thread.join() say the following:

public final void join() throws InterruptedException

Waits for this thread to die.

An invocation of this method behaves in exactly the same way as the invocation

 join(0) 

This means that every time round the loop it will stop and wait for that thread to complete it's task before creating the new one.

If you remove that call it will allow all the threads to run in parallel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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