Im vorherigen Blog haben wir den asynchronen Betrieb von Spring Boot verwendet. Zu diesem Zeitpunkt haben wir den Standard -Thread -Pool verwendet, aber wenn wir unseren eigenen Thread -Pool gemäß dem Projekt anpassen wollten, sprechen wir darüber, wie Sie den Thread -Pool anpassen!
Paket com.chhliu.springboot.async.Configuration; import org.springframework.boot.context.properties.configurationProperties; @ConfigurationProperties (Präfix = "spring.task.pool") // Die Standorte dieser Annotation wurden aktiviert. So lange wie in der Umwelt, öffentliche Klasse Taskthreadpoolconfig {private int corepoolsize; private int maxpoolsize; private int keepaliveseconds; private int Queuecapacity; ……… Gotter, Setter -Methode auslassen ………} Paket com.chhliu.springboot.async.pool; import Java.util.concurrent.executor; Import Java.util.Concurrent.Threadpoolexecutor; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.Scheduling.annotation.enableAsync; import org.springframework.Scheduling.concurrent.threadpooltaskexecutor; import com.chhliu.springboot.async.configuration.taskthreadpoolconfig; @Configuration @enableAsync öffentliche Klasse Taskexecutepool {@autowired private taskthreadpoolconfig config; @Bean Public Executor mytaskasyncpool () {threadpooltaskexecutor executor = new threadpooltaskexecutor (); executor.setcorepoolSize (config.getCorepoolSize ()); executor.setMaxpoolSize (config.getMaxpoolSize ()); Executor.SetQuEUecapacity (config.getQueuecapacity ()); Executor.SetkeepaliveSeconds (config.getkeepaliveSeconds ()); Executor.setThreadnamePrefix ("myexecutor-"); // Abstoßungspolizei: Wie man neue Aufgaben erledigt, wenn der Pool maximal die Größe erreicht hat // Caller_runs: Führen Sie keine Aufgaben im neuen Thread aus, sondern führen Sie ausführend aus. Executor.initialize (); Rückgabereiter; }} Paket com.chhliu.springboot.async; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootApplication; import org.springframework.boot.context.properties.enableConfigurationProperties; import org.springframework.Scheduling.annotation.enableAsync; import com.chhliu.springboot.async.configuration.taskthreadpoolconfig; @SpringBootApplication @enableAsync @enableConfigurationProperties ({TaskThreadpoolConfig.class}) // Konfigurationseigenschaften aktivieren, um die öffentliche Klassen SpringbootasynCapplication {public static void Main (String [] Argument) {SpringApplication.run (SpringbootasyNcaptaS.Class, Class); }} Paket com.chhliu.springboot.async.pool; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.Scheduling.annotation.async; import org.springframework.stereotype.comPonent; @Component Public Class Asynctask {Protected Final Logger logger = loggerFactory.getLogger (this.getClass ()); @ASync ("mytaskasyncpool") // mytaskasyncpool ist der Methodame zum Konfigurieren des Thread -Pools. Wenn Sie den Methodennamen des benutzerdefinierten Thread -Pools hier nicht schreiben, wird der Standard -Thread -Pool veröffentlicht. }} Paket com.chhliu.springboot.async; import Java.util.concurrent.executionException; import org.junit.test; import org.junit.runner.runwith; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; import com.chhliu.springboot.async.pool.asynctask; @Runwith (springrunner.class) @springboottest public class SpringbootasynCapplicationTests {Protected Final Logger Logger = loggerFactory.getLogger (this.getClass ()); @Autowired Private Asynctask Asynctask; @Test public void asyncKaskTest () löscht InterruptedException, ExecutionException {for (int i = 0; i <100; i ++) {asyncTask.dotask1 (i); } logger.info ("Alle Aufgaben erledigen"); }} Die Testergebnisse sind wie folgt:
2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-10] ccSpringboot.async.pool.asynctask: Task60 Start. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-25] ccSpringboot.async.pool.asynctask: Task61 Start. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-6] ccSpringboot.async.pool.asynctask: Task62 Start. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-23] CCSpringboot.async.Pool.asynctask: Task63 gestartet. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-20] ccSpringboot.async.pool.asynctask: Task64 gestartet. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-19] ccSpringboot.async.pool.asynctask: Task65 gestartet. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-16] ccSpringboot.async.pool.asynctask: Task66 Start. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-15] ccSpringboot.async.pool.asynctask: Task67 Start. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-15] ccSpringboot.async.pool.asynctask: Task67 Start. 2017-03-20 20: 15: 15.208 Info 4068 --- [Myexecutor-12] ccSpringboot.async.pool.asynctask: Task68 Start. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-1] CCSpringboot.async.Pool.asynctask: Task69 gestartet. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-11] ccSpringboot.async.pool.asynctask: Task81 Start. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-8] ccSpringboot.async.pool.asynctask: Task82 gestartet. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-7] ccSpringboot.async.pool.asynctask: Task83 gestartet. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-4] CCSpringboot.async.pool.asynctask: Task84 gestartet. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-29] CCSpringboot.async.Pool.asynctask: Task85 gestartet. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-21] ccSpringboot.async.pool.asynctask: Task86 Start. 2017-03-20 20: 15: 15.209 Info 4068 --- [Myexecutor-17] ccSpringboot.async.pool.asynctask: Task88 Start.
Testergebnisse sind in Ordnung!
Wenn wir den Standard -Thread -Pool verwenden möchten, aber nur die Konfiguration des Standard -Thread -Pools ändern möchten, was sollten wir dann tun? Zu diesem Zeitpunkt müssen wir die AsyncconFigurer -Klasse implementieren. Der Beispielcode lautet wie folgt:
import Java.lang.reflect.Method; import Java.util.concurrent.executor; Import Java.util.Concurrent.Threadpoolexecutor; import org.springframework.aop.interceptor.asyncungaugtexceptionHandler; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.configuration; import org.springframework.Scheduling.annotation.asyncconfiger; import org.springframework.Scheduling.concurrent.threadpooltaskexecutor; import com.chhliu.cq.emailservice.threadconfiguration.taskthreadpoolconfig; import lombok.extern.slf4j.slf4j; /** * Note: This thread pool is shared by all asynchronous tasks and does not belong to a certain asynchronous task* Description: Thread pool for configuring asynchronous tasks* @author chhliu * Creation time: May 22, 2017 at 10:20:56 am * @version 1.2.0 */ @Slf4j @Configuration public class AsyncTaskExecutePool implements AsyncconFigurer {@autowired private taskthreadpoolconfig config; // Um die Eigenschaftsklasse zu konfigurieren, finden Sie im obigen Code @Override Public Executor getaSyncexecutor () {Threadpooltaskexecutor executor = new threadpooltaskexecutor (); executor.setcorepoolSize (config.getCorepoolSize ()); executor.setMaxpoolSize (config.getMaxpoolSize ()); Executor.SetQuEUecapacity (config.getQueuecapacity ()); Executor.SetkeepaliveSeconds (config.getkeepaliveSeconds ()); Executor.setThreadnamePrefix ("Taskexecutor-"); // Ablehnungspolitik: Wie man neue Aufgaben erledigt, wenn der Pool eine maximale Größe erreicht hat // Caller_runs: Die Aufgabe wird nicht im neuen Thread ausgeführt, aber der Thread, in dem der Anrufer lokalisiert ist, führt ausführender. Executor.initialize (); Rückgabereiter; } @Override public asyncuncaughexceptionHandler getaSyncuncaugtexceptionHandler () {// Ausnahmehandling in asynchronen Aufgaben geben neue asyncuncauglexceptionHandler () {@Override public void handleCaughtexception (Throwable Arg0, Method Arg1, Objekt ... arg2) {{{{{{ log.error("============================================================================================================= ================================================================= ================================================================= ================================================================= ================================================================= ================================================================= ================================================================= Fügen Sie bei der Verwendung @Async zur Methode hinzu.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.