Commit ee3abe59eabc8463ae0be84cb65b7731ae9d1f35
Committed by
Andrew Shvayka
1 parent
1c79480b
added mail executor to DefaultTbApiUsageStateService
Showing
1 changed file
with
19 additions
and
5 deletions
application/src/main/java/org/thingsboard/server/service/apiusage/DefaultTbApiUsageStateService.java
@@ -59,6 +59,7 @@ import org.thingsboard.server.service.queue.TbClusterService; | @@ -59,6 +59,7 @@ import org.thingsboard.server.service.queue.TbClusterService; | ||
59 | import org.thingsboard.server.service.telemetry.InternalTelemetryService; | 59 | import org.thingsboard.server.service.telemetry.InternalTelemetryService; |
60 | 60 | ||
61 | import javax.annotation.PostConstruct; | 61 | import javax.annotation.PostConstruct; |
62 | +import javax.annotation.PreDestroy; | ||
62 | import java.util.ArrayList; | 63 | import java.util.ArrayList; |
63 | import java.util.HashSet; | 64 | import java.util.HashSet; |
64 | import java.util.List; | 65 | import java.util.List; |
@@ -67,6 +68,8 @@ import java.util.Set; | @@ -67,6 +68,8 @@ import java.util.Set; | ||
67 | import java.util.UUID; | 68 | import java.util.UUID; |
68 | import java.util.concurrent.ConcurrentHashMap; | 69 | import java.util.concurrent.ConcurrentHashMap; |
69 | import java.util.concurrent.ExecutionException; | 70 | import java.util.concurrent.ExecutionException; |
71 | +import java.util.concurrent.ExecutorService; | ||
72 | +import java.util.concurrent.Executors; | ||
70 | import java.util.concurrent.TimeUnit; | 73 | import java.util.concurrent.TimeUnit; |
71 | import java.util.concurrent.locks.Lock; | 74 | import java.util.concurrent.locks.Lock; |
72 | import java.util.concurrent.locks.ReentrantLock; | 75 | import java.util.concurrent.locks.ReentrantLock; |
@@ -111,6 +114,8 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { | @@ -111,6 +114,8 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { | ||
111 | 114 | ||
112 | private final Lock updateLock = new ReentrantLock(); | 115 | private final Lock updateLock = new ReentrantLock(); |
113 | 116 | ||
117 | + private final ExecutorService mailExecutor; | ||
118 | + | ||
114 | public DefaultTbApiUsageStateService(TbClusterService clusterService, | 119 | public DefaultTbApiUsageStateService(TbClusterService clusterService, |
115 | PartitionService partitionService, | 120 | PartitionService partitionService, |
116 | TenantService tenantService, | 121 | TenantService tenantService, |
@@ -126,6 +131,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { | @@ -126,6 +131,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { | ||
126 | this.scheduler = scheduler; | 131 | this.scheduler = scheduler; |
127 | this.tenantProfileCache = tenantProfileCache; | 132 | this.tenantProfileCache = tenantProfileCache; |
128 | this.mailService = mailService; | 133 | this.mailService = mailService; |
134 | + this.mailExecutor = Executors.newSingleThreadExecutor(); | ||
129 | } | 135 | } |
130 | 136 | ||
131 | @PostConstruct | 137 | @PostConstruct |
@@ -297,11 +303,13 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { | @@ -297,11 +303,13 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { | ||
297 | ApiUsageRecordKey key = keys[i]; | 303 | ApiUsageRecordKey key = keys[i]; |
298 | msgs[i] = new ApiUsageStateMailMessage(key, state.getProfileThreshold(key), state.get(key)); | 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 | } else { | 314 | } else { |
307 | log.warn("[{}] Can't send update of the API state to tenant with empty email!", state.getTenantId()); | 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,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 | } |