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

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

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


   SpringMVC
   org.springframework.web.servlet.DispatcherServlet
   
      contextConfigLocation
      classpath:spring-mvc.xml
   
   1
   
   true

spring-mvc.xml



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

你可能感兴趣的:(spring mvc异步操作处理,注解方式)