From a8577b8a8c9b10babde8919a8b1dbf80c5df8afa Mon Sep 17 00:00:00 2001 From: sarocs Date: Sun, 27 Apr 2025 19:21:40 -0500 Subject: [PATCH] Publish device events asynchronously --- .../server/config/AsyncConfig.java | 39 +++++++++++++++++ .../dao/device/AsyncDeviceEventPublisher.java | 42 +++++++++++++++++++ .../server/dao/device/DeviceServiceImpl.java | 17 +++++++- 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 application/src/main/java/org/thingsboard/server/config/AsyncConfig.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/device/AsyncDeviceEventPublisher.java diff --git a/application/src/main/java/org/thingsboard/server/config/AsyncConfig.java b/application/src/main/java/org/thingsboard/server/config/AsyncConfig.java new file mode 100644 index 0000000000..60012e7ef5 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/config/AsyncConfig.java @@ -0,0 +1,39 @@ +/** + * Copyright © 2016-2025 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +@Configuration +@EnableAsync +public class AsyncConfig { + + @Bean("deviceEventExecutor") + public Executor deviceEventExecutor() { + ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor(); + exec.setCorePoolSize(2); + exec.setMaxPoolSize(5); + exec.setQueueCapacity(100); + exec.setThreadNamePrefix("device-event-"); + exec.initialize(); + return exec; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/AsyncDeviceEventPublisher.java b/dao/src/main/java/org/thingsboard/server/dao/device/AsyncDeviceEventPublisher.java new file mode 100644 index 0000000000..1c37ce202b --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/device/AsyncDeviceEventPublisher.java @@ -0,0 +1,42 @@ +/** + * Copyright © 2016-2025 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.device; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; +import org.thingsboard.server.cache.device.DeviceCacheEvictEvent; +import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; + +@Service +public class AsyncDeviceEventPublisher { + private final ApplicationEventPublisher publisher; + public AsyncDeviceEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + + @Async("deviceEventExecutor") + public void publishCacheEvictEventAsync(DeviceCacheEvictEvent event) { + publisher.publishEvent(event); + } + + @Async("deviceEventExecutor") + public void publishSaveEventAsync(SaveEntityEvent event) { + publisher.publishEvent(event); + } +} + + diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java index 6d993f3e3d..3f6eb8036a 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java @@ -21,9 +21,11 @@ import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.event.TransactionalEventListener; +import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.CollectionUtils; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.cache.device.DeviceCacheEvictEvent; @@ -115,6 +117,9 @@ public class DeviceServiceImpl extends CachedVersionedEntityService