Spring Asyn 使用

来源:互联网 发布:web服务器软件 编辑:程序博客网 时间:2024/05/16 12:42

Reference : http://www.jarorwar.com/51

1.XML config

<?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-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"default-autowire="byType" default-lazy-init="true"><context:annotation-config /><task:annotation-driven /><context:component-scan base-package="com.spring.asyn" /><bean id="reportAsyncProxy" class="com.spring.asyn.ReportAsyncProxy"></bean></beans>

2.Interface

package com.spring.asyn;public interface ReportAsyncInterface{public void asyncGenerateReport();public void asyncGenerateGrpReport();}


3.Asyn class & method

package com.spring.asyn;import org.springframework.scheduling.annotation.Async;public class ReportAsyncProxy implements ReportAsyncInterface{@Asyncpublic void asyncGenerateReport(){System.out.println("====individual asyncGenerateReport === " +System.currentTimeMillis());try {Thread.sleep(10000);System.out.println("== sleep done");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Asyncpublic void asyncGenerateGrpReport(){System.out.println("====group asyncGenerateGrpReport === " +System.currentTimeMillis());}}
4.Test Class
package com.spring.asyn;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ReportAsynTest {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        String[] locations = { "asyn_config.xml" };        ApplicationContext context = new ClassPathXmlApplicationContext(                locations);        ReportService service = (ReportService) context.getBean("G3ReportService");        service.reportAsyncInterface.asyncGenerateReport();            service.reportAsyncInterface.asyncGenerateGrpReport();        }}


5. Printing

====individual asyncGenerateReport === 1339494231093
====group asyncGenerateGrpReport === 1339494231093
== sleep done

如果不用@Asyn, 结果如下: 

====individual asyncGenerateReport === 1339494231093

<here sleeping 10000 milli seconds >

== sleep done

====group asyncGenerateGrpReport === 1339494231093



6. Another good sample :

http://www.skill-guru.com/blog/2010/01/13/asynchronous-method-invocation-in-spring-3-0/

Spring 3.0  has added  annotation support for both task scheduling and asynchronous method execution.  We will discuss @Async annotation and its uses.

The @Async annotation

The @Async annotation allows invocation of that method to occur asynchronously. In other words, the caller will return immediately upon invocation.

One of the use cases of @Async function can be in situations where in user registers and system has to send confirmation mail or any similar task where you do not want to block user.

We will look at the  example below to explain it more clearly . Since for most of you this will be first Spring 3.0 application, please notice the changes in applicationContext.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:p=”http://www.springframework.org/schema/p” xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd”>

<context:component-scan base-package=”cs”/>

</beans>

Let us now look at our classes

TestService.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
import cs.service.RegularService;

public class TestService {

public static void main(String args[]){

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {
“applicationContext.xml”});

RegularService regService = (RegularService) appContext.getBean(“regularService”);

regService.registerUser(“Skill-Guru”);

}

}

RegularService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cs.async.MailUtility;

@Service
public class RegularService {

@Autowired
private MailUtility mailUtility ;

public void registerUser(String userName){

System.out.println(” User registration for  “+userName +” complete”);

mailUtility.sendMail(userName);

System.out.println(” Registration Complete. Mail will be send after 5 seconds “);
}

}

MailUtility.java

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

@Component
public class MailUtility {

@Async
public void sendMail(String name){

System.out.println(” I Will be formatting html mail and sending it  “);

try {
Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();
}

System.out.println(” Asynchronous method call of send email — Complete “);

}

}

Once you run the TestService.java the output would be

User registration for  Skill-Guru complete
I Will be formatting html mail and sending it
Asynchronous method call of send email — Complete
Registration Complete. Mail will be send after 5 seconds

Now wait a minute. This does not look like asynchronous call. What the hell is wrong here ?

We missed <task:annotation-driven/> in applicationContext.xml

The modified applicationContext.xml would be

<?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:p=”http://www.springframework.org/schema/p” 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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd“>

<context:component-scan base-package=”cs”/>

<task:annotation-driven/>

</beans>

Notice the changes in bold.

Now run your TestService again. The output would be

User registration for  Skill-Guru complete
Registration Complete. Mail will be send after 5 seconds
I Will be formatting html mail and sending it

The program will not exit and after 5 second you will see

Asynchronous method call of send email — Complete

You can see registerUser calls sendMail and the control is returned back to caller immediately.

Are you preparing for Spring Certification ? Try out the Spring certification Mock Practice test




原创粉丝点击