异步并发地使用Mqttclient处理多个消息传递

时间:2017-12-10 14:50:30

标签: java multithreading mqtt paho

我正在开发一个消耗来自MQTT主题的消息的程序,我的目标是我可以异步使用和处理多个消息。

我正在使用eclipse客户端: window manager https://www.eclipse.org/paho/files/javadoc/org/eclipse/paho/client/mqttv3/MqttClient.html

问题是几条消息不会同时处理,它们都在同一个线程中执行。我不太清楚使用MqttClient和MqttAsyncClient之间的区别。 javadoc说:

MqttClient

  

轻量级客户端,用于使用方法与MQTT服务器通信   阻止直到操作完成。

MqttAsyncClient

  

轻量级客户端,用于使用非阻塞与MQTT服务器通信   允许操作在后台运行的方法。

我都不清楚使用“subscribe”或“setCallback”方法之间的区别。只有使用“subscribe”才能声明几个Listener: setCallback

  

设置一个回调侦听器,用于异步发生的事件。   订阅   订阅主题......

它试图同时发送十条消息。我的测试如下:

public class FooListener implements IMqttMessageListener {
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("Thread [ " + Thread.currentThread().getName() + 
                "], Topic[ "+ topic + "],  Message [" + message +"] ");
    }
}

public class FooCallbackListener implements MqttCallback {

    @Override
    public void connectionLost(Throwable e) {
        e.printStackTrace();
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        //TODO:emtpy
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("Thread [ " + Thread.currentThread().getName() + 
                "], Topic[ "+ topic + "],  Message [" + message +"] ");
    }

}

MqttClient并订阅:

public class FooMqttClient {

    public static void main(String[] args) {
        MqttConnectOptions connOpt = new MqttConnectOptions();
        connOpt.setCleanSession(true);
        connOpt.setKeepAliveInterval(30);
        String serverUri = "tcp://iot.eclipse.org:1883";
        String clientId = UUID.randomUUID().toString();

        try {
            MqttClient myClient = new MqttClient(serverUri, clientId);
            myClient.connect(connOpt);
            myClient.subscribe("topic/foo", new FooListener());
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

结果:

Thread [ MQTT Call: 7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[topic/foo],  Message [Foo 0]  
Thread [ MQTT Call:7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 1]  
Thread [ MQTT Call: 7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 2]  
Thread [ MQTT Call:7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 3]  
Thread [ MQTT Call: 7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 4]  
Thread [ MQTT Call:7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 5]  
Thread [ MQTT Call: 7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 6]  
Thread [ MQTT Call:7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 7]  
Thread [ MQTT Call: 7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 8]  
Thread [ MQTT Call:7bb52a01-03db-4870-8921-8d6432f2fe27], Topic[ topic/foo],  Message [Foo 9]

MqttClient和setCallback:

public class FooMqttCallbackClient {

    public static void main(String[] args) {
        MqttConnectOptions connOpt = new MqttConnectOptions();
        connOpt.setCleanSession(true);
        connOpt.setKeepAliveInterval(30);
        String serverUri = "tcp://iot.eclipse.org:1883";
        String clientId = UUID.randomUUID().toString();

        try {
            MqttClient myClient = new MqttClient(serverUri, clientId);
            myClient.connect(connOpt);
            myClient.subscribe("topic/foo");
            myClient.setCallback(new FooCallbackListener());
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

结果:

Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 0]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 1]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 2]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 3]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 4]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 5]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 6]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 7]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 8]  
Thread [ MQTT Call: 3d571fd4-906d-4a1f-b06f-a2f7d43b95b2], Topic[ topic/foo],  Message [Foo 9]

MqttAsyncClient和subscribe:

public class FooAsyncMqttClient {
    public static void main(String[] args) {
        MqttConnectOptions connOpt = new MqttConnectOptions();
        connOpt.setCleanSession(true);
        connOpt.setKeepAliveInterval(30);
        String serverUri = "tcp://iot.eclipse.org:1883";
        String clientId = UUID.randomUUID().toString();

        try {
            MqttAsyncClient myClient = new MqttAsyncClient(serverUri, clientId);
            myClient.connect(connOpt);
            Thread.sleep(1000);
            myClient.subscribe("topic/foo", 1, new FooListener());
        } catch (MqttException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

结果:

Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 0]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 1]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 2]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 3]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 4]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 5]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 6]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 7]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 8]  
Thread [ MQTT Call: bbc21897-3c38-4f96-8dea-74522250afee], Topic[ topic/foo],  Message [Foo 9]

MqttAsyncClient和setCallback

public class FooAsyncMqttCallbackClient {

    public static void main(String[] args) {
        MqttConnectOptions connOpt = new MqttConnectOptions();
        connOpt.setCleanSession(true);
        connOpt.setKeepAliveInterval(30);
        String serverUri = "tcp://iot.eclipse.org:1883";
        String clientId = UUID.randomUUID().toString();

        try {
            MqttAsyncClient myClient = new MqttAsyncClient(serverUri, clientId);
            myClient.connect(connOpt);
            Thread.sleep(1000);
            myClient.subscribe("topic/foo", 1);
            myClient.setCallback(new FooCallbackListener());
        } catch (MqttException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

结果:

Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 0]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 1]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 2]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 3]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 4]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 5]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 6]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 7]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 8]  
Thread [ MQTT Call: 75b827c9-34fe-4e2f-a723-cf0f277b91d6], Topic[ topic/foo],  Message [Foo 9]

在我的所有测试中,侦听器在同一个线程中执行而不是同时执行。如何同时处理消息? MqttClient和MqttAsyncClient有什么区别?

解决方案:

public class FooExecutorListener implements IMqttMessageListener {

    private ExecutorService pool = Executors.newFixedThreadPool(10);

    class MessageHandler implements Runnable {
        MqttMessage message;
        String topic;

        public MessageHandler(String topic, MqttMessage message) {
            this.message = message;
            this.topic = topic;
        }

        public void run() {
            System.out.println("Thread [ " + Thread.currentThread().getName() + 
                    "], Topic[ "+ topic + "],  Message [" + message +"] ");
        }
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        pool.execute(new MessageHandler(topic, message));
    }

}

结果:

Thread [ pool-2-thread-1], Topic[ topic/foo],  Message [Foo 0] 
Thread [ pool-2-thread-2], Topic[ topic/foo],  Message [Foo 1] 
Thread [ pool-2-thread-5], Topic[ topic/foo],  Message [Foo 4] 
Thread [ pool-2-thread-4], Topic[ topic/foo],  Message [Foo 3] 
Thread [ pool-2-thread-7], Topic[ topic/foo],  Message [Foo 6] 
Thread [ pool-2-thread-6], Topic[ topic/foo],  Message [Foo 5] 
Thread [ pool-2-thread-8], Topic[ topic/foo],  Message [Foo 7] 
Thread [ pool-2-thread-3], Topic[ topic/foo],  Message [Foo 2] 
Thread [ pool-2-thread-1], Topic[ topic/foo],  Message [Foo 10] 
Thread [ pool-2-thread-2], Topic[ topic/foo],  Message [Foo 11] 
Thread [ pool-2-thread-5], Topic[ topic/foo],  Message [Foo 12] 
Thread [ pool-2-thread-5], Topic[ topic/foo],  Message [Foo 13] 
Thread [ pool-2-thread-5], Topic[ topic/foo],  Message [Foo 14] 
Thread [ pool-2-thread-9], Topic[ topic/foo],  Message [Foo 8] 
Thread [ pool-2-thread-10], Topic[ topic/foo],  Message [Foo 9] 

1 个答案:

答案 0 :(得分:2)

2版客户端之间的区别在于连接/发布未订阅。 Async版本将无阻塞地连接和发布。

两种情况下的订阅处理均在后台网络上处理。

如果要并行处理传入消息,则需要实现自己的线程池并将传入消息分发到池中。

最简单的方法是使用Java的ExecutorService课程。 e.g。

public class FooListener implements IMqttMessageListener {

  ExecutorService pool = new ExecutorService(10);

  class MessageHandler implements Runnable {
    MqttMessage message;
    String topic;

    public MessageHandler(String topic; MqttMessage message) {
      this.message = message;
    }

    public void run() {
      //process message
    }
  }

  @Override
  public void messageArrived(String topic, MqttMessage message) throws Exception {
    pool.execute(new MessageHandler(topic,message));
  }
}