Spring QuartzJobBean example


      Click here to attend Spring Framework 4.x and certification course with a discount

package nl.kameroom.emailservice;

import java.util.HashMap;
import java.util.Map;

import nl.kameroom.advert.AdvertManager;
import nl.kameroom.advert.AdvertPublished;
import nl.kameroom.user.UserManager;
import nl.kameroom.util.LocaleMessageSource;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class AdvertPublishedJob extends QuartzJobBean {

 private LocaleMessageSource localeMessageSource;
 private AdvertManager advertManager;
 private EmailService emailService;
 private UserManager userManager;
 
 public void setAdvertManager(AdvertManager am) {
 advertManager = am;
 }
 
 public void setLocaleMessageSource(LocaleMessageSource ms) {
 localeMessageSource = ms;
 }
 
 public void setEmailService(EmailService es) {
 emailService = es;
 }
 
 public void setUserManager(UserManager um) {
 userManager = um;
 }
 
 @Override
 protected void executeInternal(JobExecutionContext arg0)
 throws JobExecutionException {
 
 //ROOM ADVERTS
 for (AdvertPublished advertRoomPublished : advertManager.getRoomAdvertsPublished()) {
 sendRoomNotification(advertRoomPublished);
 advertManager.updateEmailSentAndTime(Integer.valueOf(advertRoomPublished.getIdadvert()));
 }
 
 //ROOM WANTED ADVERTS
 for (AdvertPublished advertRoomWantedPublished: advertManager.getRoomWantedAdvertPublished()) {
 sendRoomWantedNotification(advertRoomWantedPublished);
 advertManager.updateEmailSentAndTime(Integer.valueOf(advertRoomWantedPublished.getIdadvert()));
 }
 
 
 }

 public void sendRoomNotification (AdvertPublished apub) {
 
 Map<String, Object> model1 = new HashMap<String, Object>();
 model1.put("from", "norepy@kameroom.nl"); 
 model1.put("subject", "Room Advert published @kameroom.nl " + apub.getStreetname()); //CHANGE
 model1.put("to", apub.getEmail()); //Attention 
 model1.put("place", apub.getCityname()); 
 model1.put("postcode", apub.getPostcode()); 
 model1.put("street", apub.getStreetname()); 
 model1.put("housenum", apub.getHousenum()); 
 model1.put("rent", apub.getRent()); 
 model1.put("id", apub.getIdadvert());
 model1.put("name", apub.getName());
 model1.put("nurl", "http://www.kameroom.nl/navigate.k?email="+apub.getEmail()+"&k="+userManager.getReminderUserPassKey(apub.getEmail())+"&p=viewradvert&id="+apub.getIdadvert()+"&language="+apub.getLanguage());
 boolean result = false;
 if(apub.getLanguage().equals("EN")) {
 result = emailService.send("roompublished.vm", model1); } 
 else {
 result = emailService.send("roompublished_nl.vm", model1); 
 }
 if(result == true)
 {
 //logging
 } 
 
 }
 public void sendRoomWantedNotification (AdvertPublished apub) {
 
 Map<String, Object> model1 = new HashMap<String, Object>();
 model1.put("from", "noreply@kameroom.nl"); 
 model1.put("subject", "Room Wanted Advert published @kameroom.nl " + apub.getCityname()); //CHANGE
 model1.put("to", apub.getEmail()); //Attention 
 model1.put("place", apub.getCityname()); 
 model1.put("name", apub.getName());
 model1.put("id", apub.getIdadvert());
 model1.put("minrent", apub.getMinrent());
 if(apub.getMinrent().equals(apub.getMaxrent()))
 model1.put("maxrent", "2000+");
 else
 model1.put("maxrent", apub.getMaxrent());
 model1.put("nurl", "http://www.kameroom.nl/navigate.k?email="+apub.getEmail()+"&k="+userManager.getReminderUserPassKey(apub.getEmail())+"&p=viewrwadvert&id="+apub.getIdadvert()+"&language="+apub.getLanguage());
 boolean result = false;
 if(apub.getLanguage().equals("EN")) {
 result = emailService.send("roomwantedpublished.vm", model1); } 
 else {
 result = emailService.send("roomwantedpublished_nl.vm", model1); 
 }
 if(result == true){
 //logging
 } 
 
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s