06.SpringBoot異步任務

使用場景, 發短信,發送郵件,APP消息推送

 

A.啓動類:

@EnableAsync  註解主要是爲了掃描範圍包下的所有 @Async註解

package springboot_async;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
@SpringBootApplication
@EnableAsync
public class App {
 
	public static void main(String[] args) {
 
		SpringApplication.run(App.class, args);
	}
 
}

B.使用異步回調:

在controller中無限循環判斷異步方法是否執行完成。

在service的方法中返回Future值。

controller:

package springboot_async.async_test;
 
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class TestController {
 
	@Autowired
	private MyService myService;
	
	@RequestMapping("/test")
	public String getInedx() throws InterruptedException, ExecutionException {
		System.out.println("開始訪問");
		long l1 = System.currentTimeMillis();
		Future<String> r1 = myService.JobOne();
		Future<String> r2 = myService.JobTwo();
		Future<String> r3 = myService.JobThree();
		while(true) {//死循環,每隔2000ms執行一次,判斷一下這三個異步調用的方法是否全都執行完了。
			if(r1.isDone() && r2.isDone() && r3.isDone()) {//使用Future的isDone()方法返回該方法是否執行完成
				//如果異步方法全部執行完,跳出循環
				break;
			}
			Thread.sleep(2000);//每隔2000毫秒判斷一次
		}
		long l2 = System.currentTimeMillis();//跳出while循環時說明此時三個異步調用的方法都執行完成了,此時得到當前時間
		
		String result = r1.get();
		System.out.println("結束訪問,用時"+(l2-l1));
		System.out.println("使用get方法獲得的返回內容:"+result);
		return "finished";
	}
}

C.service :

package springboot_async.async_test;
 
import java.util.concurrent.Future;
 
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
 
@Service
public class MyService {
 
	
	@Async
	public Future<String> JobOne() throws InterruptedException {
		System.out.println("開始執行任務一");
		long l1 = System.currentTimeMillis();
		Thread.sleep(2000);
		long l2 = System.currentTimeMillis();
		System.out.println("任務一用時"+(l2-l1));
		return new AsyncResult<String>("任務一完成");//可以使用try,catch定義在正常完成時返回一個success信息,出現異常時返回error信息。
	}
	
	@Async
	public Future<String> JobTwo() throws InterruptedException {
		System.out.println("開始執行任務二");
		long l1 = System.currentTimeMillis();
		Thread.sleep(2000);
		long l2 = System.currentTimeMillis();
		System.out.println("任務二用時"+(l2-l1));
		return new AsyncResult<String>("任務二完成");
	}
	
	
	@Async
	public Future<String> JobThree() throws InterruptedException {
		System.out.println("開始執行任務三");
		long l1 = System.currentTimeMillis();
		Thread.sleep(2000);
		long l2 = System.currentTimeMillis();
		System.out.println("任務三用時"+(l2-l1));
		return new AsyncResult<String>("任務三完成");
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章