|
| 1 | +/* |
| 2 | + * Copyright 2022-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.mqtt.core; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.concurrent.ScheduledFuture; |
| 21 | +import java.util.concurrent.atomic.AtomicReference; |
| 22 | + |
| 23 | +import org.eclipse.paho.client.mqttv3.IMqttAsyncClient; |
| 24 | +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; |
| 25 | +import org.eclipse.paho.client.mqttv3.MqttAsyncClient; |
| 26 | +import org.eclipse.paho.client.mqttv3.MqttCallback; |
| 27 | +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| 28 | +import org.eclipse.paho.client.mqttv3.MqttException; |
| 29 | +import org.eclipse.paho.client.mqttv3.MqttMessage; |
| 30 | + |
| 31 | +import org.springframework.integration.context.IntegrationObjectSupport; |
| 32 | +import org.springframework.integration.support.management.ManageableLifecycle; |
| 33 | + |
| 34 | +public class Mqttv3ClientManager extends IntegrationObjectSupport implements ClientManager<IMqttAsyncClient>, |
| 35 | + ManageableLifecycle, MqttCallback { |
| 36 | + |
| 37 | + private AtomicReference<ScheduledFuture<?>> scheduledReconnect; |
| 38 | + |
| 39 | + private final MqttConnectOptions connectOptions; |
| 40 | + |
| 41 | + private final String clientId; |
| 42 | + |
| 43 | + private IMqttAsyncClient client; |
| 44 | + |
| 45 | + public Mqttv3ClientManager(MqttConnectOptions connectOptions, String clientId) throws MqttException { |
| 46 | + this.connectOptions = connectOptions; |
| 47 | + this.client = new MqttAsyncClient(connectOptions.getServerURIs()[0], clientId); |
| 48 | + this.client.setCallback(this); |
| 49 | + this.clientId = clientId; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public IMqttAsyncClient getClient() { |
| 54 | + return client; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void start() { |
| 59 | + if (this.client == null) { |
| 60 | + try { |
| 61 | + this.client = new MqttAsyncClient(this.connectOptions.getServerURIs()[0], this.clientId); |
| 62 | + } |
| 63 | + catch (MqttException e) { |
| 64 | + throw new IllegalStateException("could not start client manager", e); |
| 65 | + } |
| 66 | + this.client.setCallback(this); |
| 67 | + } |
| 68 | + try { |
| 69 | + connect(); |
| 70 | + } |
| 71 | + catch (MqttException e) { |
| 72 | + logger.error(e, "could not start client manager, scheduling reconnect, client_id=" + |
| 73 | + this.client.getClientId()); |
| 74 | + scheduleReconnect(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void stop() { |
| 80 | + if (this.client == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + try { |
| 84 | + this.client.disconnectForcibly(this.connectOptions.getConnectionTimeout()); |
| 85 | + } |
| 86 | + catch (MqttException e) { |
| 87 | + logger.error(e, "could not disconnect from the client"); |
| 88 | + } |
| 89 | + finally { |
| 90 | + try { |
| 91 | + this.client.close(); |
| 92 | + } |
| 93 | + catch (MqttException e) { |
| 94 | + logger.error(e, "could not close the client"); |
| 95 | + } |
| 96 | + this.client = null; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean isRunning() { |
| 102 | + return this.client != null; |
| 103 | + } |
| 104 | + |
| 105 | + private synchronized void connect() throws MqttException { |
| 106 | + if (this.client == null) { |
| 107 | + logger.error("could not connect on a null client reference"); |
| 108 | + return; |
| 109 | + } |
| 110 | + MqttConnectOptions options = Mqttv3ClientManager.this.connectOptions; |
| 111 | + this.client.connect(options).waitForCompletion(options.getConnectionTimeout()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public synchronized void connectionLost(Throwable cause) { |
| 116 | + logger.error(cause, "connection lost, scheduling reconnect, client_id=" + this.client.getClientId()); |
| 117 | + scheduleReconnect(); |
| 118 | + } |
| 119 | + |
| 120 | + private void scheduleReconnect() { |
| 121 | + if (this.scheduledReconnect.get() != null) { |
| 122 | + this.scheduledReconnect.get().cancel(false); |
| 123 | + } |
| 124 | + this.scheduledReconnect.set(getTaskScheduler().schedule(() -> { |
| 125 | + try { |
| 126 | + connect(); |
| 127 | + this.scheduledReconnect.set(null); |
| 128 | + } |
| 129 | + catch (MqttException e) { |
| 130 | + logger.error(e, "could not reconnect"); |
| 131 | + scheduleReconnect(); |
| 132 | + } |
| 133 | + }, Instant.now().plusSeconds(10))); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void messageArrived(String topic, MqttMessage message) { |
| 138 | + // not this manager concern |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void deliveryComplete(IMqttDeliveryToken token) { |
| 143 | + // nor this manager concern |
| 144 | + } |
| 145 | +} |
0 commit comments