我有两个服务,其中一个是触发邮件的邮件服务,我正在使用Quartz Scheduler来调度作业,因此当我尝试自动连接dao类(将所有数据库记录都检索到邮件服务)时,它会抛出一个错误。空指针异常。我在其他服务中调用相同的dao方法,并且可以正常工作。谁能给我解决办法
@Service("mailService")
public class MailServiceImpl implements MailService{
static Logger logger = Logger.getLogger(MailServiceImpl.class);
@Autowired
ReleaseScheduleDaoImpl releaseScheduleDao ;
@Override
public void sendEmail(String from, String to, String subject, String content){
//PropertyConfigurator.configure("log4j.properties");
System.out.println("Inside sendEmail()");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"sureshnambiar.c@gmail.com", "Ammaammus");
}
});
session.setDebug(true);
Transport transport = session.getTransport();
InternetAddress addressFrom = new InternetAddress(from);
MimeMessage message = new MimeMessage(session);
message.setSender(addressFrom);
message.setSubject(subject);
message.setContent(content, "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
transport.connect();
transport.send(message);
transport.close();
}catch (MessagingException mex){
logger.error(mex.getMessage());
}
}
// here I am thrown with an null pointer exception
@Override
public List<ReleaseScheduleFieldName> getAllReleaseScheduleFieldName() {
return releaseScheduleDao.getAllReleaseScheduleFieldName();
}
}
// my quartz scheduler execute method
public class ReleaseMailScheduler extends QuartzJobBean {
MailServiceImpl mailService = new MailServiceImpl();
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("inside execute");
try {
List<ReleaseScheduleFieldName> fieldList = mailService.getAllReleaseScheduleFieldName();
System.out.println("fieldList " + fieldList);
mailService.sendEmail("sureshnambiar.c@gmail.com", "sureshnambiar.c@gmail.com", "test Plain", "this is a test");
}catch (Exception e){
e.printStackTrace();
}
}
}
//my dao class
@Override
public List<ReleaseScheduleFieldName> getAllReleaseScheduleFieldName() {
session = this.sessionFactory.openSession();
List<ReleaseScheduleFieldName> fieldNames= new ArrayList<>();
try {
String sql = "from ReleaseScheduleFieldName rf ORDER BY rf.fieldNameId";
fieldNames = (List<ReleaseScheduleFieldName>) session.createQuery(sql).list();
}catch(Exception e){
logger.error(e.getMessage());
}finally {
session.close();
}
return fieldNames;
}
以下是我得到的例外情况
java.lang.NullPointerException
at com.cerner.release.Service.MailServiceImpl.getAllReleaseScheduleFieldName(MailServiceImpl.java:72)
at com.cerner.release.Scheduler.ReleaseMailScheduler.executeInternal(ReleaseMailScheduler.java:18)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)