/
PooledMqttClientFactory.java
159 lines (140 loc) · 5.85 KB
/
PooledMqttClientFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
* Copyright (c) 2016, 2020 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.transport.mqtt.pooling;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.eclipse.kapua.transport.mqtt.MqttClient;
import org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions;
import org.eclipse.kapua.transport.mqtt.exception.MqttClientException;
import org.eclipse.kapua.transport.mqtt.exception.MqttClientTerminateException;
import org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting;
import org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSettingKeys;
import org.eclipse.kapua.transport.mqtt.setting.MqttClientSetting;
import org.eclipse.kapua.transport.mqtt.setting.MqttClientSettingKeys;
import org.eclipse.kapua.transport.utils.ClientIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
/**
* Pooled object factory for {@link MqttClientPool}.
*
* @since 1.0.0
*/
public class PooledMqttClientFactory extends BasePooledObjectFactory<MqttClient> {
private static final Logger LOG = LoggerFactory.getLogger(PooledMqttClientFactory.class);
private static final ClientIdGenerator CLIENT_ID_GENERATOR = ClientIdGenerator.getInstance();
private final String serverURI;
public PooledMqttClientFactory(String serverURI) {
this.serverURI = serverURI;
}
/**
* Creates the {@link MqttClient} for the {@link MqttClientPool}.
*
* <p>
* The client is initialized and connected. In case of any failure on connect operation, an exception is thrown and the the created client is destroyed.
* </p>
*
* @throws Exception FIXME [javadoc] document exception.
* @since 1.0.0
*/
@Override
public MqttClient create() throws Exception {
//
// User pwd generation
MqttClientSetting mqttClientSettings = MqttClientSetting.getInstance();
MqttClientPoolSetting mqttClientPoolSettings = MqttClientPoolSetting.getInstance();
String username = mqttClientSettings.getString(MqttClientSettingKeys.TRANSPORT_CREDENTIAL_USERNAME);
char[] password = mqttClientSettings.getString(MqttClientSettingKeys.TRANSPORT_CREDENTIAL_PASSWORD).toCharArray();
String clientId = CLIENT_ID_GENERATOR.next(mqttClientPoolSettings.getString(MqttClientPoolSettingKeys.CLIENT_POOL_CLIENT_ID_PREFIX));
//
// Get new client and connection options
MqttClientConnectionOptions connectionOptions = new MqttClientConnectionOptions();
connectionOptions.setClientId(clientId);
connectionOptions.setUsername(username);
connectionOptions.setPassword(password);
connectionOptions.setEndpointURI(URI.create(serverURI));
//
// Connect client
MqttClient kapuaClient = new MqttClient();
try {
kapuaClient.connectClient(connectionOptions);
} catch (MqttClientException mce) {
try {
kapuaClient.terminateClient();
} catch (MqttClientTerminateException mcte) {
LOG.error("Unable to properly terminate MQTT client after failed connect attempt: {}", clientId, mcte);
}
throw mce;
}
return kapuaClient;
}
/**
* Wraps the given {@link MqttClient} into a {@link DefaultPooledObject}.
*
* @param mqttClient The object to wrap for {@link BasePooledObjectFactory}.
* @since 1.0.0
*/
@Override
public PooledObject<MqttClient> wrap(MqttClient mqttClient) {
return new DefaultPooledObject<>(mqttClient);
}
/**
* Validates status of the given {@link MqttClient} pooled object.
*
* <p>
* Check performed for the client to be marked as valid are:
* </p>
* <ul>
* <li>{@link MqttClient} {@code != null}</li>
* <li>{@link MqttClient#isConnected()} {@code == true}</li>
* </ul>
*
* @param pooledMqttClient The object to validate.
* @since 1.0.0
*/
@Override
public boolean validateObject(PooledObject<MqttClient> pooledMqttClient) {
if (pooledMqttClient == null) {
return false;
}
MqttClient mqttClient = pooledMqttClient.getObject();
return (mqttClient != null && mqttClient.isConnected());
}
/**
* Destroys the given {@link MqttClient} pooled object.
* <p>
* Before calling super implementation {@link BasePooledObjectFactory#destroyObject(PooledObject)} it tries to clean up the {@link MqttClient}.
* </p>
*
* @param pooledMqttClient The pooled object to destroy.
* @since 1.0.0.
*/
@Override
public void destroyObject(PooledObject<MqttClient> pooledMqttClient) throws Exception {
if (pooledMqttClient == null) {
return;
}
MqttClient mqttClient = pooledMqttClient.getObject();
try {
if (mqttClient != null) {
if (mqttClient.isConnected()) {
mqttClient.disconnectClient();
}
mqttClient.terminateClient();
}
} catch (MqttClientException mce) {
LOG.warn("Error while cleaning MqttClient {} before destroying it... Removing it from the pool anyway!", mqttClient.getClientId(), mce);
}
super.destroyObject(pooledMqttClient);
}
}