|
|
@@ -34,101 +34,101 @@ import org.slf4j.LoggerFactory;
|
|
|
*/
|
|
|
public class DefaultHttp2MessageClientFactory {
|
|
|
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(DefaultHttp2MessageClientFactory.class);
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(DefaultHttp2MessageClientFactory.class);
|
|
|
|
|
|
- private static ExecutorService reConnectExecutorService;
|
|
|
+ private static ExecutorService reConnectExecutorService;
|
|
|
|
|
|
/**
|
|
|
* key: bridgeId
|
|
|
* value: client
|
|
|
*/
|
|
|
- private static Map<String,MessageClient> clientMap =new ConcurrentHashMap<>();
|
|
|
+ private static Map<String,MessageClient> clientMap =new ConcurrentHashMap<>();
|
|
|
|
|
|
private static Map<String,BridgeConnectionStatusHandler> handlerMap =new ConcurrentHashMap<>();
|
|
|
|
|
|
- public static MessageClient initClient(DefaultBridgeBootstrap bridgeBootstrap,String productKey, String deviceName, String deviceSecret, String endpoint) {
|
|
|
- return initClient(bridgeBootstrap,productKey, deviceName, deviceSecret, endpoint, false);
|
|
|
- }
|
|
|
+ public static MessageClient initClient(DefaultBridgeBootstrap bridgeBootstrap,String productKey, String deviceName, String deviceSecret, String endpoint) {
|
|
|
+ return initClient(bridgeBootstrap,productKey, deviceName, deviceSecret, endpoint, false);
|
|
|
+ }
|
|
|
|
|
|
- public static MessageClient initClient(DefaultBridgeBootstrap bridgeBootstrap,String productKey, String deviceName, String deviceSecret,
|
|
|
- String endpoint, boolean isReconnect) {
|
|
|
+ public static MessageClient initClient(DefaultBridgeBootstrap bridgeBootstrap,String productKey, String deviceName, String deviceSecret,
|
|
|
+ String endpoint, boolean isReconnect) {
|
|
|
MessageClient client = clientMap.get(bridgeBootstrap.getId());
|
|
|
if (client!= null && !isReconnect) {
|
|
|
- return client;
|
|
|
- }
|
|
|
- Profile profile = Profile.getDeviceProfile(endpoint, productKey, deviceName, deviceSecret,
|
|
|
- String.format(BridgeConfigConsts.CLIENT_ID_TEMPLATE, productKey, deviceName));
|
|
|
- if (reConnectExecutorService == null) {
|
|
|
- reConnectExecutorService = new ThreadPoolExecutor(profile.getCallbackThreadCorePoolSize(),
|
|
|
- profile.getCallbackThreadMaximumPoolSize(), 60, TimeUnit.SECONDS,
|
|
|
- new LinkedBlockingQueue<>(35000),
|
|
|
- new ThreadFactoryBuilder().setDaemon(true).setNameFormat("iot-bridge-reconnect-%d").build(),
|
|
|
- new ThreadPoolExecutor.AbortPolicy());
|
|
|
- }
|
|
|
- client = MessageClientFactory.messageClient(profile);
|
|
|
- client.connect(new MessageCallback() {
|
|
|
- @Override
|
|
|
- public Action consume(MessageToken messageToken) {
|
|
|
- Message message = messageToken.getMessage();
|
|
|
- byte[] payload = message.getPayload();
|
|
|
- String responseString = new String(payload, StandardCharsets.UTF_8);
|
|
|
- logger.warn("Unexpected message [{}] received: {}", message.getTopic(), responseString);
|
|
|
- return Action.CommitSuccess;
|
|
|
- }
|
|
|
- });
|
|
|
- client.setConnectionCallback(new ConnectionCallback() {
|
|
|
- @Override
|
|
|
- public void onConnectionLost() {
|
|
|
- logger.error("Connection to http2 gateway [{}] is lost", endpoint);
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+ Profile profile = Profile.getDeviceProfile(endpoint, productKey, deviceName, deviceSecret,
|
|
|
+ String.format(BridgeConfigConsts.CLIENT_ID_TEMPLATE, productKey, deviceName));
|
|
|
+ if (reConnectExecutorService == null) {
|
|
|
+ reConnectExecutorService = new ThreadPoolExecutor(profile.getCallbackThreadCorePoolSize(),
|
|
|
+ profile.getCallbackThreadMaximumPoolSize(), 60, TimeUnit.SECONDS,
|
|
|
+ new LinkedBlockingQueue<>(35000),
|
|
|
+ new ThreadFactoryBuilder().setDaemon(true).setNameFormat("iot-bridge-reconnect-%d").build(),
|
|
|
+ new ThreadPoolExecutor.AbortPolicy());
|
|
|
+ }
|
|
|
+ client = MessageClientFactory.messageClient(profile);
|
|
|
+ client.connect(new MessageCallback() {
|
|
|
+ @Override
|
|
|
+ public Action consume(MessageToken messageToken) {
|
|
|
+ Message message = messageToken.getMessage();
|
|
|
+ byte[] payload = message.getPayload();
|
|
|
+ String responseString = new String(payload, StandardCharsets.UTF_8);
|
|
|
+ logger.warn("Unexpected message [{}] received: {}", message.getTopic(), responseString);
|
|
|
+ return Action.CommitSuccess;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ client.setConnectionCallback(new ConnectionCallback() {
|
|
|
+ @Override
|
|
|
+ public void onConnectionLost() {
|
|
|
+ logger.error("Connection to http2 gateway [{}] is lost", endpoint);
|
|
|
BridgeConnectionStatusHandler bridgeConnectionStatusHandler = handlerMap.get(bridgeBootstrap.getId());
|
|
|
if (bridgeConnectionStatusHandler != null) {
|
|
|
- bridgeConnectionStatusHandler.onConnectionLost();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onConnected(boolean isReconnected) {
|
|
|
- String subDeviceConnectMode = ConfigFactory.getBridgeConfigManager().getSubDeviceConnectMode();
|
|
|
- logger.info("Bridge onConnected, isReconnected={}, subDeviceConnectMode={}",
|
|
|
- isReconnected, subDeviceConnectMode);
|
|
|
+ bridgeConnectionStatusHandler.onConnectionLost();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onConnected(boolean isReconnected) {
|
|
|
+ String subDeviceConnectMode = ConfigFactory.getBridgeConfigManager().getSubDeviceConnectMode();
|
|
|
+ logger.info("Bridge onConnected, isReconnected={}, subDeviceConnectMode={}",
|
|
|
+ isReconnected, subDeviceConnectMode);
|
|
|
BridgeConnectionStatusHandler bridgeConnectionStatusHandler = handlerMap.get(bridgeBootstrap.getId());
|
|
|
- if (bridgeConnectionStatusHandler != null) {
|
|
|
- bridgeConnectionStatusHandler.onConnected(isReconnected);
|
|
|
- }
|
|
|
- if (isReconnected) {
|
|
|
- if (!BridgeConfigConsts.SUBDEVICE_CONNECT_MODE_VALUE_3.equals(subDeviceConnectMode)) {
|
|
|
- reConnectAllSubDevices();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ if (bridgeConnectionStatusHandler != null) {
|
|
|
+ bridgeConnectionStatusHandler.onConnected(isReconnected);
|
|
|
+ }
|
|
|
+ if (isReconnected) {
|
|
|
+ if (!BridgeConfigConsts.SUBDEVICE_CONNECT_MODE_VALUE_3.equals(subDeviceConnectMode)) {
|
|
|
+ reConnectAllSubDevices();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
clientMap.put(bridgeBootstrap.getId(),client);
|
|
|
- return client;
|
|
|
- }
|
|
|
-
|
|
|
- private static void reConnectAllSubDevices() {
|
|
|
- Collection<Session> sessions = SessionManagerFactory.getInstance().getSessions();
|
|
|
- if (sessions.isEmpty()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- UplinkChannelHandler handler = new UplinkChannelHandler();
|
|
|
- sessions.forEach(session -> {
|
|
|
- try {
|
|
|
- reConnectExecutorService.submit(() -> {
|
|
|
- logger.info("Re-online device {}", session.getOriginalIdentity());
|
|
|
- try {
|
|
|
- handler.doOnline(session.getOriginalIdentity());
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("Unexpected exception occurred while Re-online device:" + session.getOriginalIdentity(), e);
|
|
|
- }
|
|
|
- });
|
|
|
- } catch (RejectedExecutionException e) {
|
|
|
- logger.error("Re-online task rejected, device:{}", session.getOriginalIdentity());
|
|
|
- } catch (Throwable e) {
|
|
|
- logger.error("Re-online unExpected exception, device:" + session.getOriginalIdentity(), e);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void reConnectAllSubDevices() {
|
|
|
+ Collection<Session> sessions = SessionManagerFactory.getInstance().getSessions();
|
|
|
+ if (sessions.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ UplinkChannelHandler handler = new UplinkChannelHandler();
|
|
|
+ sessions.forEach(session -> {
|
|
|
+ try {
|
|
|
+ reConnectExecutorService.submit(() -> {
|
|
|
+ logger.info("Re-online device {}", session.getOriginalIdentity());
|
|
|
+ try {
|
|
|
+ handler.doOnline(session.getOriginalIdentity());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("Unexpected exception occurred while Re-online device:" + session.getOriginalIdentity(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (RejectedExecutionException e) {
|
|
|
+ logger.error("Re-online task rejected, device:{}", session.getOriginalIdentity());
|
|
|
+ } catch (Throwable e) {
|
|
|
+ logger.error("Re-online unExpected exception, device:" + session.getOriginalIdentity(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
public static MessageClient getClient(String id) {
|
|
|
return clientMap.get(id);
|
|
|
@@ -136,32 +136,32 @@ public class DefaultHttp2MessageClientFactory {
|
|
|
|
|
|
|
|
|
public static MessageClient getClient(DefaultBridgeBootstrap bridgeBootstrap) {
|
|
|
- return clientMap.get(bridgeBootstrap.getId());
|
|
|
- }
|
|
|
+ return clientMap.get(bridgeBootstrap.getId());
|
|
|
+ }
|
|
|
|
|
|
- public static void setClient(DefaultBridgeBootstrap bridgeBootstrap,MessageClient c) {
|
|
|
- clientMap.put(bridgeBootstrap.getId(),c);
|
|
|
- }
|
|
|
+ public static void setClient(DefaultBridgeBootstrap bridgeBootstrap,MessageClient c) {
|
|
|
+ clientMap.put(bridgeBootstrap.getId(),c);
|
|
|
+ }
|
|
|
|
|
|
- public static void setReConnectExecutorService(ExecutorService executorService) {
|
|
|
- reConnectExecutorService = executorService;
|
|
|
- }
|
|
|
+ public static void setReConnectExecutorService(ExecutorService executorService) {
|
|
|
+ reConnectExecutorService = executorService;
|
|
|
+ }
|
|
|
|
|
|
- public static void setBridgeConnectionStatusHandler(DefaultBridgeBootstrap bridgeBootstrap,BridgeConnectionStatusHandler handler) {
|
|
|
- handlerMap.merge(bridgeBootstrap.getId(),handler,(o,n)->n);
|
|
|
- }
|
|
|
+ public static void setBridgeConnectionStatusHandler(DefaultBridgeBootstrap bridgeBootstrap,BridgeConnectionStatusHandler handler) {
|
|
|
+ handlerMap.merge(bridgeBootstrap.getId(),handler,(o,n)->n);
|
|
|
+ }
|
|
|
|
|
|
- public static void disconnect(DefaultBridgeBootstrap bridgeBootstrap) {
|
|
|
+ public static void disconnect(DefaultBridgeBootstrap bridgeBootstrap) {
|
|
|
MessageClient client = clientMap.get(bridgeBootstrap.getId());
|
|
|
if (client != null) {
|
|
|
- client.disconnect();
|
|
|
- client = null;
|
|
|
- }
|
|
|
- }
|
|
|
+ client.disconnect();
|
|
|
+ client = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public static boolean isConnected(DefaultBridgeBootstrap bridgeBootstrap) {
|
|
|
+ public static boolean isConnected(DefaultBridgeBootstrap bridgeBootstrap) {
|
|
|
MessageClient client = clientMap.get(bridgeBootstrap.getId());
|
|
|
- return client != null && client.isConnected();
|
|
|
- }
|
|
|
+ return client != null && client.isConnected();
|
|
|
+ }
|
|
|
|
|
|
}
|