这篇文章主要介绍了使用Spring开启@Async异步方式(javaconfig配置),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
目录
Spring开启@Async异步(javaconfig配置)
应用场景
创建AsyncTask
创建spring配置AppConfig
测试
Spring @Async Demo
Spring开启@Async异步(javaconfig配置)
在Spring中,基于@Async标注的方法,称之为异步方法;这些方法将在执行的时候,将会在独立的线程中被执行,调用者无需等待它的完成,即可继续其他的操作。
应用场景
某些耗时较长的方法,后面的程序不需要用到这个方法的处理结果时
创建AsyncTask
/** * 异步任务 * * @author Peng */ public class AsyncTask { @Async public void doAsyncTask() throws InterruptedException { // 假设执行一个很耗时的任务 Thread.sleep(10 * 1000); System.out.println("执行完成,我执行了10秒"); } }
创建spring配置AppConfig
/** * spring 配置 * * @author Peng */ @Configuration @EnableAsync public class AppConfig { /** * 声明异步任务bean * * @return */ @Bean public AsyncTask asyncTask() { return new AsyncTask(); } }
测试
/** * 异步测试 * * @author Peng */ public class AppTest { public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); AsyncTask task = ctx.getBean(AsyncTask.class); task.doAsyncTask(); System.out.println("异步任务调用成功,返回客户端执行成功,异步任务继续执行"); } }
执行结果
异步任务调用成功,返回客户端执行成功,异步任务继续执行
执行完成,我执行了10秒
从结果可以看出,异步任务测试成功!
Spring @Async Demo
Project Directory
Maven Dependency
4.0.0org.fool.springasyncspringasyncspringasyncwar1.0.0-BUILD-SNAPSHOT4.2.8.RELEASEOrg.springframeworkspring-context${org.springframework-version}org.springframeworkspring-webmvc${org.springframework-version}com.fasterxml.jackson.corejackson-databind2.8.1com.google.guavaguava19.0org.apache.commonscommons-lang33.4javax.injectjavax.inject1javax.servletjavax.servlet-api3.1.0providedjavax.servlet.jspjavax.servlet.jsp-api2.3.1providedjavax.servletjstl1.2junitjunit4.12testorg.apache.maven.pluginsmaven-eclipse-plugin2.10org.springframework.ide.eclipse.core.springnatureorg.springframework.ide.eclipse.core.springbuildertruetrueorg.apache.maven.pluginsmaven-compiler-plugin3.5.11.81.8-Xlint:alltruetrueorg.eclipse.jettyjetty-maven-plugin9.3.11.v20160721108888/springasync
web.xml
contextConfigLocation/WEB-INF/spring/root-context.xmlorg.springframework.web.context.ContextLoaderListenerappServletorg.springframework.web.servlet.dispatcherServletcontextConfigLocation/WEB-INF/spring/appServlet/servlet-context.xml1appServlet/
root-context.xml
servlet-context.xml
AsyncConfig.java
package org.fool.springasync; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration @EnableAsync @PropertySource("classpath:async.properties") public class AsyncConfig { /** Set the ThreadPoolExecutor's core pool size. */ @Value("${core.pool.size}") private Integer corePoolSize; /** Set the ThreadPoolExecutor's maximum pool size. */ @Value("${max.pool.size}") private Integer maxPoolSize; /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */ @Value("${queue.capacity}") private Integer queueCapacity; @Value("${thread.name.prefix}") private String ThreadNamePrefix; @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfig() { return new PropertySourcesPlaceholderConfigurer(); } @Bean public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setThreadNamePrefix(ThreadNamePrefix); // rejection-policy:当pool已经达到max size的时候,如何处理新任务 // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; } }
Note:
AsyncConfig使用Annotation进行Spring Async的配置,当然也可以用XML的方式进行配置,只需要在servlet-context.xml中添加task的命名空间,同时加下如下两行配置:
配置参数:
id:当配置多个executor时,被@Async("id")指定使用;也被作为线程名的前缀。
pool-size:
core size:最小的线程数,缺省:1
max size:最大的线程数,缺省:Integer.MAX_VALUE
queue-capacity:当最小的线程数已经被占用满后,新的任务会被放进queue里面,当这个queue的capacity也被占满之后,pool里面会创建新线程处理这个任务,直到总线程数达到了max size,这时系统会拒绝这个任务并抛出TaskRejectedException异常(缺省配置的情况下,可以通过rejection-policy来决定如何处理这种情况)。缺省值为:Integer.MAX_VALUE
keep-alive:超过core size的那些线程,任务完成后,再经过这个时长(秒)会被结束掉
rejection-policy:当pool已经达到max size的时候,如何处理新任务
ABORT(缺省):抛出TaskRejectedException异常,然后不执行
disCARD:不执行,也不抛出异常
disCARD_OLDEST:丢弃queue中最旧的那个任务
CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
MailService.java
package org.fool.springasync; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class MailService { @Async public void sendMail(String username) { System.out.println("Send Mail initialization..."); System.out.println("Execute method asynchronously - " + Thread.currentThread().getName()); try { Thread.sleep(5000); System.out.println("Welcome " + username); } catch (InterruptedException e) { e.printstacktrace(); } System.out.println("Send Mail Async done!!!"); } }
User.java
package org.fool.springasync; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class User { private Long id; private String username; private String password; public User() { } public User(Long id, String username, String password) { this.id = id; this.username = username; this.password = password; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getpassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
UserService.java
package org.fool.springasync; import javax.inject.Inject; import org.springframework.stereotype.Service; @Service public class UserService { @Inject private MailService mailService; public void registerUser(User user) { System.out.println("insert user to db..."); mailService.sendMail(user.getUsername()); System.out.println("register done, please check the email later!!!"); } }
UserController.java
package org.fool.springasync; import javax.inject.Inject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/user") public class UserController { @Inject private UserService userService; @RequestMapping(value = "/testasync", method = RequestMethod.POST) @ResponseBody public User register(@RequestBody User user) { System.out.println(user); userService.registerUser(user); return user; } }
Test
http://localhost:8888/springasync/user/testasync
POST请求(send两次)
Console Output
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。