I am trying to send an email from a class that implements Quartz Job, in order to do that I have to @Autowire the IEmailService inside the class.
Here is the method I use to create a Quartz Job:
@Override
public Boolean sendInfoEmail(ManifestationProp manifProp, ServletRequest request) throws SchedulerException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String token = httpRequest.getHeader("jwt_token");
if(token == null) {
System.out.println("(ManifestationPropDaoImp) - TOKEN NULL");
return false;
}
String email = tokenUtils.getUsernameFromToken(token);
User user = userDao.findUserByEmail(email);
if(update(manifProp) != null) {
Scheduler sc = StdSchedulerFactory.getDefaultScheduler();
sc.start();
JobKey jobKey = new JobKey("Job_"+manifProp.getId(),"group1");
if(!sc.checkExists(jobKey)) { System.out.println("-----------------------------------------------");
System.out.println("Scheduling a Job for Manifestation Prop with ID - "+ manifProp.getId());
System.out.println("Current time - " + new Date());
System.out.println("Scheduled time - NOW" );
System.out.println("User - "+ user.getEmail());
System.out.println("Manifestation Prop - "+manifProp.getName());
JobDataMap jdm = new JobDataMap();
jdm.put("manifProp",manifProp);
jdm.put("user", user);
JobDetail jobDetail = JobBuilder.newJob(QuartzInformUser.class)
.withIdentity(jobKey)
.usingJobData(jdm)
.build();
Trigger t = TriggerBuilder.newTrigger().withIdentity("SimpleTrigger_"+manifProp.getId()).startNow().build();
sc.scheduleJob(jobDetail, t);
System.out.println("-----------------------------------------------");
}else {
System.out.println(" *** Job_"+manifProp.getId()+" already exists! *** ");
}
return true;
}else {
System.out.println("Could not update manifestation prop!");
}
return false;
}
Here is the code of the class which implements Job interface:
@Service
public class QuartzInformUser implements Job{
@Autowired
IEmailService emailService;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
JobDataMap dataMap = arg0.getJobDetail().getJobDataMap();
User user = (User)dataMap.get("user");
ManifestationProp manifProp = (ManifestationProp)dataMap.get("manifProp");
System.out.println("USER: "+user);
System.out.println("MANIFESTATION PROP: "+manifProp);
emailService.informUser(user,manifProp);
}catch(Exception e){
e.printStackTrace();
}
}
}
Quartz Job gets created perfectly fine, the problem is in the class QuartzInformUser
. Spring does not inject IEmailService
into the class, therefore the field emailService
is null
and i get the following exception:
If anyone has any idea how can i fix this issue I would really appreciate the help! Thank you in advance!