在application-quartz.xml配置文件中添加如下配置信息:
<!-- Quartz -->
<bean id="getSendEmailObject" class="com.luguang.baseinfo.util.SendEmailJob">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="getSendEmailtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="getSendEmailObject"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>taskjob</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="getSendEmailTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="getSendEmailtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 0 9 * * ?</value>
<!-- <value>0 19 45 ? * * </value>-->
</property>
</bean>
对应的调用类如下:
package com.luguang.baseinfo.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Transient;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.luguang.model.LgmUser;
import com.luguang.product.service.LgpProductLicenseService;
import com.luguang.project.service.LgpLicenseService;
public class SendEmailJob {
private SessionFactory sessionFactory;
private LgpLicenseService lgpLicenseService;
private LgpProductLicenseService lgpProductLicenseService;
public LgpProductLicenseService getLgpProductLicenseService() {
return lgpProductLicenseService;
}
@Autowired
public void setLgpProductLicenseService(
LgpProductLicenseService lgpProductLicenseService) {
this.lgpProductLicenseService = lgpProductLicenseService;
}
public LgpLicenseService getLgpLicenseService() {
return lgpLicenseService;
}
@Autowired
public void setLgpLicenseService(LgpLicenseService lgpLicenseService) {
this.lgpLicenseService = lgpLicenseService;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SendEmailJob() {
}
@Transient
public void taskjob() throws JobExecutionException {
lgpLicenseService.sendEmail();
lgpProductLicenseService.sendEmail();
}
// 判断传入对象是否是空字符串
public boolean isNullOrEmptyString(Object o) {
if (o == null) {
return true;
}
if (o instanceof String) {
String str = (String) o;
if (str.length() == 0) {
return true;
}
}
return false;
}
/**判断是否是节假日
* @param checkDay
* @param isWeekend 1是周末 0不是周末
* @return true为是 ,否则为否
*/
public boolean checkDayIsHoliday(Session session,Date checkDay)
{
int isWeekend = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(checkDay);
if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7) {
isWeekend = 1;
}
String hql = "SELECT a FROM LgpHoliday a WHERE to_char(a.startDate,'YYYY-MM-DD') <= '" + new SimpleDateFormat("yyyy-MM-dd").format(checkDay) + "' "
+ " and to_char(a.endDate,'YYYY-MM-DD') >= '" + new SimpleDateFormat("yyyy-MM-dd").format(checkDay) + "' and a.isActive = '0' ";
if (isWeekend == 0) {
hql += " and a.holidayType != '2' ";
}
if (isWeekend == 1) {
hql += " and a.holidayType = '2' ";
}
List list = session.createQuery(hql).list();
if (isWeekend == 1 && list.size() > 0) {
return false;
}
if (isWeekend == 0 && list.size() == 0) {
return false;
}
return true;
}
}
service层代码如下:
public String sendEmail(){
return lgpProductLicenseDao.sendEmail();
}
dao层代码如下:
/*
* 发送电子邮件
*/
public String sendEmail(){
Date now=new Date();
EmailForProduct emailForProduct;
String hql="select new com.lg.product.model.EmailForProduct(a.productName,b.lgpLicenseId,c.userAlias,d.mailAddress,b.endDate,b.remindDays) from LgpProduct as a,LgpLicense as b,LgmUser as c,LgmUserIncrement as d where 1=1"
+" and a.lgpProductId=b.lgpProductId"
+" and a.productLeader=c.userId"
+" and c.userId=d.userId";
List list=this.getHibernateTemplate().find(hql);
if(list!=null&&list.size()>0){
for(int i=0;i<list.size();i++){
emailForProduct=(EmailForProduct)list.get(i);
long interval=(emailForProduct.getEndDate().getTime()-now.getTime())/1000;
if(interval<emailForProduct.getRemindDays().longValue()*24*60*60){
this.sendEmail(emailForProduct.getProductName(), emailForProduct.getEmailAddress());
}
}
}
return null;
}
public void sendEmail(String productName ,String emailAddress){
SimpleEmail email=new SimpleEmail();
email.setHostName("smtp.126.com");
email.setAuthentication("name", "password");
email.setCharset("UTF-8");
try {
email.addTo(emailAddress);
email.setFrom(name@server.com);
email.setSubject("subject");
email.setMsg("msg");
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
实现邮件的定时发送,通过在程序中判断邮件发送时间来控制邮件发送,避免在配置文件中设定动态的邮件发送时间(配置文件中获得动态参数是非常麻烦的,尽量在程序中实现)。