Java生鮮電商平臺-SpringBoot2異步線程池實戰(小程序/APP)

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

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章