diff --git a/Chapter4-1-4/pom.xml b/Chapter4-1-4/pom.xml new file mode 100644 index 0000000..c8b4be2 --- /dev/null +++ b/Chapter4-1-4/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + com.didispace + Chapter4-1-4 + 1.0.0 + jar + + Chapter4-1-4 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.projectlombok + lombok + 1.16.20 + provided + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter4-1-4/src/main/java/com/didispace/Application.java b/Chapter4-1-4/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..5de0f63 --- /dev/null +++ b/Chapter4-1-4/src/main/java/com/didispace/Application.java @@ -0,0 +1,36 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; + +import java.util.concurrent.Executor; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @EnableAsync + @Configuration + class TaskPoolConfig { + + @Bean("taskExecutor") + public Executor taskExecutor() { + ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler(); + executor.setPoolSize(20); + executor.setThreadNamePrefix("taskExecutor-"); + + executor.setWaitForTasksToCompleteOnShutdown(true); + executor.setAwaitTerminationSeconds(60); + return executor; + } + + } + +} diff --git a/Chapter4-1-4/src/main/java/com/didispace/async/Task.java b/Chapter4-1-4/src/main/java/com/didispace/async/Task.java new file mode 100644 index 0000000..ac0b102 --- /dev/null +++ b/Chapter4-1-4/src/main/java/com/didispace/async/Task.java @@ -0,0 +1,49 @@ +package com.didispace.async; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/16 下午12:58. + * @blog http://blog.didispace.com + */ +@Slf4j +@Component +public class Task { + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Async("taskExecutor") + public void doTaskOne() throws Exception { + log.info("开始做任务一"); + long start = System.currentTimeMillis(); + log.info(stringRedisTemplate.randomKey()); + long end = System.currentTimeMillis(); + log.info("完成任务一,耗时:" + (end - start) + "毫秒"); + } + + @Async("taskExecutor") + public void doTaskTwo() throws Exception { + log.info("开始做任务二"); + long start = System.currentTimeMillis(); + log.info(stringRedisTemplate.randomKey()); + long end = System.currentTimeMillis(); + log.info("完成任务二,耗时:" + (end - start) + "毫秒"); + } + + @Async("taskExecutor") + public void doTaskThree() throws Exception { + log.info("开始做任务三"); + long start = System.currentTimeMillis(); + log.info(stringRedisTemplate.randomKey()); + long end = System.currentTimeMillis(); + log.info("完成任务三,耗时:" + (end - start) + "毫秒"); + } + +} diff --git a/Chapter4-1-4/src/main/resources/application.properties b/Chapter4-1-4/src/main/resources/application.properties new file mode 100644 index 0000000..806eddf --- /dev/null +++ b/Chapter4-1-4/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.redis.pool.max-wait=5000 +spring.redis.pool.max-active=10 diff --git a/Chapter4-1-4/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-1-4/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..61107f2 --- /dev/null +++ b/Chapter4-1-4/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,34 @@ +package com.didispace; + +import com.didispace.async.Task; +import lombok.SneakyThrows; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class ApplicationTests { + + @Autowired + private Task task; + + @Test + @SneakyThrows + public void test() { + + for (int i = 0; i < 10000; i++) { + task.doTaskOne(); + task.doTaskTwo(); + task.doTaskThree(); + + if (i == 9999) { + System.exit(0); + } + } + } + +}