Skip to content

Commit

Permalink
move async classes to new maven module
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopar committed Jan 17, 2024
1 parent 0428ee8 commit 8267031
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 34 deletions.
22 changes: 22 additions & 0 deletions modules/async/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ SPDX-License-Identifier: Apache-2.0
~ SPDX-FileCopyrightText: Huawei Inc.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.7-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>async</artifactId>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

package org.eclipse.xpanse.modules.deployment.async;
package org.eclipse.xpanse.modules.async;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

package org.eclipse.xpanse.modules.deployment.async;
package org.eclipse.xpanse.modules.async;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -18,14 +18,15 @@
@Configuration
public class TaskConfiguration {

public static final String ASYNC_EXECUTOR_NAME = "xpanseAsyncTaskExecutor";
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();

/**
* Define ThreadPoolTaskExecutor named taskExecutor.
*
* @return executor
*/
@Bean("xpanseAsyncTaskExecutor")
@Bean(ASYNC_EXECUTOR_NAME)
public Executor taskExecutor() {
ServiceThreadPoolTaskExecutor executor = new ServiceThreadPoolTaskExecutor();
executor.setCorePoolSize(CPU_COUNT * 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

package org.eclipse.xpanse.modules.deployment.async;
package org.eclipse.xpanse.modules.async;

import java.util.Map;
import java.util.concurrent.Callable;
Expand All @@ -26,19 +26,16 @@ public final class ThreadMdcUtil {
*/
public static <T> Callable<T> wrap(final Callable<T> callable,
final Map<String, String> context) {
return new Callable<>() {
@Override
public T call() throws Exception {
if (context == null) {
MDC.clear();
} else {
MDC.setContextMap(context);
}
try {
return callable.call();
} finally {
MDC.clear();
}
return () -> {
if (context == null) {
MDC.clear();
} else {
MDC.setContextMap(context);
}
try {
return callable.call();
} finally {
MDC.clear();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.modules.deployment.async;
package org.eclipse.xpanse.modules.async;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.modules.deployment.async;
package org.eclipse.xpanse.modules.async;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.modules.deployment.async;
package org.eclipse.xpanse.modules.async;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -28,9 +28,7 @@ void testConcurrent() {

@Test
public void testWrapCallableWithNullContext() throws Exception {
Callable<String> callable = () -> {
return MDC.get("key");
};
Callable<String> callable = () -> MDC.get("key");
Callable<String> wrappedCallable = ThreadMdcUtil.wrap(callable, null);
MDC.put("key", "value");
String result = wrappedCallable.call();
Expand All @@ -40,9 +38,7 @@ public void testWrapCallableWithNullContext() throws Exception {

@Test
public void testWrapCallableWithNonNullContext() throws Exception {
Callable<String> callable = () -> {
return MDC.get("key");
};
Callable<String> callable = () -> MDC.get("key");
Map<String, String> context = new HashMap<>();
context.put("key", "value");
Callable<String> wrappedCallable = ThreadMdcUtil.wrap(callable, context);
Expand All @@ -54,7 +50,6 @@ public void testWrapCallableWithNonNullContext() throws Exception {
@Test
public void testWrapRunnableWithNullContext() {
Runnable runnable = () -> {
String value = MDC.get("key");
MDC.put("key", "modified");
};
Runnable wrappedRunnable = ThreadMdcUtil.wrap(runnable, null);
Expand All @@ -67,7 +62,6 @@ public void testWrapRunnableWithNullContext() {
@Test
public void testWrapRunnableWithNonNullContext() {
Runnable runnable = () -> {
String value = MDC.get("key");
MDC.put("key", "modified");
};
Map<String, String> context = new HashMap<>();
Expand Down
5 changes: 5 additions & 0 deletions modules/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<artifactId>policy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>async</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import java.util.UUID;
import java.util.concurrent.Executor;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.async.TaskConfiguration;
import org.eclipse.xpanse.modules.deployment.DeployService;
import org.eclipse.xpanse.modules.deployment.DeployServiceEntityHandler;
import org.eclipse.xpanse.modules.deployment.DeploymentResultCallbackManager;
import org.eclipse.xpanse.modules.deployment.async.TaskConfiguration;
import org.eclipse.xpanse.modules.deployment.deployers.terraform.config.TerraformLocalConfig;
import org.eclipse.xpanse.modules.deployment.utils.DeployEnvironments;
import org.eclipse.xpanse.modules.models.service.common.enums.Csp;
Expand Down Expand Up @@ -149,7 +149,7 @@ void testDeploy() {
Assertions.assertNotNull(deployResult.getPrivateProperties());
tfState = deployResult.getPrivateProperties().get(STATE_FILE_NAME);
Assertions.assertNull(tfState);
Assertions.assertEquals(null, deployResult.getState());
Assertions.assertNull(deployResult.getState());

}

Expand Down
1 change: 1 addition & 0 deletions modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<module>policy</module>
<module>workflow</module>
<module>observability</module>
<module>async</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.common.openapi.OpenApiGeneratorJarManage;
import org.eclipse.xpanse.common.openapi.OpenApiUrlManage;
import org.eclipse.xpanse.modules.async.TaskConfiguration;
import org.eclipse.xpanse.modules.database.servicetemplate.ServiceTemplateEntity;
import org.eclipse.xpanse.modules.models.common.exceptions.OpenApiFileGenerationException;
import org.eclipse.xpanse.modules.models.service.common.enums.Category;
Expand Down Expand Up @@ -89,7 +90,7 @@ public String getOpenApi(ServiceTemplateEntity serviceTemplateEntity) {
*
* @param registerService Registered services.
*/
@Async("xpanseAsyncTaskExecutor")
@Async(TaskConfiguration.ASYNC_EXECUTOR_NAME)
public void generateServiceApi(ServiceTemplateEntity registerService) {
createServiceApi(registerService);
}
Expand All @@ -99,7 +100,7 @@ public void generateServiceApi(ServiceTemplateEntity registerService) {
*
* @param registerService Registered services.
*/
@Async("xpanseAsyncTaskExecutor")
@Async(TaskConfiguration.ASYNC_EXECUTOR_NAME)
public void updateServiceApi(ServiceTemplateEntity registerService) {
File file =
new File(this.openApiGeneratorJarManage.getOpenApiWorkdir(),
Expand All @@ -115,7 +116,7 @@ public void updateServiceApi(ServiceTemplateEntity registerService) {
*
* @param id ID of registered service.
*/
@Async("xpanseAsyncTaskExecutor")
@Async(TaskConfiguration.ASYNC_EXECUTOR_NAME)
public void deleteServiceApi(String id) {
File file = new File(this.openApiGeneratorJarManage.getOpenApiWorkdir(),
id + OPENAPI_FILE_EXTENSION);
Expand Down Expand Up @@ -151,7 +152,7 @@ public String createServiceApi(ServiceTemplateEntity registerService) {
if (yamlFile.exists() && jarPath.exists()) {
String comm = String.format("java -jar %s generate -g html2 "
+ "-i %s -o %s", jarPath.getPath(), yamlFile.getPath(), openApiDir);
Process exec = Runtime.getRuntime().exec(comm);
Process exec = Runtime.getRuntime().exec(comm.split("\\s+"));
StringBuilder stdErrOut = new StringBuilder();
BufferedReader outputReader =
new BufferedReader(new InputStreamReader(exec.getErrorStream()));
Expand Down
Binary file added runtime/src/main/resources/static/favicon.ico
Binary file not shown.

0 comments on commit 8267031

Please sign in to comment.