mqttclient使用问题

使用的jar包为:org.eclipse.paho.client.mqttv3-1.0.2.jar

报错如下:

 [akka:/ server/user/$a] 正在进行过多的发布
正在进行过多的发布 (32202)
    at org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:436)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:121)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:139)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:858)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:836)
    at com.hp.es.business.messageservice.MqttActor.onReceive(MqttActor.java:76)
    at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:167)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:467)
    at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:97)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
    at akka.actor.ActorCell.invoke(ActorCell.scala:487)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
    at akka.dispatch.Mailbox.run(Mailbox.scala:220)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

查看了源码,分析原因分析入下:

发布消息的时候qos=1,需要等待broker会ack,由于客户端发送量太大,未能及时抽到broker回复的ack(这一点可能因为网络有延时),但是客户端维持的一个变量—maxinflight,默认值为10,如果以qos=1发送的消息,maxinflight就会+1,当抽到broker回复的ack时,maxinflight-1,所以当客户端发送量太大时,又未能及时收到broker回复的ack,导致达到了maxinflght的值,出现上述问题。

鄙人能想到的临时解决方法如下:
(1) 改qos=0
(2)调整maxinflight值的上限
(3)自己做流量控制
三种方法都各自有着优缺点,需根据不同情况自己确定。

你可能感兴趣的:(mqtt)