|
|
|
@ -15,6 +15,9 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.service.queue; |
|
|
|
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import com.google.common.util.concurrent.ListeningExecutorService; |
|
|
|
import com.google.common.util.concurrent.MoreExecutors; |
|
|
|
import lombok.Getter; |
|
|
|
import lombok.Setter; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
@ -148,6 +151,7 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
protected volatile ExecutorService consumersExecutor; |
|
|
|
protected volatile ExecutorService usageStatsExecutor; |
|
|
|
private volatile ExecutorService firmwareStatesExecutor; |
|
|
|
private volatile ListeningExecutorService deviceActivityEventsExecutor; |
|
|
|
|
|
|
|
public DefaultTbCoreConsumerService(TbCoreQueueFactory tbCoreQueueFactory, |
|
|
|
ActorSystemContext actorContext, |
|
|
|
@ -195,6 +199,7 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
this.consumersExecutor = Executors.newCachedThreadPool(ThingsBoardThreadFactory.forName("tb-core-consumer")); |
|
|
|
this.usageStatsExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-core-usage-stats-consumer")); |
|
|
|
this.firmwareStatesExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-core-firmware-notifications-consumer")); |
|
|
|
this.deviceActivityEventsExecutor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-core-device-activity-events-consumer"))); |
|
|
|
} |
|
|
|
|
|
|
|
@PreDestroy |
|
|
|
@ -209,6 +214,9 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
if (firmwareStatesExecutor != null) { |
|
|
|
firmwareStatesExecutor.shutdownNow(); |
|
|
|
} |
|
|
|
if (deviceActivityEventsExecutor != null) { |
|
|
|
deviceActivityEventsExecutor.shutdownNow(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@AfterStartUp(order = AfterStartUp.REGULAR_SERVICE) |
|
|
|
@ -665,13 +673,13 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
} |
|
|
|
var tenantId = toTenantId(deviceConnectMsg.getTenantIdMSB(), deviceConnectMsg.getTenantIdLSB()); |
|
|
|
var deviceId = new DeviceId(new UUID(deviceConnectMsg.getDeviceIdMSB(), deviceConnectMsg.getDeviceIdLSB())); |
|
|
|
try { |
|
|
|
stateService.onDeviceConnect(tenantId, deviceId, deviceConnectMsg.getLastConnectTime()); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn("[{}] Failed to process device connect message for device [{}]", tenantId.getId(), deviceId.getId(), e); |
|
|
|
callback.onFailure(e); |
|
|
|
} |
|
|
|
ListenableFuture<?> future = deviceActivityEventsExecutor.submit(() -> stateService.onDeviceConnect(tenantId, deviceId, deviceConnectMsg.getLastConnectTime())); |
|
|
|
DonAsynchron.withCallback(future, |
|
|
|
__ -> callback.onSuccess(), |
|
|
|
t -> { |
|
|
|
log.warn("[{}] Failed to process device connect message for device [{}]", tenantId.getId(), deviceId.getId(), t); |
|
|
|
callback.onFailure(t); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
void forwardToStateService(TransportProtos.DeviceActivityProto deviceActivityMsg, TbCallback callback) { |
|
|
|
@ -680,13 +688,13 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
} |
|
|
|
var tenantId = toTenantId(deviceActivityMsg.getTenantIdMSB(), deviceActivityMsg.getTenantIdLSB()); |
|
|
|
var deviceId = new DeviceId(new UUID(deviceActivityMsg.getDeviceIdMSB(), deviceActivityMsg.getDeviceIdLSB())); |
|
|
|
try { |
|
|
|
stateService.onDeviceActivity(tenantId, deviceId, deviceActivityMsg.getLastActivityTime()); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn("[{}] Failed to process device activity message for device [{}]", tenantId.getId(), deviceId.getId(), e); |
|
|
|
callback.onFailure(new RuntimeException("Failed to update device activity for device [" + deviceId.getId() + "]!", e)); |
|
|
|
} |
|
|
|
ListenableFuture<?> future = deviceActivityEventsExecutor.submit(() -> stateService.onDeviceActivity(tenantId, deviceId, deviceActivityMsg.getLastActivityTime())); |
|
|
|
DonAsynchron.withCallback(future, |
|
|
|
__ -> callback.onSuccess(), |
|
|
|
t -> { |
|
|
|
log.warn("[{}] Failed to process device activity message for device [{}]", tenantId.getId(), deviceId.getId(), t); |
|
|
|
callback.onFailure(new RuntimeException("Failed to update device activity for device [" + deviceId.getId() + "]!", t)); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
void forwardToStateService(TransportProtos.DeviceDisconnectProto deviceDisconnectMsg, TbCallback callback) { |
|
|
|
@ -695,13 +703,13 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
} |
|
|
|
var tenantId = toTenantId(deviceDisconnectMsg.getTenantIdMSB(), deviceDisconnectMsg.getTenantIdLSB()); |
|
|
|
var deviceId = new DeviceId(new UUID(deviceDisconnectMsg.getDeviceIdMSB(), deviceDisconnectMsg.getDeviceIdLSB())); |
|
|
|
try { |
|
|
|
stateService.onDeviceDisconnect(tenantId, deviceId, deviceDisconnectMsg.getLastDisconnectTime()); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn("[{}] Failed to process device activity message for device [{}]", tenantId.getId(), deviceId.getId(), e); |
|
|
|
callback.onFailure(e); |
|
|
|
} |
|
|
|
ListenableFuture<?> future = deviceActivityEventsExecutor.submit(() -> stateService.onDeviceDisconnect(tenantId, deviceId, deviceDisconnectMsg.getLastDisconnectTime())); |
|
|
|
DonAsynchron.withCallback(future, |
|
|
|
__ -> callback.onSuccess(), |
|
|
|
t -> { |
|
|
|
log.warn("[{}] Failed to process device disconnect message for device [{}]", tenantId.getId(), deviceId.getId(), t); |
|
|
|
callback.onFailure(t); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
void forwardToStateService(TransportProtos.DeviceInactivityProto deviceInactivityMsg, TbCallback callback) { |
|
|
|
@ -710,13 +718,13 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore |
|
|
|
} |
|
|
|
var tenantId = toTenantId(deviceInactivityMsg.getTenantIdMSB(), deviceInactivityMsg.getTenantIdLSB()); |
|
|
|
var deviceId = new DeviceId(new UUID(deviceInactivityMsg.getDeviceIdMSB(), deviceInactivityMsg.getDeviceIdLSB())); |
|
|
|
try { |
|
|
|
stateService.onDeviceInactivity(tenantId, deviceId, deviceInactivityMsg.getLastInactivityTime()); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn("[{}] Failed to process device inactivity message for device [{}]", tenantId.getId(), deviceId.getId(), e); |
|
|
|
callback.onFailure(e); |
|
|
|
} |
|
|
|
ListenableFuture<?> future = deviceActivityEventsExecutor.submit(() -> stateService.onDeviceInactivity(tenantId, deviceId, deviceInactivityMsg.getLastInactivityTime())); |
|
|
|
DonAsynchron.withCallback(future, |
|
|
|
__ -> callback.onSuccess(), |
|
|
|
t -> { |
|
|
|
log.warn("[{}] Failed to process device inactivity message for device [{}]", tenantId.getId(), deviceId.getId(), t); |
|
|
|
callback.onFailure(t); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void forwardToNotificationSchedulerService(TransportProtos.NotificationSchedulerServiceMsg msg, TbCallback callback) { |
|
|
|
|