19
19
import java .time .Instant ;
20
20
import java .util .concurrent .ScheduledFuture ;
21
21
22
- import org .apache .commons .logging .Log ;
23
- import org .apache .commons .logging .LogFactory ;
24
22
import org .eclipse .paho .client .mqttv3 .IMqttAsyncClient ;
25
23
import org .eclipse .paho .client .mqttv3 .IMqttDeliveryToken ;
26
24
import org .eclipse .paho .client .mqttv3 .MqttCallback ;
27
25
import org .eclipse .paho .client .mqttv3 .MqttConnectOptions ;
28
26
import org .eclipse .paho .client .mqttv3 .MqttException ;
29
27
import org .eclipse .paho .client .mqttv3 .MqttMessage ;
30
28
29
+ import org .springframework .beans .factory .BeanFactory ;
30
+ import org .springframework .beans .factory .BeanFactoryAware ;
31
+ import org .springframework .beans .factory .InitializingBean ;
32
+ import org .springframework .integration .context .IntegrationContextUtils ;
31
33
import org .springframework .scheduling .TaskScheduler ;
32
34
import org .springframework .util .Assert ;
33
35
34
- public class Mqttv3ClientManager extends AbstractMqttClientManager <IMqttAsyncClient > implements MqttCallback {
36
+ public class Mqttv3ClientManager extends AbstractMqttClientManager <IMqttAsyncClient >
37
+ implements MqttCallback , InitializingBean , BeanFactoryAware {
35
38
36
39
/**
37
40
* The default reconnect timeout in millis.
38
41
*/
39
42
private static final long DEFAULT_RECOVERY_INTERVAL = 10_000 ;
40
43
41
- private final Log logger = LogFactory .getLog (this .getClass ());
42
-
43
44
private final MqttPahoClientFactory clientFactory ;
44
45
45
- private final TaskScheduler taskScheduler ;
46
+ private BeanFactory beanFactory ;
47
+
48
+ private TaskScheduler taskScheduler ;
46
49
47
50
private volatile ScheduledFuture <?> scheduledReconnect ;
48
51
49
52
private volatile IMqttAsyncClient client ;
50
53
51
54
private long recoveryInterval = DEFAULT_RECOVERY_INTERVAL ;
52
55
53
- public Mqttv3ClientManager (MqttPahoClientFactory clientFactory , TaskScheduler taskScheduler , String url ,
54
- String clientId ) {
55
-
56
- super (url , clientId );
57
- Assert .notNull (clientId , "'clientFactory' is required" );
58
- Assert .notNull (clientId , "'taskScheduler' is required" );
59
- if (url == null ) {
60
- Assert .notEmpty (clientFactory .getConnectionOptions ().getServerURIs (), "'serverURIs' must be provided in the 'MqttConnectionOptions'" );
61
- }
56
+ public Mqttv3ClientManager (MqttPahoClientFactory clientFactory , String clientId ) {
57
+ super (clientId );
58
+ Assert .notNull (clientFactory , "'clientFactory' is required" );
62
59
this .clientFactory = clientFactory ;
63
- this .taskScheduler = taskScheduler ;
60
+ String [] serverURIs = clientFactory .getConnectionOptions ().getServerURIs ();
61
+ Assert .notEmpty (serverURIs , "'serverURIs' must be provided in the 'MqttConnectionOptions'" );
62
+ setUrl (serverURIs [0 ]);
63
+ }
64
+
65
+ public Mqttv3ClientManager (String url , String clientId ) {
66
+ super (clientId );
67
+ Assert .notNull (url , "'url' is required" );
68
+ setUrl (url );
69
+ MqttConnectOptions connectOptions = new MqttConnectOptions ();
70
+ connectOptions .setServerURIs (new String []{ url });
71
+ DefaultMqttPahoClientFactory defaultFactory = new DefaultMqttPahoClientFactory ();
72
+ defaultFactory .setConnectionOptions (connectOptions );
73
+ this .clientFactory = defaultFactory ;
64
74
}
65
75
66
76
@ Override
67
77
public IMqttAsyncClient getClient () {
68
78
return this .client ;
69
79
}
70
80
81
+ @ Override
82
+ public void afterPropertiesSet () {
83
+ this .taskScheduler = IntegrationContextUtils .getTaskScheduler (this .beanFactory );
84
+ }
85
+
86
+ @ Override
87
+ public void setBeanFactory (BeanFactory beanFactory ) {
88
+ Assert .notNull (beanFactory , "'beanFactory' must not be null" );
89
+ this .beanFactory = beanFactory ;
90
+ }
91
+
71
92
@ Override
72
93
public synchronized void start () {
73
94
if (this .client == null ) {
@@ -84,7 +105,7 @@ public synchronized void start() {
84
105
connect ();
85
106
}
86
107
catch (MqttException e ) {
87
- this . logger .error ("could not start client manager, scheduling reconnect, client_id=" +
108
+ logger .error ("could not start client manager, scheduling reconnect, client_id=" +
88
109
this .client .getClientId (), e );
89
110
scheduleReconnect ();
90
111
}
@@ -99,14 +120,14 @@ public synchronized void stop() {
99
120
this .client .disconnectForcibly (this .clientFactory .getConnectionOptions ().getConnectionTimeout ());
100
121
}
101
122
catch (MqttException e ) {
102
- this . logger .error ("could not disconnect from the client" , e );
123
+ logger .error ("could not disconnect from the client" , e );
103
124
}
104
125
finally {
105
126
try {
106
127
this .client .close ();
107
128
}
108
129
catch (MqttException e ) {
109
- this . logger .error ("could not close the client" , e );
130
+ logger .error ("could not close the client" , e );
110
131
}
111
132
this .client = null ;
112
133
}
@@ -119,9 +140,9 @@ public synchronized boolean isRunning() {
119
140
120
141
@ Override
121
142
public synchronized void connectionLost (Throwable cause ) {
122
- this . logger .error ("connection lost, scheduling reconnect, client_id=" + this .client .getClientId (),
143
+ logger .error ("connection lost, scheduling reconnect, client_id=" + this .client .getClientId (),
123
144
cause );
124
- scheduleReconnect (); // todo: do we need to resubscribe if con lost?
145
+ scheduleReconnect ();
125
146
}
126
147
127
148
@ Override
@@ -161,7 +182,7 @@ private synchronized void scheduleReconnect() {
161
182
this .scheduledReconnect = null ;
162
183
}
163
184
catch (MqttException e ) {
164
- this . logger .error ("could not reconnect" , e );
185
+ logger .error ("could not reconnect" , e );
165
186
scheduleReconnect ();
166
187
}
167
188
}, Instant .now ().plusMillis (getRecoveryInterval ()));
0 commit comments