Commit fe511f080e7b3bd0e846833962cf81359299340b
Committed by
Andrew Shvayka
1 parent
177c0f46
TbHttpClient for Rest API call node: implemented shared event loop for netty for…
… any rest api call node instance
Showing
3 changed files
with
71 additions
and
4 deletions
... | ... | @@ -78,7 +78,7 @@ public class TbHttpClient { |
78 | 78 | private AsyncRestTemplate httpClient; |
79 | 79 | private Deque<ListenableFuture<ResponseEntity<String>>> pendingFutures; |
80 | 80 | |
81 | - TbHttpClient(TbRestApiCallNodeConfiguration config) throws TbNodeException { | |
81 | + TbHttpClient(TbRestApiCallNodeConfiguration config, EventLoopGroup eventLoopGroupShared) throws TbNodeException { | |
82 | 82 | try { |
83 | 83 | this.config = config; |
84 | 84 | if (config.getMaxParallelRequestsCount() > 0) { |
... | ... | @@ -139,8 +139,7 @@ public class TbHttpClient { |
139 | 139 | } |
140 | 140 | httpClient = new AsyncRestTemplate(); |
141 | 141 | } else { |
142 | - this.eventLoopGroup = new NioEventLoopGroup(); | |
143 | - Netty4ClientHttpRequestFactory nettyFactory = new Netty4ClientHttpRequestFactory(this.eventLoopGroup); | |
142 | + Netty4ClientHttpRequestFactory nettyFactory = new Netty4ClientHttpRequestFactory(getSharedOrCreateEventLoopGroup(eventLoopGroupShared)); | |
144 | 143 | nettyFactory.setSslContext(config.getCredentials().initSslContext()); |
145 | 144 | nettyFactory.setReadTimeout(config.getReadTimeoutMs()); |
146 | 145 | httpClient = new AsyncRestTemplate(nettyFactory); |
... | ... | @@ -150,6 +149,13 @@ public class TbHttpClient { |
150 | 149 | } |
151 | 150 | } |
152 | 151 | |
152 | + EventLoopGroup getSharedOrCreateEventLoopGroup(EventLoopGroup eventLoopGroupShared) { | |
153 | + if (eventLoopGroupShared != null) { | |
154 | + return eventLoopGroupShared; | |
155 | + } | |
156 | + return this.eventLoopGroup = new NioEventLoopGroup(); | |
157 | + } | |
158 | + | |
153 | 159 | private void checkSystemProxyProperties() throws TbNodeException { |
154 | 160 | boolean useHttpProxy = !StringUtils.isEmpty(System.getProperty("http.proxyHost")) && !StringUtils.isEmpty(System.getProperty("http.proxyPort")); |
155 | 161 | boolean useHttpsProxy = !StringUtils.isEmpty(System.getProperty("https.proxyHost")) && !StringUtils.isEmpty(System.getProperty("https.proxyPort")); | ... | ... |
... | ... | @@ -51,7 +51,7 @@ public class TbRestApiCallNode implements TbNode { |
51 | 51 | @Override |
52 | 52 | public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { |
53 | 53 | TbRestApiCallNodeConfiguration config = TbNodeUtils.convert(configuration, TbRestApiCallNodeConfiguration.class); |
54 | - httpClient = new TbHttpClient(config); | |
54 | + httpClient = new TbHttpClient(config, ctx.getSharedEventLoop()); | |
55 | 55 | useRedisQueueForMsgPersistence = config.isUseRedisQueueForMsgPersistence(); |
56 | 56 | if (useRedisQueueForMsgPersistence) { |
57 | 57 | log.warn("[{}][{}] Usage of Redis Template is deprecated starting 2.5 and will have no affect", ctx.getTenantId(), ctx.getSelfId()); | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.rule.engine.rest; | |
17 | + | |
18 | + | |
19 | +import io.netty.channel.EventLoopGroup; | |
20 | +import io.netty.channel.nio.NioEventLoopGroup; | |
21 | +import org.junit.After; | |
22 | +import org.junit.Before; | |
23 | +import org.junit.Test; | |
24 | + | |
25 | +import static org.hamcrest.MatcherAssert.assertThat; | |
26 | +import static org.hamcrest.Matchers.instanceOf; | |
27 | +import static org.hamcrest.Matchers.is; | |
28 | +import static org.mockito.ArgumentMatchers.any; | |
29 | +import static org.mockito.BDDMockito.willCallRealMethod; | |
30 | +import static org.mockito.Mockito.mock; | |
31 | + | |
32 | +public class TbHttpClientTest { | |
33 | + | |
34 | + EventLoopGroup eventLoop; | |
35 | + TbHttpClient client; | |
36 | + | |
37 | + @Before | |
38 | + public void setUp() throws Exception { | |
39 | + client = mock(TbHttpClient.class); | |
40 | + willCallRealMethod().given(client).getSharedOrCreateEventLoopGroup(any()); | |
41 | + } | |
42 | + | |
43 | + @After | |
44 | + public void tearDown() throws Exception { | |
45 | + if (eventLoop != null) { | |
46 | + eventLoop.shutdownGracefully(); | |
47 | + } | |
48 | + } | |
49 | + | |
50 | + @Test | |
51 | + public void givenSharedEventLoop_whenGetEventLoop_ThenReturnShared() { | |
52 | + eventLoop = mock(EventLoopGroup.class); | |
53 | + assertThat(client.getSharedOrCreateEventLoopGroup(eventLoop), is(eventLoop)); | |
54 | + } | |
55 | + | |
56 | + @Test | |
57 | + public void givenNull_whenGetEventLoop_ThenReturnShared() { | |
58 | + eventLoop = client.getSharedOrCreateEventLoopGroup(null); | |
59 | + assertThat(eventLoop, instanceOf(NioEventLoopGroup.class)); | |
60 | + } | |
61 | +} | |
\ No newline at end of file | ... | ... |