Pages

2014년 2월 18일 화요일

[SPRING] Spring mvc에서 Quartz 적용하기 - (7)

6. 작업 클래스 만들기
- 기존에서는 PostMan 클래스에서 주기마다 반복하면서 작업을 처리했는데, 스케줄러를 도입했으므로, 단순히 작업만 하는 클래스를 만들어보겠다.
- 간단한 PostMan 클래스에서 주기 처리 부분이 빠졌다고 보면 되겠다.

01package kr.kangwoo.postman.core;
02 
03import java.util.Date;
04import java.util.List;
05 
06import kr.kangwoo.postman.domain.Mail;
07import kr.kangwoo.postman.service.MailManager;
08import kr.kangwoo.postman.service.MailSendManager;
09import kr.kangwoo.postman.service.MailTemplateManager;
10import kr.kangwoo.util.StringUtils;
11 
12import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14 
15public class PostManJob {
16 
17    private Logger logger = LoggerFactory.getLogger(PostManJob.class);
18     
19    private String daemonName = getClass().getName();
20     
21    private MailManager mailManager;
22    private MailTemplateManager mailTemplateManager;
23    private MailSendManager mailSendManager;
24     
25    public void setDaemonName(String daemonName) {
26        this.daemonName = daemonName;
27    }
28 
29    public void setMailManager(MailManager mailManager) {
30        this.mailManager = mailManager;
31    }
32 
33    public void setMailTemplateManager(MailTemplateManager mailTemplateManager) {
34        this.mailTemplateManager = mailTemplateManager;
35    }
36 
37    public void setMailSendManager(MailSendManager mailSendManager) {
38        this.mailSendManager = mailSendManager;
39    }
40 
41    public void run() {
42        try {
43            logger.info("메일 템플릿 정보 적재 시작");
44            mailTemplateManager.reload();
45            logger.info("메일 템플릿 정보 적재 완료");
46             
47            List<Mail> sendList = mailManager.getSendList(daemonName);
48            if (logger.isDebugEnabled()) {
49                logger.debug("{}개의 메일을 가져왔습니다.", sendList != null ? sendList.size() : 0);  
50            }
51             
52            if (sendList != null) {
53                String subject = null;
54                String content = null;
55                for (Mail mail : sendList) {
56                    if (StringUtils.equals(mail.getStatusCode(), MailStatusCode.ACCEPTED)) {
57                        try {
58                            subject = mailTemplateManager.getSubject(mail);
59                            content = mailTemplateManager.getContent(mail);
60                            mail.setStatusCode(MailStatusCode.SEND_READY);
61                             
62                            mailSendManager.send(mail.getToAddress(), mail.getToName(), mail.getFromAddress(), mail.getFromName(), subject, content);
63                            mail.setStatusCode(MailStatusCode.SEND_OK);
64                            mail.setSentDate(new Date());
65                        catch (MailException e) {
66                            mail.setStatusCode(e.getStatusCode());
67                            logger.warn("메일 발송 중 에러가 발생했습니다.", e);
68                        catch (MessageException e) {
69                            mail.setStatusCode(MailStatusCode.UNKOWN_ERROR);
70                            logger.warn("메일 발송 중 에러가 발생했습니다.", e);
71                        catch (Exception e) {
72                            mail.setStatusCode(MailStatusCode.UNKOWN_ERROR);
73                            logger.warn("메일 발송 중 에러가 발생했습니다.", e);
74                        finally {
75                            mail.setUpdatedBy(daemonName);
76                            mail.setUpdatedDate(new Date());
77                            mailManager.updateMail(mail);
78                        }
79                    else {
80                        logger.warn("메일 상태 코드가 잘못되었습니다. (STATUS_CODE={})", mail.getStatusCode());
81                    }
82                }
83            }
84        catch (Exception e) {
85            logger.error(e.getMessage(), e);
86        }
87    }
88}

- PostMan 클래스는 단순히 스프링 프레임워크의 ApplicationContext 를 생성하는 역할만을 한다.
01package kr.kangwoo.postman.core;
02 
03import org.slf4j.Logger;
04import org.slf4j.LoggerFactory;
05import org.springframework.context.ApplicationContext;
06import org.springframework.context.support.ClassPathXmlApplicationContext;
07 
08public class PostMan {
09 
10    private Logger logger = LoggerFactory.getLogger(PostMan.class);
11     
12    public static void main(String[] args) {
13        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"post-man-scheduler.xml"});
14    }
15 
16}

댓글 없음:

댓글 쓰기