|
1 | | -package com.envisioniot.enos.iot_mqtt_sdk.core; |
2 | | - |
3 | | -import com.google.common.util.concurrent.ThreadFactoryBuilder; |
4 | | - |
5 | | -import java.util.concurrent.*; |
6 | | - |
7 | | -/** |
8 | | - * Factory that provides thread pools for handling connect, publish, time-out scheduler, |
9 | | - * async callback. Note that this factory is per MqttClient. <br/> |
10 | | - * <br/> |
11 | | - * User can customize the relevant thread pool through the provided <b>set</b> method. |
12 | | - * |
13 | | - * @author zhensheng.cai |
14 | | - * @author jian.zhang4 |
15 | | - */ |
16 | | -public class ExecutorFactory implements IExecutorFactory { |
17 | | - |
18 | | - /** |
19 | | - * Thread pools that handles mqtt publish action in async way. |
20 | | - */ |
21 | | - private ExecutorService publishExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, |
22 | | - new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("publish-executor-%d").build()); |
23 | | - |
24 | | - /** |
25 | | - * We should only have one connect action per MqttClient at the same time. So one |
26 | | - * thread should be good enough to handle async connection. |
27 | | - */ |
28 | | - private ExecutorService connectExecutor = Executors.newSingleThreadExecutor( |
29 | | - new ThreadFactoryBuilder().setNameFormat("connect-executor-%d").build()); |
30 | | - |
31 | | - /** |
32 | | - * callback timeout pool |
33 | | - */ |
34 | | - private ScheduledExecutorService timeoutScheduler = new ScheduledThreadPoolExecutor(3, |
35 | | - new ThreadFactoryBuilder().setNameFormat("timeout-pool-%d").build()); |
36 | | - |
37 | | - /** |
38 | | - * Thread pools that execute async callback provided by user |
39 | | - */ |
40 | | - private ExecutorService callbackExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, |
41 | | - new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setNameFormat("callback-executor-%d").build()); |
42 | | - |
43 | | - |
44 | | - /** |
45 | | - * shutdown all thread pools managed by this factory |
46 | | - */ |
47 | | - public void shutdownExecutorServices() { |
48 | | - publishExecutor.shutdownNow(); |
49 | | - connectExecutor.shutdownNow(); |
50 | | - timeoutScheduler.shutdownNow(); |
51 | | - callbackExecutor.shutdownNow(); |
52 | | - } |
53 | | - |
54 | | - public ExecutorService getPublishExecutor() { |
55 | | - return publishExecutor; |
56 | | - } |
57 | | - |
58 | | - public void setPublishExecutor(ExecutorService publishExecutor) { |
59 | | - this.publishExecutor = publishExecutor; |
60 | | - } |
61 | | - |
62 | | - public ExecutorService getConnectExecutor() { |
63 | | - return connectExecutor; |
64 | | - } |
65 | | - |
66 | | - public void setConnectExecutor(ExecutorService connectExecutor) { |
67 | | - this.connectExecutor = connectExecutor; |
68 | | - } |
69 | | - |
70 | | - public ScheduledExecutorService getTimeoutScheduler() { |
71 | | - return timeoutScheduler; |
72 | | - } |
73 | | - |
74 | | - public void setTimeoutScheduler(ScheduledExecutorService timeoutScheduler) { |
75 | | - this.timeoutScheduler = timeoutScheduler; |
76 | | - } |
77 | | - |
78 | | - public ExecutorService getCallbackExecutor() { |
79 | | - return callbackExecutor; |
80 | | - } |
81 | | - |
82 | | - public void setCallbackExecutor(ExecutorService callbackExecutor) { |
83 | | - this.callbackExecutor = callbackExecutor; |
84 | | - } |
85 | | -} |
| 1 | +package com.envisioniot.enos.iot_mqtt_sdk.core; |
| 2 | + |
| 3 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 4 | + |
| 5 | +import java.util.concurrent.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * Factory that provides thread pools for handling connect, publish, time-out scheduler, |
| 9 | + * async callback. Note that this factory is per MqttClient. <br/> |
| 10 | + * <br/> |
| 11 | + * User can customize the relevant thread pool through the provided <b>set</b> method. |
| 12 | + * |
| 13 | + * @author zhensheng.cai |
| 14 | + * @author jian.zhang4 |
| 15 | + */ |
| 16 | +public class ExecutorFactory implements IExecutorFactory { |
| 17 | + |
| 18 | + /** |
| 19 | + * Thread pools that handles mqtt publish action in async way. |
| 20 | + */ |
| 21 | + private ExecutorService publishExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, |
| 22 | + new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("publish-executor-%d").build()); |
| 23 | + |
| 24 | + /** |
| 25 | + * We should only have one connect action per MqttClient at the same time. So one |
| 26 | + * thread should be good enough to handle async connection. |
| 27 | + */ |
| 28 | + private ExecutorService connectExecutor = Executors.newSingleThreadExecutor( |
| 29 | + new ThreadFactoryBuilder().setNameFormat("connect-executor-%d").build()); |
| 30 | + |
| 31 | + /** |
| 32 | + * callback timeout pool |
| 33 | + */ |
| 34 | + private ScheduledExecutorService timeoutScheduler = new ScheduledThreadPoolExecutor(3, |
| 35 | + new ThreadFactoryBuilder().setNameFormat("timeout-pool-%d").build()); |
| 36 | + |
| 37 | + /** |
| 38 | + * Thread pools that execute async callback provided by user |
| 39 | + */ |
| 40 | + private ExecutorService callbackExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, |
| 41 | + new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setNameFormat("callback-executor-%d").build()); |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * shutdown all thread pools managed by this factory |
| 46 | + */ |
| 47 | + public void shutdownExecutorServices() { |
| 48 | + publishExecutor.shutdownNow(); |
| 49 | + connectExecutor.shutdownNow(); |
| 50 | + timeoutScheduler.shutdownNow(); |
| 51 | + callbackExecutor.shutdownNow(); |
| 52 | + } |
| 53 | + |
| 54 | + public ExecutorService getPublishExecutor() { |
| 55 | + return publishExecutor; |
| 56 | + } |
| 57 | + |
| 58 | + public void setPublishExecutor(ExecutorService publishExecutor) { |
| 59 | + this.publishExecutor = publishExecutor; |
| 60 | + } |
| 61 | + |
| 62 | + public ExecutorService getConnectExecutor() { |
| 63 | + return connectExecutor; |
| 64 | + } |
| 65 | + |
| 66 | + public void setConnectExecutor(ExecutorService connectExecutor) { |
| 67 | + this.connectExecutor = connectExecutor; |
| 68 | + } |
| 69 | + |
| 70 | + public ScheduledExecutorService getTimeoutScheduler() { |
| 71 | + return timeoutScheduler; |
| 72 | + } |
| 73 | + |
| 74 | + public void setTimeoutScheduler(ScheduledExecutorService timeoutScheduler) { |
| 75 | + this.timeoutScheduler = timeoutScheduler; |
| 76 | + } |
| 77 | + |
| 78 | + public ExecutorService getCallbackExecutor() { |
| 79 | + return callbackExecutor; |
| 80 | + } |
| 81 | + |
| 82 | + public void setCallbackExecutor(ExecutorService callbackExecutor) { |
| 83 | + this.callbackExecutor = callbackExecutor; |
| 84 | + } |
| 85 | +} |
0 commit comments