Spring 4 @Async 實例

#Spring 4 @Async 實例java

這裏寫一個簡單的示例來講明 Spring 異步任務的 @Async 註解使用;spring

##看下咱們的依賴關係app

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>

##在例子中,咱們會用到註解,看下咱們的 spring.xml 文件:異步

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="osuya.example"/>

    <task:annotation-driven />

    <bean class="osuya.example.Spring4Tasks" name="spring4Tasks"></bean>
</beans>

這個配置文件中,最主要的就是 task的命名空間以及這句 <task:annotation-driven /> 這告訴spring掃描咱們項目中 使用 @Async 和 @Scheduled 的註解async

##首先咱們建立 @Serviceide

>NormalService + NormalServiceImpl
>ASyncService + ASyncServiceImpl

##ASyncServiceImpl實現類:code

package osuya.example;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

@Service
public class ASyncServiceImpl implements ASyncService {

    @Autowired NormalService normalService;

    @Async
    @Override
    public Future<Boolean> async() {

        // Demonstrate that our beans are being injected
        System.out.println("Managed bean injected: " + (normalService != null));

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("I'm done!");

        return new AsyncResult<Boolean>(true);
    }

}

能夠看到,在這個異步方法中,咱們使用了 @Async 而且返回了 AsyncResult。component

##如今咱們來看看他是怎麼工做的:orm

package osuya.example;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Spring4Tasks {

    @Autowired ASyncService asyncService;

    @Autowired NormalService normalService;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/spring.xml");
        Spring4Tasks app = context.getBean(Spring4Tasks.class);
        app.start();
        System.exit(0);
    }

    public void start() {
        normalService.notAsync();

        Future<Boolean> result = asyncService.async();

        for(int i = 0; i < 5; i++) normalService.notAsync();

        while(!result.isDone()){
            // we wait
        }
    }
}

##輸出結果:xml

Not in a thread

Not in a thread

Not in a thread

Not in a thread

Not in a thread

Not in a thread

Managed bean injected: true

I'm done!

相關文章
相關標籤/搜索