From fe511f080e7b3bd0e846833962cf81359299340b Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Thu, 16 Sep 2021 14:34:24 +0300 Subject: [PATCH] TbHttpClient for Rest API call node: implemented shared event loop for netty for any rest api call node instance --- .../rule/engine/rest/TbHttpClient.java | 12 +++- .../rule/engine/rest/TbRestApiCallNode.java | 2 +- .../rule/engine/rest/TbHttpClientTest.java | 61 +++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbHttpClientTest.java diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java index 76f10ebb0c..08eec6fd2a 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java @@ -78,7 +78,7 @@ public class TbHttpClient { private AsyncRestTemplate httpClient; private Deque>> pendingFutures; - TbHttpClient(TbRestApiCallNodeConfiguration config) throws TbNodeException { + TbHttpClient(TbRestApiCallNodeConfiguration config, EventLoopGroup eventLoopGroupShared) throws TbNodeException { try { this.config = config; if (config.getMaxParallelRequestsCount() > 0) { @@ -139,8 +139,7 @@ public class TbHttpClient { } httpClient = new AsyncRestTemplate(); } else { - this.eventLoopGroup = new NioEventLoopGroup(); - Netty4ClientHttpRequestFactory nettyFactory = new Netty4ClientHttpRequestFactory(this.eventLoopGroup); + Netty4ClientHttpRequestFactory nettyFactory = new Netty4ClientHttpRequestFactory(getSharedOrCreateEventLoopGroup(eventLoopGroupShared)); nettyFactory.setSslContext(config.getCredentials().initSslContext()); nettyFactory.setReadTimeout(config.getReadTimeoutMs()); httpClient = new AsyncRestTemplate(nettyFactory); @@ -150,6 +149,13 @@ public class TbHttpClient { } } + EventLoopGroup getSharedOrCreateEventLoopGroup(EventLoopGroup eventLoopGroupShared) { + if (eventLoopGroupShared != null) { + return eventLoopGroupShared; + } + return this.eventLoopGroup = new NioEventLoopGroup(); + } + private void checkSystemProxyProperties() throws TbNodeException { boolean useHttpProxy = !StringUtils.isEmpty(System.getProperty("http.proxyHost")) && !StringUtils.isEmpty(System.getProperty("http.proxyPort")); boolean useHttpsProxy = !StringUtils.isEmpty(System.getProperty("https.proxyHost")) && !StringUtils.isEmpty(System.getProperty("https.proxyPort")); diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNode.java index 2ace27ef22..a23dbf820e 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNode.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNode.java @@ -51,7 +51,7 @@ public class TbRestApiCallNode implements TbNode { @Override public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { TbRestApiCallNodeConfiguration config = TbNodeUtils.convert(configuration, TbRestApiCallNodeConfiguration.class); - httpClient = new TbHttpClient(config); + httpClient = new TbHttpClient(config, ctx.getSharedEventLoop()); useRedisQueueForMsgPersistence = config.isUseRedisQueueForMsgPersistence(); if (useRedisQueueForMsgPersistence) { log.warn("[{}][{}] Usage of Redis Template is deprecated starting 2.5 and will have no affect", ctx.getTenantId(), ctx.getSelfId()); diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbHttpClientTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbHttpClientTest.java new file mode 100644 index 0000000000..52e37abec9 --- /dev/null +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbHttpClientTest.java @@ -0,0 +1,61 @@ +/** + * Copyright © 2016-2021 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.rule.engine.rest; + + +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willCallRealMethod; +import static org.mockito.Mockito.mock; + +public class TbHttpClientTest { + + EventLoopGroup eventLoop; + TbHttpClient client; + + @Before + public void setUp() throws Exception { + client = mock(TbHttpClient.class); + willCallRealMethod().given(client).getSharedOrCreateEventLoopGroup(any()); + } + + @After + public void tearDown() throws Exception { + if (eventLoop != null) { + eventLoop.shutdownGracefully(); + } + } + + @Test + public void givenSharedEventLoop_whenGetEventLoop_ThenReturnShared() { + eventLoop = mock(EventLoopGroup.class); + assertThat(client.getSharedOrCreateEventLoopGroup(eventLoop), is(eventLoop)); + } + + @Test + public void givenNull_whenGetEventLoop_ThenReturnShared() { + eventLoop = client.getSharedOrCreateEventLoopGroup(null); + assertThat(eventLoop, instanceOf(NioEventLoopGroup.class)); + } +} \ No newline at end of file