일반 메소드를 AOP를 통하여 멀티 쓰레드로 동작시키기

2014/10/23 09:13

서비 Tip&Tech

reference : http://springinpractice.com/2008/11/15/aop-101-speeding-up-springs-javamailsenderimpl-with-aop


Advice 작성
[code java] package app.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;

public class ForkAdvice {
private static final Logger log = Logger.getLogger(ForkAdvice.class);

public void fork(final ProceedingJoinPoint pjp) {
new Thread(new Runnable() {
public void run() {
log.info("Forking method execution: " + pjp);
try {
pjp.proceed();
} catch (Throwable t) {
// All we can do is log the error.
log.error(t);
}
}
}).start();
}
} [/code]

applicationContext 를 통해 bean 설정. ( 혹은 annotation 기반으로 위 adice 에 pointcut 지정 )
[code xml]<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd>
         <jee:jndi-lookup id="mailSession" jndi-name="mail/Session" resource-ref="true"/>
         <bean id="mailSender"
        class="org.springframework.mail.javamail.JavaMailSenderImpl"
        p:session-ref="mailSession"/>
         <bean id="mailingListService"
        class="app.service.MailingListServiceImpl"
        p:mailSender-ref="mailSender"/>
             <bean id="forkAdvice" class="app.aop.ForkAdvice"/>
         <aop:config>
        <aop:aspect ref="forkAdvice">
            <aop:around method="fork" pointcut="execution(* org.springframework.mail.javamail.JavaMailSenderImpl.send(..))"/>
        </aop:aspect>
    </aop:config>
         ...
     </beans> [/code]
2014/10/23 09:13 2014/10/23 09:13
Trackback Address:이 글에는 트랙백을 보낼 수 없습니다