spring mvc异步操作处理,注解方式

简单的异步操作处理;本用例使用的spring mvc框架,进行异步处理

首先设置web.xml:将是否支持异步设置为true

<servlet>
   <servlet-name>SpringMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
   <!--是否支持异步-->
   <async-supported>true</async-supported>
</servlet>

spring-mvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                  http://www.springframework.org/schema/task
                      http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- 支持异步方法执行 -->
<task:executor id="task_executor" pool-size="5"/>
<task:annotation-driven executor="task_executor"/>

service业务逻辑:

public interface ITaskService {
    @Async
    void asyncDemo();
}
@Service
@EnableAsync
public class TaskServiceImpl implements ITaskService {

    @Async
    @Override
    public void asyncDemo() {
        try {
            System.out.println("异步执行开始:"+new Date());
            Thread.sleep(10 * 1000);
            System.out.println("异步执行完毕:"+new Date());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

controller调用:

@Controller
@RequestMapping("/asyncTestDemo")
public class AsyncTestDemo {
    @Autowired
    private ITaskService taskService;

    @RequestMapping(params = "async")
    @ResponseBody
    public Map async(HttpServletRequest request) {
        Map map = new HashMap();
        map.put("code","201");
        taskService.asyncDemo();
        map.put("code","200");
        return map;
    }

}

转载于:https://my.oschina.net/wugong/blog/1558258

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值