Commit 08895f84 authored by 张辰's avatar 张辰

test async

parent 34e69d74
package com.tanpu.community.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceAsyncTaskExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Component
@Configuration
public class ExecutorConfig {
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(50);
executor.setKeepAliveSeconds(10);
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
...@@ -5,14 +5,15 @@ import lombok.extern.slf4j.Slf4j; ...@@ -5,14 +5,15 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceAsyncTaskExecutor; import org.springframework.cloud.sleuth.instrument.async.LazyTraceAsyncTaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@Service //@Service
@Slf4j //@Slf4j
public class TraceTestService { public class TraceTestService {
@Autowired @Autowired
...@@ -36,6 +37,17 @@ public class TraceTestService { ...@@ -36,6 +37,17 @@ public class TraceTestService {
count = new AtomicInteger(0); count = new AtomicInteger(0);
} }
@Async("asyncTaskExecutor")
public void testTraceIdAsync(int i) {
try {
Thread.sleep(1000);
log.info("async count is " + i);
} catch (Exception e) {
}
}
public void testTraceId() { public void testTraceId() {
Integer c = count.incrementAndGet(); Integer c = count.incrementAndGet();
log.info("parent count is " + c); log.info("parent count is " + c);
...@@ -55,6 +67,5 @@ public class TraceTestService { ...@@ -55,6 +67,5 @@ public class TraceTestService {
} }
}); });
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment