상세 컨텐츠

본문 제목

[Spring] Async pattern 예제

소프트웨어/spring

by 야솔아빠 2012. 1. 30. 16:03

본문

반응형
1. spring 설정 xml에 다음과 같이 등록한다.

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:beans="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

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">


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->

<annotation-driven />


<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

<resources mapping="/resources/**" location="/resources/" />


<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<beans:property name="prefix" value="/WEB-INF/views/" />

<beans:property name="suffix" value=".jsp" />

</beans:bean>

<context:component-scan base-package="com.inho.async" />

<task:annotation-driven scheduler="scheduler"/>

<task:scheduler id="scheduler" pool-size="1"/>

<beans:bean id="messageSender" class="com.inho.async.message.MessageSenderImpl" />

</beans:beans> 


2. MessageSender.java 생성 

package com.inho.async.message;


import java.util.concurrent.Future;


import org.springframework.scheduling.annotation.Async;

import org.springframework.stereotype.Component;


@Component

public interface MessageSender {

@Async

public Future<String> send(String message);

@Async

public void send2(String message);

}


3. MessageSenderImpl.java 코딩.

package com.inho.async.message;


import java.util.concurrent.Future;


import org.springframework.scheduling.annotation.Async;

import org.springframework.scheduling.annotation.AsyncResult;


public class MessageSenderImpl implements MessageSender {

@Async

public Future<String> send(String message) {

// TODO Auto-generated method stub

long sTime = System.currentTimeMillis();

System.out.println(System.currentTimeMillis() + " : " + "MessagesSenderImpl.send(String message) start. ");

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

long eTime = System.currentTimeMillis();

System.out.println(System.currentTimeMillis() + " : " +"MessagesSenderImpl.send(String message) end. ");

return new AsyncResult<String>("[echo]" + message);

}


@Async

public void send2(String message) {

// TODO Auto-generated method stub

long sTime = System.currentTimeMillis();

System.out.println(System.currentTimeMillis() + " : " + "MessagesSenderImpl.send2(String message) start. ");

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

long eTime = System.currentTimeMillis();

System.out.println(System.currentTimeMillis() + " : " + "MessagesSenderImpl.send2(String message) end. ");

}


}


위 소스를 보면 Future<String> 형태로 return type이 있는 예와 void tyep 예로 두개가 있습니다.

간단히 STS에서 생성된 HomeController.java를 이용해서 테스트를 해봤습니다.
HomeController.java

package com.inho.async;


import java.text.DateFormat;

import java.util.Date;

import java.util.Locale;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Future;


import javax.annotation.Resource;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;


import com.inho.async.message.MessageSender;


/**

 * Handles requests for the application home page.

 */

@Controller

public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

@Resource

private MessageSender messageSender;

/**

* Simply selects the home view to render by returning its name.

*/

@RequestMapping(value = "/", method = RequestMethod.GET)

public String home(Locale locale, Model model) {

logger.info("Welcome home! the client locale is "+ locale.toString());

Date date = new Date();

DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

String formattedDate = dateFormat.format(date);

model.addAttribute("serverTime", formattedDate );

System.out.println(System.currentTimeMillis() + " : " + "controller(Future type) start. " );

Future<String> recvMessage = messageSender.send("test message1");


try {

System.out.println(System.currentTimeMillis() + " : " + "controller(Future type) end. - " + recvMessage.get());

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(System.currentTimeMillis() + " : " + "controller(void type) start. " );

messageSender.send2("test message2");

System.out.println(System.currentTimeMillis() + " : " + "controller(void type) end. " );

return "home";

}

}


테스트를 하기 위해서 웹브라우져서에서 다음과 같이 입력하면됩니다.
http://localhost:8080/SpringAsync/ 

테스트 결과 화면

INFO : com.inho.async.HomeController - Welcome home! the client locale is ko_KR

1327905084094 : controller(Future type) start. 

1327905084216 : MessagesSenderImpl.send(String message) start. 

1327905094218 : MessagesSenderImpl.send(String message) end. 

1327905084214 : controller(Future type) end. - [echo]test message1

1327905094220 : controller(void type) start. 

1327905094220 : controller(void type) end. 

1327905094275 : MessagesSenderImpl.send2(String message) start. 

1327905104275 : MessagesSenderImpl.send2(String message) end. 

 
좀 의아했던 것은, Future type으로 return 되는 api 테스트는  아래와 같이 controller 관련 log가 먼저 나오지 않았습니다.
controller(Future type) start.
controller(Future type) end. - [echo]test message1

최소한, 다음과 같이 Impl 보다 controller에 제어권이 넘어올 줄 알았습니다.
controller(Future type) start. 
MessagesSenderImpl.send(String message) start.
controller(Future type) end. - [echo]test message1
MessagesSenderImpl.send(String message) end. 

그런데, system time을 보면 비동기로 수행된 것은 맞습니다.
1327905084216 : MessagesSenderImple.send(String message) start.
1327905084214 : controller(Future type) end. - [echo]test message1
반응형

관련글 더보기

댓글 영역