Commit ee3abe59eabc8463ae0be84cb65b7731ae9d1f35

Authored by YevhenBondarenko
Committed by Andrew Shvayka
1 parent 1c79480b

added mail executor to DefaultTbApiUsageStateService

... ... @@ -59,6 +59,7 @@ import org.thingsboard.server.service.queue.TbClusterService;
59 59 import org.thingsboard.server.service.telemetry.InternalTelemetryService;
60 60
61 61 import javax.annotation.PostConstruct;
  62 +import javax.annotation.PreDestroy;
62 63 import java.util.ArrayList;
63 64 import java.util.HashSet;
64 65 import java.util.List;
... ... @@ -67,6 +68,8 @@ import java.util.Set;
67 68 import java.util.UUID;
68 69 import java.util.concurrent.ConcurrentHashMap;
69 70 import java.util.concurrent.ExecutionException;
  71 +import java.util.concurrent.ExecutorService;
  72 +import java.util.concurrent.Executors;
70 73 import java.util.concurrent.TimeUnit;
71 74 import java.util.concurrent.locks.Lock;
72 75 import java.util.concurrent.locks.ReentrantLock;
... ... @@ -111,6 +114,8 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
111 114
112 115 private final Lock updateLock = new ReentrantLock();
113 116
  117 + private final ExecutorService mailExecutor;
  118 +
114 119 public DefaultTbApiUsageStateService(TbClusterService clusterService,
115 120 PartitionService partitionService,
116 121 TenantService tenantService,
... ... @@ -126,6 +131,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
126 131 this.scheduler = scheduler;
127 132 this.tenantProfileCache = tenantProfileCache;
128 133 this.mailService = mailService;
  134 + this.mailExecutor = Executors.newSingleThreadExecutor();
129 135 }
130 136
131 137 @PostConstruct
... ... @@ -297,11 +303,13 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
297 303 ApiUsageRecordKey key = keys[i];
298 304 msgs[i] = new ApiUsageStateMailMessage(key, state.getProfileThreshold(key), state.get(key));
299 305 }
300   - try {
301   - mailService.sendApiFeatureStateEmail(apiFeature, stateValue, email, msgs);
302   - } catch (ThingsboardException e) {
303   - log.warn("[{}] Can't send update of the API state to tenant with provided email [{}]", state.getTenantId(), email, e);
304   - }
  306 + mailExecutor.submit(() -> {
  307 + try {
  308 + mailService.sendApiFeatureStateEmail(apiFeature, stateValue, email, msgs);
  309 + } catch (ThingsboardException e) {
  310 + log.warn("[{}] Can't send update of the API state to tenant with provided email [{}]", state.getTenantId(), email, e);
  311 + }
  312 + });
305 313 });
306 314 } else {
307 315 log.warn("[{}] Can't send update of the API state to tenant with empty email!", state.getTenantId());
... ... @@ -386,4 +394,10 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
386 394 }
387 395 }
388 396
  397 + @PreDestroy
  398 + private void destroy() {
  399 + if (mailExecutor != null) {
  400 + mailExecutor.shutdownNow();
  401 + }
  402 + }
389 403 }
... ...