daily spring Async spring的异步方法演示 - wtdig/study GitHub Wiki

spring异步方法

1、 异步类写法:

package cn.wt.spring.saync;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.AsyncResult;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author wb-wt261136
 * @version 2017年9月7日 下午3:44:10
 */
@Component("processing")
public class Processing {

    @Async
    public void say() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("你好");
    }

    @Async("myAsync")
    public Future<String> sayReturn() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<String>("可以返回参数的:你好");
    }
}


2、 测试类方法

package cn.wt.spring.saync.test;

import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import cn.wt.spring.saync.Processing;

/**
 * @author wb-wt261136
 * @version 2017年9月7日 下午3:46:33
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AsyncTest {

    @Autowired
    private Processing processing;

    @Test
    public void asyncTest() throws Exception {
        System.out.println("开始");
        processing.say();
        System.out.println("结束");
        // 不让主进程过早结束
        Thread.sleep(3 * 1000);
    }

    @Test
    public void asyncTestReturn() throws Exception {
        System.out.println("开始");
        Future<String> sayReturn = processing.sayReturn();
        System.out.println("结束");
        // 不让主进程过早结束
        System.out.println(sayReturn.get());
        Thread.sleep(3 * 1000);
        //new FutureTask<>(callable);
    }
}

3、 配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd">
    
    <task:annotation-driven executor="annotationExecutor"/>
    <task:executor id="annotationExecutor" pool-size="10"/>
    <task:executor id="myAsync" pool-size="10"/>
    <context:component-scan base-package="cn.wt" />

</beans>

注意:在使用Async注解的方法体里面,不能出现多个request请求,否则会出现错误

⚠️ **GitHub.com Fallback** ⚠️