Commit 2ad4ddf1fbe0c1350e31a70e369fe96c0085bb84
Committed by
Andrew Shvayka
1 parent
1b9df18c
pubsub improvements
Showing
5 changed files
with
124 additions
and
52 deletions
... | ... | @@ -45,6 +45,8 @@ import org.thingsboard.server.queue.settings.TbQueueTransportApiSettings; |
45 | 45 | import org.thingsboard.server.queue.settings.TbQueueTransportNotificationSettings; |
46 | 46 | import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
47 | 47 | |
48 | +import javax.annotation.PreDestroy; | |
49 | + | |
48 | 50 | @Component |
49 | 51 | @ConditionalOnExpression("'${queue.type:null}'=='pubsub' && '${service.type:null}'=='monolith'") |
50 | 52 | public class PubSubMonolithQueueFactory implements TbCoreQueueFactory, TbRuleEngineQueueFactory { |
... | ... | @@ -153,4 +155,23 @@ public class PubSubMonolithQueueFactory implements TbCoreQueueFactory, TbRuleEng |
153 | 155 | public TbQueueRequestTemplate<TbProtoJsQueueMsg<JsInvokeProtos.RemoteJsRequest>, TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> createRemoteJsRequestTemplate() { |
154 | 156 | return null; |
155 | 157 | } |
158 | + | |
159 | + @PreDestroy | |
160 | + private void destroy() { | |
161 | + if (coreAdmin != null) { | |
162 | + coreAdmin.destroy(); | |
163 | + } | |
164 | + if (ruleEngineAdmin != null) { | |
165 | + ruleEngineAdmin.destroy(); | |
166 | + } | |
167 | + if (jsExecutorAdmin != null) { | |
168 | + jsExecutorAdmin.destroy(); | |
169 | + } | |
170 | + if (transportApiAdmin != null) { | |
171 | + transportApiAdmin.destroy(); | |
172 | + } | |
173 | + if (notificationAdmin != null) { | |
174 | + notificationAdmin.destroy(); | |
175 | + } | |
176 | + } | |
156 | 177 | } | ... | ... |
... | ... | @@ -42,6 +42,8 @@ import org.thingsboard.server.queue.pubsub.TbPubSubConsumerTemplate; |
42 | 42 | import org.thingsboard.server.queue.pubsub.TbPubSubProducerTemplate; |
43 | 43 | import org.thingsboard.server.queue.pubsub.TbPubSubSettings; |
44 | 44 | |
45 | +import javax.annotation.PreDestroy; | |
46 | + | |
45 | 47 | @Component |
46 | 48 | @ConditionalOnExpression("'${queue.type:null}'=='pubsub' && '${service.type:null}'=='tb-core'") |
47 | 49 | public class PubSubTbCoreQueueFactory implements TbCoreQueueFactory { |
... | ... | @@ -128,4 +130,20 @@ public class PubSubTbCoreQueueFactory implements TbCoreQueueFactory { |
128 | 130 | public TbQueueRequestTemplate<TbProtoJsQueueMsg<JsInvokeProtos.RemoteJsRequest>, TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> createRemoteJsRequestTemplate() { |
129 | 131 | return null; |
130 | 132 | } |
133 | + | |
134 | + @PreDestroy | |
135 | + private void destroy() { | |
136 | + if (coreAdmin != null) { | |
137 | + coreAdmin.destroy(); | |
138 | + } | |
139 | + if (jsExecutorAdmin != null) { | |
140 | + jsExecutorAdmin.destroy(); | |
141 | + } | |
142 | + if (transportApiAdmin != null) { | |
143 | + transportApiAdmin.destroy(); | |
144 | + } | |
145 | + if (notificationAdmin != null) { | |
146 | + notificationAdmin.destroy(); | |
147 | + } | |
148 | + } | |
131 | 149 | } | ... | ... |
common/queue/src/main/java/org/thingsboard/server/queue/provider/PubSubTbRuleEngineQueueFactory.java
... | ... | @@ -41,6 +41,8 @@ import org.thingsboard.server.queue.settings.TbQueueCoreSettings; |
41 | 41 | import org.thingsboard.server.queue.settings.TbQueueRuleEngineSettings; |
42 | 42 | import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
43 | 43 | |
44 | +import javax.annotation.PreDestroy; | |
45 | + | |
44 | 46 | @Component |
45 | 47 | @ConditionalOnExpression("'${queue.type:null}'=='pubsub' && '${service.type:null}'=='tb-rule-engine'") |
46 | 48 | public class PubSubTbRuleEngineQueueFactory implements TbRuleEngineQueueFactory { |
... | ... | @@ -117,4 +119,20 @@ public class PubSubTbRuleEngineQueueFactory implements TbRuleEngineQueueFactory |
117 | 119 | public TbQueueRequestTemplate<TbProtoJsQueueMsg<JsInvokeProtos.RemoteJsRequest>, TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> createRemoteJsRequestTemplate() { |
118 | 120 | return null; |
119 | 121 | } |
122 | + | |
123 | + @PreDestroy | |
124 | + private void destroy() { | |
125 | + if (coreAdmin != null) { | |
126 | + coreAdmin.destroy(); | |
127 | + } | |
128 | + if (ruleEngineAdmin != null) { | |
129 | + ruleEngineAdmin.destroy(); | |
130 | + } | |
131 | + if (jsExecutorAdmin != null) { | |
132 | + jsExecutorAdmin.destroy(); | |
133 | + } | |
134 | + if (notificationAdmin != null) { | |
135 | + notificationAdmin.destroy(); | |
136 | + } | |
137 | + } | |
120 | 138 | } | ... | ... |
... | ... | @@ -40,6 +40,8 @@ import org.thingsboard.server.queue.settings.TbQueueRuleEngineSettings; |
40 | 40 | import org.thingsboard.server.queue.settings.TbQueueTransportApiSettings; |
41 | 41 | import org.thingsboard.server.queue.settings.TbQueueTransportNotificationSettings; |
42 | 42 | |
43 | +import javax.annotation.PreDestroy; | |
44 | + | |
43 | 45 | @Component |
44 | 46 | @ConditionalOnExpression("'${queue.type:null}'=='pubsub' && ('${service.type:null}'=='monolith' || '${service.type:null}'=='tb-transport')") |
45 | 47 | @Slf4j |
... | ... | @@ -111,4 +113,20 @@ public class PubSubTransportQueueFactory implements TbTransportQueueFactory { |
111 | 113 | transportNotificationSettings.getNotificationsTopic() + "." + serviceInfoProvider.getServiceId(), |
112 | 114 | msg -> new TbProtoQueueMsg<>(msg.getKey(), ToTransportMsg.parseFrom(msg.getData()), msg.getHeaders())); |
113 | 115 | } |
116 | + | |
117 | + @PreDestroy | |
118 | + private void destroy() { | |
119 | + if (coreAdmin != null) { | |
120 | + coreAdmin.destroy(); | |
121 | + } | |
122 | + if (ruleEngineAdmin != null) { | |
123 | + ruleEngineAdmin.destroy(); | |
124 | + } | |
125 | + if (transportApiAdmin != null) { | |
126 | + transportApiAdmin.destroy(); | |
127 | + } | |
128 | + if (notificationAdmin != null) { | |
129 | + notificationAdmin.destroy(); | |
130 | + } | |
131 | + } | |
114 | 132 | } | ... | ... |
... | ... | @@ -40,9 +40,10 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
40 | 40 | private static final String ACK_DEADLINE = "ackDeadlineInSec"; |
41 | 41 | private static final String MESSAGE_RETENTION = "messageRetentionInSec"; |
42 | 42 | |
43 | + private final TopicAdminClient topicAdminClient; | |
44 | + private final SubscriptionAdminClient subscriptionAdminClient; | |
45 | + | |
43 | 46 | private final TbPubSubSettings pubSubSettings; |
44 | - private final SubscriptionAdminSettings subscriptionAdminSettings; | |
45 | - private final TopicAdminSettings topicAdminSettings; | |
46 | 47 | private final Set<String> topicSet = ConcurrentHashMap.newKeySet(); |
47 | 48 | private final Set<String> subscriptionSet = ConcurrentHashMap.newKeySet(); |
48 | 49 | private final Map<String, String> subscriptionProperties; |
... | ... | @@ -51,6 +52,7 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
51 | 52 | this.pubSubSettings = pubSubSettings; |
52 | 53 | this.subscriptionProperties = subscriptionSettings; |
53 | 54 | |
55 | + TopicAdminSettings topicAdminSettings; | |
54 | 56 | try { |
55 | 57 | topicAdminSettings = TopicAdminSettings.newBuilder().setCredentialsProvider(pubSubSettings.getCredentialsProvider()).build(); |
56 | 58 | } catch (IOException e) { |
... | ... | @@ -58,6 +60,7 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
58 | 60 | throw new RuntimeException("Failed to create TopicAdminSettings."); |
59 | 61 | } |
60 | 62 | |
63 | + SubscriptionAdminSettings subscriptionAdminSettings; | |
61 | 64 | try { |
62 | 65 | subscriptionAdminSettings = SubscriptionAdminSettings.newBuilder().setCredentialsProvider(pubSubSettings.getCredentialsProvider()).build(); |
63 | 66 | } catch (IOException e) { |
... | ... | @@ -65,7 +68,9 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
65 | 68 | throw new RuntimeException("Failed to create SubscriptionAdminSettings."); |
66 | 69 | } |
67 | 70 | |
68 | - try (TopicAdminClient topicAdminClient = TopicAdminClient.create(topicAdminSettings)) { | |
71 | + try { | |
72 | + topicAdminClient = TopicAdminClient.create(topicAdminSettings); | |
73 | + | |
69 | 74 | ListTopicsRequest listTopicsRequest = |
70 | 75 | ListTopicsRequest.newBuilder().setProject(ProjectName.format(pubSubSettings.getProjectId())).build(); |
71 | 76 | TopicAdminClient.ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest); |
... | ... | @@ -77,7 +82,8 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
77 | 82 | throw new RuntimeException("Failed to get topics.", e); |
78 | 83 | } |
79 | 84 | |
80 | - try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings)) { | |
85 | + try { | |
86 | + subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings); | |
81 | 87 | |
82 | 88 | ListSubscriptionsRequest listSubscriptionsRequest = |
83 | 89 | ListSubscriptionsRequest.newBuilder() |
... | ... | @@ -104,31 +110,21 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
104 | 110 | return; |
105 | 111 | } |
106 | 112 | |
107 | - try (TopicAdminClient topicAdminClient = TopicAdminClient.create(topicAdminSettings)) { | |
108 | - ListTopicsRequest listTopicsRequest = | |
109 | - ListTopicsRequest.newBuilder().setProject(ProjectName.format(pubSubSettings.getProjectId())).build(); | |
110 | - TopicAdminClient.ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest); | |
111 | - for (Topic topic : response.iterateAll()) { | |
112 | - if (topic.getName().contains(topicName.toString())) { | |
113 | - topicSet.add(topic.getName()); | |
114 | - createSubscriptionIfNotExists(partition, topicName); | |
115 | - return; | |
116 | - } | |
113 | + ListTopicsRequest listTopicsRequest = | |
114 | + ListTopicsRequest.newBuilder().setProject(ProjectName.format(pubSubSettings.getProjectId())).build(); | |
115 | + TopicAdminClient.ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest); | |
116 | + for (Topic topic : response.iterateAll()) { | |
117 | + if (topic.getName().contains(topicName.toString())) { | |
118 | + topicSet.add(topic.getName()); | |
119 | + createSubscriptionIfNotExists(partition, topicName); | |
120 | + return; | |
117 | 121 | } |
118 | - | |
119 | - topicAdminClient.createTopic(topicName); | |
120 | - topicSet.add(topicName.toString()); | |
121 | - log.info("Created new topic: [{}]", topicName.toString()); | |
122 | - createSubscriptionIfNotExists(partition, topicName); | |
123 | - } catch (IOException e) { | |
124 | - log.error("Failed to create topic: [{}].", topicName.toString(), e); | |
125 | - throw new RuntimeException("Failed to create topic.", e); | |
126 | 122 | } |
127 | - } | |
128 | - | |
129 | - @Override | |
130 | - public void destroy() { | |
131 | 123 | |
124 | + topicAdminClient.createTopic(topicName); | |
125 | + topicSet.add(topicName.toString()); | |
126 | + log.info("Created new topic: [{}]", topicName.toString()); | |
127 | + createSubscriptionIfNotExists(partition, topicName); | |
132 | 128 | } |
133 | 129 | |
134 | 130 | private void createSubscriptionIfNotExists(String partition, ProjectTopicName topicName) { |
... | ... | @@ -139,36 +135,27 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
139 | 135 | return; |
140 | 136 | } |
141 | 137 | |
142 | - try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings)) { | |
143 | - ListSubscriptionsRequest listSubscriptionsRequest = | |
144 | - ListSubscriptionsRequest.newBuilder() | |
145 | - .setProject(ProjectName.of(pubSubSettings.getProjectId()).toString()) | |
146 | - .build(); | |
147 | - SubscriptionAdminClient.ListSubscriptionsPagedResponse response = | |
148 | - subscriptionAdminClient.listSubscriptions(listSubscriptionsRequest); | |
149 | - | |
150 | - for (Subscription subscription : response.iterateAll()) { | |
151 | - if (subscription.getName().equals(subscriptionName.toString())) { | |
152 | - subscriptionSet.add(subscription.getName()); | |
153 | - return; | |
154 | - } | |
138 | + ListSubscriptionsRequest listSubscriptionsRequest = | |
139 | + ListSubscriptionsRequest.newBuilder().setProject(ProjectName.of(pubSubSettings.getProjectId()).toString()).build(); | |
140 | + SubscriptionAdminClient.ListSubscriptionsPagedResponse response = subscriptionAdminClient.listSubscriptions(listSubscriptionsRequest); | |
141 | + for (Subscription subscription : response.iterateAll()) { | |
142 | + if (subscription.getName().equals(subscriptionName.toString())) { | |
143 | + subscriptionSet.add(subscription.getName()); | |
144 | + return; | |
155 | 145 | } |
146 | + } | |
156 | 147 | |
157 | - Subscription.Builder subscriptionBuilder = Subscription | |
158 | - .newBuilder() | |
159 | - .setName(subscriptionName.toString()) | |
160 | - .setTopic(topicName.toString()); | |
148 | + Subscription.Builder subscriptionBuilder = Subscription | |
149 | + .newBuilder() | |
150 | + .setName(subscriptionName.toString()) | |
151 | + .setTopic(topicName.toString()); | |
161 | 152 | |
162 | - setAckDeadline(subscriptionBuilder); | |
163 | - setMessageRetention(subscriptionBuilder); | |
153 | + setAckDeadline(subscriptionBuilder); | |
154 | + setMessageRetention(subscriptionBuilder); | |
164 | 155 | |
165 | - subscriptionAdminClient.createSubscription(subscriptionBuilder.build()); | |
166 | - subscriptionSet.add(subscriptionName.toString()); | |
167 | - log.info("Created new subscription: [{}]", subscriptionName.toString()); | |
168 | - } catch (IOException e) { | |
169 | - log.error("Failed to create subscription: [{}].", subscriptionName.toString(), e); | |
170 | - throw new RuntimeException("Failed to create subscription.", e); | |
171 | - } | |
156 | + subscriptionAdminClient.createSubscription(subscriptionBuilder.build()); | |
157 | + subscriptionSet.add(subscriptionName.toString()); | |
158 | + log.info("Created new subscription: [{}]", subscriptionName.toString()); | |
172 | 159 | } |
173 | 160 | |
174 | 161 | private void setAckDeadline(Subscription.Builder builder) { |
... | ... | @@ -186,4 +173,14 @@ public class TbPubSubAdmin implements TbQueueAdmin { |
186 | 173 | builder.setMessageRetentionDuration(duration); |
187 | 174 | } |
188 | 175 | } |
176 | + | |
177 | + @Override | |
178 | + public void destroy() { | |
179 | + if (topicAdminClient != null) { | |
180 | + topicAdminClient.close(); | |
181 | + } | |
182 | + if (subscriptionAdminClient != null) { | |
183 | + subscriptionAdminClient.close(); | |
184 | + } | |
185 | + } | |
189 | 186 | } | ... | ... |