KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > imagedb > scheduling > ListImagesQuartzJob


1 package org.springframework.samples.imagedb.scheduling;
2
3 import java.util.List JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.quartz.JobExecutionContext;
8
9 import org.springframework.mail.MailSender;
10 import org.springframework.mail.SimpleMailMessage;
11 import org.springframework.samples.imagedb.ImageDatabase;
12 import org.springframework.samples.imagedb.ImageDescriptor;
13 import org.springframework.scheduling.quartz.QuartzJobBean;
14 import org.springframework.util.StringUtils;
15
16 /**
17  * Quartz Job implementation that lists all images in the image database.
18  * Writes the list to the log at INFO level and sends a corresponding email.
19  *
20  * <p>The email recipient is defined in "WEB-INF/mail.properties", to be
21  * passed to this Quartz Job via a bean property with a placeholder value.
22  *
23  * <p>NOTE: This is mainly an illustration for how to implement a
24  * Spring-managed Quartz Job. It is normally preferable to move
25  * business logic to manager classes, delegating to them via
26  * MethodInvokingJobDetailFactoryBean.
27  *
28  * @author Juergen Hoeller
29  * @since 23.02.2004
30  * @see ListImagesTimerTask
31  * @see org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
32  */

33 public class ListImagesQuartzJob extends QuartzJobBean {
34
35     private final Log logger = LogFactory.getLog(getClass());
36
37     private ImageDatabase imageDatabase;
38
39     private MailSender mailSender;
40
41     private String JavaDoc mailFrom;
42
43     private String JavaDoc mailTo;
44
45     public void setImageDatabase(ImageDatabase imageDatabase) {
46         this.imageDatabase = imageDatabase;
47     }
48
49     public void setMailSender(MailSender mailSender) {
50         this.mailSender = mailSender;
51     }
52
53     public void setMailFrom(String JavaDoc mailFrom) {
54         this.mailFrom = mailFrom;
55     }
56
57     public void setMailTo(String JavaDoc mailTo) {
58         this.mailTo = mailTo;
59     }
60
61     protected void executeInternal(JobExecutionContext context) {
62         logger.info("Listing images in image database, scheduled by Quartz");
63         List JavaDoc images = this.imageDatabase.getImages();
64         String JavaDoc[] imageNames = new String JavaDoc[images.size()];
65         for (int i = 0; i < images.size(); i++) {
66             ImageDescriptor image = (ImageDescriptor) images.get(i);
67             imageNames[i] = image.getName();
68         }
69
70         String JavaDoc text = "Images in image database: " + StringUtils.arrayToDelimitedString(imageNames, ", ");
71         logger.info(text);
72
73         if (!"".equals(this.mailTo)) {
74             logger.info("Sending image list mail to: " + this.mailTo);
75             SimpleMailMessage message = new SimpleMailMessage();
76             message.setFrom(this.mailFrom);
77             message.setTo(this.mailTo);
78             message.setSubject("Image list");
79             message.setText(text);
80             this.mailSender.send(message);
81         }
82         else {
83             logger.info("Not sending image list mail - specify mail settings in 'WEB-INF/mail.properties'");
84         }
85
86         logger.info("Next job execution at: " + context.getNextFireTime());
87     }
88
89 }
90
Popular Tags