From 6439354efe4dffa0aa03f7ada615cdba54ccc39f Mon Sep 17 00:00:00 2001 From: BLasan Date: Wed, 27 Nov 2024 13:49:20 +0530 Subject: [PATCH] fixes: https://github.com/wso2/api-manager/issues/3297 --- .../listeners/GatewayStartupListener.java | 26 ++++++---- .../gateway/utils/InternalServiceCall.java | 47 +++++++++++++++++++ .../impl/listeners/ServerStartupListener.java | 6 ++- 3 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/InternalServiceCall.java diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayStartupListener.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayStartupListener.java index 0aad7acfdd8c..690e686c342a 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayStartupListener.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayStartupListener.java @@ -37,6 +37,7 @@ import org.wso2.carbon.apimgt.gateway.throttling.util.BlockingConditionRetriever; import org.wso2.carbon.apimgt.gateway.throttling.util.KeyTemplateRetriever; import org.wso2.carbon.apimgt.gateway.utils.GatewayUtils; +import org.wso2.carbon.apimgt.gateway.utils.InternalServiceCall; import org.wso2.carbon.apimgt.gateway.webhooks.WebhooksDataHolder; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException; @@ -66,6 +67,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.stream.Stream; /** @@ -80,6 +83,7 @@ public class GatewayStartupListener extends AbstractAxis2ConfigurationContextObs private JMSTransportHandler jmsTransportHandlerForTrafficManager; private JMSTransportHandler jmsTransportHandlerForEventHub; private ThrottleProperties throttleProperties; + private ExecutorService service = Executors.newFixedThreadPool(3, new InternalServiceCall()); private GatewayArtifactSynchronizerProperties gatewayArtifactSynchronizerProperties; private boolean isAPIsDeployedInSyncMode = false; private boolean isGatewayPoliciesDeployedInSyncMode = false; @@ -192,11 +196,13 @@ public void completedServerStartup() { } }).start(); SubscriptionDataHolder.getInstance().registerTenantSubscriptionStore(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); - try { - retrieveAllAPIMetadata(); - } catch (DataLoadingException e) { - log.error("Error while loading All API Metadata", e); - } + service.execute(() -> { + try { + retrieveAllAPIMetadata(); + } catch (DataLoadingException e) { + log.error("Error while loading All API Metadata", e); + } + }); if (GatewayUtils.isOnDemandLoading()) { try { new EndpointCertificateDeployer().deployAllCertificatesAtStartup(); @@ -222,9 +228,13 @@ public void completedServerStartup() { jmsTransportHandlerForEventHub.subscribeForJmsEvents(APIConstants.TopicNames.TOPIC_ASYNC_WEBHOOKS_DATA, new GatewayJMSMessageListener()); copyTenantArtifacts(); - APILoggerManager.getInstance().initializeAPILoggerList(); - LLMProviderManager.getInstance().initializeLLMProviderConfigurations( - MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + service.execute(() -> { + APILoggerManager.getInstance().initializeAPILoggerList(); + }); + service.execute(() -> { + LLMProviderManager.getInstance() + .initializeLLMProviderConfigurations(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + }); } else { log.info("Running on migration enabled mode: Stopped at Gateway Startup listener completed"); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/InternalServiceCall.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/InternalServiceCall.java new file mode 100644 index 000000000000..4c33074dac13 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/InternalServiceCall.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com/). + * + * WSO2 LLC. licenses this file to you 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.wso2.carbon.apimgt.gateway.utils; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class InternalServiceCall implements ThreadFactory { + private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1); + private final ThreadGroup group; + private final AtomicInteger threadNumber = new AtomicInteger(1); + private final String namePrefix; + + public InternalServiceCall() { + SecurityManager securityManager = System.getSecurityManager(); + group = (securityManager != null) ? securityManager.getThreadGroup() : Thread.currentThread().getThreadGroup(); + namePrefix = "InternalServiceCall-pool-" + POOL_NUMBER.getAndIncrement() + "-thread-"; + } + + @Override + public Thread newThread(Runnable runnable) { + Thread thread = new Thread(group, runnable, namePrefix + threadNumber.getAndIncrement(), 0); + if (thread.isDaemon()) { + thread.setDaemon(false); + } + if (thread.getPriority() != Thread.NORM_PRIORITY) { + thread.setPriority(Thread.NORM_PRIORITY); + } + return thread; + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/listeners/ServerStartupListener.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/listeners/ServerStartupListener.java index f8fbeb0a17af..d07b0d6e240b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/listeners/ServerStartupListener.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/listeners/ServerStartupListener.java @@ -74,7 +74,11 @@ public void completedServerStartup() { } else { log.info("Running on migration enabled mode: Stopped at ServerStartupListener completed"); } - CorrelationConfigManager.getInstance().initializeCorrelationComponentList(); + + Thread thread = new Thread(() -> { + CorrelationConfigManager.getInstance().initializeCorrelationComponentList(); + }); + thread.start(); } /**