KickJava   Java API By Example, From Geeks To Geeks.

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


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

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