Java生鲜电商平台-SpringBoot2异步线程池操作实战(小程序/APP)
说明:在Java生鲜电商平台中,不可避免的需要线程池以及异步操作。目的就是为了充分利用线程池来提高整个业务的吞吐量.
1.先看Java POM文件。
<?xml version="1.0" encoding="UTF-8"?>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.juren.fresh</groupId>
<artifactId>springboot-async</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-async</name>
<description>springboot-async</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 配置线程池。 线程池的具体参数,这个请自己去学习,这里不进行详细了解.
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; @Configuration public class ThreadConfig { @Bean("taskExecutor") public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(200); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("taskExecutor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); return executor; } }
3. 使用方法以及测试用例.
@EnableAsync //开启异步调用 @SpringBootApplication public class SpringbootAsyncApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAsyncApplication.class, args); } }
备注说明:
* springboot异步操作可以使用@EnableAsync和@Async两个注解,本质就是多线程和动态代理 * 异步方法使用注解@Async ,返回值为void或者Future * 异步方法和调用方法一定要写在不同的类中,如果写在一个类中是没有效果的
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.stream.IntStream; @Service public class TestService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); //@Async @Async("taskExecutor") public void test(){ logger.info(Thread.currentThread().getName()+".........2"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } logger.info(Thread.currentThread().getName()+".........3"); } }
最终测试类:
2021-01-04 19:57:50.491 INFO 6496 --- [ main] c.c.t.s.SpringbootAsyncApplicationTests : Started SpringbootAsyncApplicationTests in 4.074 seconds (JVM running for 5.927) [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.482 s - in com.ctg.test.springbootasync.SpringbootAsyncApplicationTests 2021-01-04 19:57:51.115 INFO 6496 --- [ taskExecutor-1] c.ctg.test.springbootasync.TestService : taskExecutor-1.........2 2021-01-04 19:57:51.141 INFO 6496 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'taskExecutor' 2021-01-04 19:57:52.115 INFO 6496 --- [ taskExecutor-1] c.ctg.test.springbootasync.TestService : taskExecutor-1.........3 [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ springboot-async ---
结语
复盘与总结.
总结:
做Java生鲜电商平台的互联网应用,无论是生鲜小程序还是APP,在高可用的系统设计中多线程异步的思路是非常重要的,本文只是起一个抛砖引玉的作用,
希望用生鲜小程序的搭建多线程异步的设计思路实战经验告诉大家一些实际的项目经验,希望对大家有用.
QQ:137071249
共同学习QQ群:793305035