1 package org.springframework.samples.imagedb.scheduling; 2 3 import java.util.List ; 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 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 mailFrom; 42 43 private String 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 mailFrom) { 54 this.mailFrom = mailFrom; 55 } 56 57 public void setMailTo(String 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 images = this.imageDatabase.getImages(); 64 String [] imageNames = new String [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 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 |