29
29
import org .eclipse .paho .client .mqttv3 .MqttMessage ;
30
30
31
31
import org .springframework .context .ApplicationEventPublisher ;
32
- import org .springframework .context .ApplicationEventPublisherAware ;
33
32
import org .springframework .integration .IntegrationMessageHeaderAccessor ;
34
33
import org .springframework .integration .acks .SimpleAcknowledgment ;
35
34
import org .springframework .integration .mqtt .core .ConsumerStopAction ;
38
37
import org .springframework .integration .mqtt .core .MqttPahoComponent ;
39
38
import org .springframework .integration .mqtt .event .MqttConnectionFailedEvent ;
40
39
import org .springframework .integration .mqtt .event .MqttSubscribedEvent ;
40
+ import org .springframework .integration .mqtt .support .DefaultPahoMessageConverter ;
41
41
import org .springframework .integration .mqtt .support .MqttUtils ;
42
42
import org .springframework .integration .support .AbstractIntegrationMessageBuilder ;
43
43
import org .springframework .messaging .Message ;
60
60
*
61
61
*/
62
62
public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDrivenChannelAdapter
63
- implements MqttCallback , MqttPahoComponent , ApplicationEventPublisherAware {
64
-
65
- /**
66
- * The default completion timeout in milliseconds.
67
- */
68
- public static final long DEFAULT_COMPLETION_TIMEOUT = 30_000L ;
63
+ implements MqttCallback , MqttPahoComponent {
69
64
70
65
/**
71
66
* The default disconnect completion timeout in milliseconds.
@@ -78,14 +73,8 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
78
73
79
74
private int recoveryInterval = DEFAULT_RECOVERY_INTERVAL ;
80
75
81
- private long completionTimeout = DEFAULT_COMPLETION_TIMEOUT ;
82
-
83
76
private long disconnectCompletionTimeout = DISCONNECT_COMPLETION_TIMEOUT ;
84
77
85
- private boolean manualAcks ;
86
-
87
- private ApplicationEventPublisher applicationEventPublisher ;
88
-
89
78
private volatile IMqttClient client ;
90
79
91
80
private volatile ScheduledFuture <?> reconnectFuture ;
@@ -139,16 +128,6 @@ public MqttPahoMessageDrivenChannelAdapter(String url, String clientId, String..
139
128
this (url , clientId , new DefaultMqttPahoClientFactory (), topic );
140
129
}
141
130
142
- /**
143
- * Set the completion timeout for operations. Not settable using the namespace.
144
- * Default {@value #DEFAULT_COMPLETION_TIMEOUT} milliseconds.
145
- * @param completionTimeout The timeout.
146
- * @since 4.1
147
- */
148
- public synchronized void setCompletionTimeout (long completionTimeout ) {
149
- this .completionTimeout = completionTimeout ;
150
- }
151
-
152
131
/**
153
132
* Set the completion timeout when disconnecting. Not settable using the namespace.
154
133
* Default {@value #DISCONNECT_COMPLETION_TIMEOUT} milliseconds.
@@ -169,23 +148,6 @@ public synchronized void setRecoveryInterval(int recoveryInterval) {
169
148
this .recoveryInterval = recoveryInterval ;
170
149
}
171
150
172
- /**
173
- * Set the acknowledgment mode to manual.
174
- * @param manualAcks true for manual acks.
175
- * @since 5.3
176
- */
177
- public void setManualAcks (boolean manualAcks ) {
178
- this .manualAcks = manualAcks ;
179
- }
180
-
181
- /**
182
- * @since 4.2.2
183
- */
184
- @ Override
185
- public void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher ) {
186
- this .applicationEventPublisher = applicationEventPublisher ; // NOSONAR (inconsistent synchronization)
187
- }
188
-
189
151
@ Override
190
152
public MqttConnectOptions getConnectionInfo () {
191
153
MqttConnectOptions options = this .clientFactory .getConnectionOptions ();
@@ -199,6 +161,17 @@ public MqttConnectOptions getConnectionInfo() {
199
161
return options ;
200
162
}
201
163
164
+ @ Override
165
+ protected void onInit () {
166
+ super .onInit ();
167
+ if (getConverter () == null ) {
168
+ DefaultPahoMessageConverter pahoMessageConverter = new DefaultPahoMessageConverter ();
169
+ pahoMessageConverter .setBeanFactory (getBeanFactory ());
170
+ setConverter (pahoMessageConverter );
171
+
172
+ }
173
+ }
174
+
202
175
@ Override
203
176
protected void doStart () {
204
177
Assert .state (getTaskScheduler () != null , "A 'taskScheduler' is required" );
@@ -293,22 +266,26 @@ private synchronized void connectAndSubscribe() throws MqttException {
293
266
this .client = this .clientFactory .getClientInstance (getUrl (), getClientId ());
294
267
this .client .setCallback (this );
295
268
if (this .client instanceof MqttClient ) {
296
- ((MqttClient ) this .client ).setTimeToWait (this . completionTimeout );
269
+ ((MqttClient ) this .client ).setTimeToWait (getCompletionTimeout () );
297
270
}
298
271
299
272
this .topicLock .lock ();
300
273
String [] topics = getTopic ();
274
+ ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher ();
301
275
try {
302
276
this .client .connect (connectionOptions );
303
- this .client .setManualAcks (this .manualAcks );
304
- int [] requestedQos = getQos ();
305
- int [] grantedQos = Arrays .copyOf (requestedQos , requestedQos .length );
306
- this .client .subscribe (topics , grantedQos );
307
- warnInvalidQosForSubscription (topics , requestedQos , grantedQos );
277
+ this .client .setManualAcks (isManualAcks ());
278
+ if (topics .length > 0 ) {
279
+ int [] requestedQos = getQos ();
280
+ int [] grantedQos = Arrays .copyOf (requestedQos , requestedQos .length );
281
+ this .client .subscribe (topics , grantedQos );
282
+ warnInvalidQosForSubscription (topics , requestedQos , grantedQos );
283
+ }
308
284
}
309
285
catch (MqttException ex ) {
310
- if (this .applicationEventPublisher != null ) {
311
- this .applicationEventPublisher .publishEvent (new MqttConnectionFailedEvent (this , ex ));
286
+
287
+ if (applicationEventPublisher != null ) {
288
+ applicationEventPublisher .publishEvent (new MqttConnectionFailedEvent (this , ex ));
312
289
}
313
290
logger .error (ex , () -> "Error connecting or subscribing to " + Arrays .toString (topics ));
314
291
if (this .client != null ) { // Could be reset during event handling before
@@ -331,8 +308,8 @@ private synchronized void connectAndSubscribe() throws MqttException {
331
308
this .connected = true ;
332
309
String message = "Connected and subscribed to " + Arrays .toString (topics );
333
310
logger .debug (message );
334
- if (this . applicationEventPublisher != null ) {
335
- this . applicationEventPublisher .publishEvent (new MqttSubscribedEvent (this , message ));
311
+ if (applicationEventPublisher != null ) {
312
+ applicationEventPublisher .publishEvent (new MqttSubscribedEvent (this , message ));
336
313
}
337
314
}
338
315
}
@@ -397,8 +374,9 @@ public synchronized void connectionLost(Throwable cause) {
397
374
}
398
375
this .client = null ;
399
376
scheduleReconnect ();
400
- if (this .applicationEventPublisher != null ) {
401
- this .applicationEventPublisher .publishEvent (new MqttConnectionFailedEvent (this , cause ));
377
+ ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher ();
378
+ if (applicationEventPublisher != null ) {
379
+ applicationEventPublisher .publishEvent (new MqttConnectionFailedEvent (this , cause ));
402
380
}
403
381
}
404
382
}
@@ -407,7 +385,7 @@ public synchronized void connectionLost(Throwable cause) {
407
385
public void messageArrived (String topic , MqttMessage mqttMessage ) {
408
386
AbstractIntegrationMessageBuilder <?> builder = toMessageBuilder (topic , mqttMessage );
409
387
if (builder != null ) {
410
- if (this . manualAcks ) {
388
+ if (isManualAcks () ) {
411
389
builder .setHeader (IntegrationMessageHeaderAccessor .ACKNOWLEDGMENT_CALLBACK ,
412
390
new AcknowledgmentImpl (mqttMessage .getId (), mqttMessage .getQos (), this .client ));
413
391
}
@@ -458,7 +436,7 @@ public void deliveryComplete(IMqttDeliveryToken token) {
458
436
}
459
437
460
438
/**
461
- * Used to complete message arrival when {@link #manualAcks } is true.
439
+ * Used to complete message arrival when {@link #isManualAcks() } is true.
462
440
*
463
441
* @since 5.3
464
442
*/
0 commit comments