1.
package com.xiaowei.birthday;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.xiaowei.domain.BirthdayPerson;
import com.xiaowei.domain.Person;
import com.xiaowei.email.MailUtils;
import com.xiaowei.service.BirthdayService;
//1.实现ServletContextListener接口
public class BirthdayListener implements ServletContextListener {
// 2.重写这个接口的方法
// 3.在web.xml文件中注册
//启动
public void contextInitialized(ServletContextEvent sce) {
// 当web应用启动 开启任务调动---功能在用户的生日当前发送邮件
// 4.开启一个定时器
Timer time = new Timer();
/*
* task:一个任务
* firstTime:开发中起始时间是一个固定的时间
* period:开发中间隔时间是1天
*/
time.scheduleAtFixedRate(new TimerTask() {
public void run() {
// 5.为当前过生日的用户发送邮件
// 5.1 获取今天谁过生日
// 今天的日期 将今天的日期转成字符串
SimpleDateFormat format = new SimpleDateFormat("MM-dd");
String currentTime = format.format(new Date());
// 6.将今天的日期传到service层 获取过生日的人的集合
BirthdayService service = new BirthdayService();
List<BirthdayPerson> birthdayPersonList = null;
try {
birthdayPersonList = service.findBirthdayList(currentTime);
} catch (SQLException e) {
e.printStackTrace();
}
// 11.向集合中的这些人发邮件
if (birthdayPersonList != null && birthdayPersonList.size() > 0) {
for (BirthdayPerson person : birthdayPersonList) {
String message = "hello"+person.getUsername()+" happy birthday";
try {
MailUtils.sendMail(person.getEmail(), "生日提醒", message);
System.out.println(person.getRealname()+"的邮件已经发送完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}, new Date(), 1000*60*60*24);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
1.1web.xml文件中的配置
<listener>
<listener-class>com.xiaowei.birthday.BirthdayListener</listener-class>
</listener>
2.service层
package com.xiaowei.service;
import java.sql.SQLException;
import java.util.List;
import com.xiaowei.dao.BirthdayDao;
import com.xiaowei.domain.BirthdayPerson;
public class BirthdayService {
// 7.根据时间获取当前过生日的人的集合
public List findBirthdayList(String currentTime) throws SQLException {
BirthdayDao dao = new BirthdayDao();
List<BirthdayPerson> birthdayPersonList = dao.findBirthdayList(currentTime);
return birthdayPersonList;
}
}
3.dao层
package com.xiaowei.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.xiaowei.domain.BirthdayPerson;
import com.xiaowei.utils.DataSourceUtils;
public class BirthdayDao {
// 8.根据时间获取当前过生日的人的集合
public List findBirthdayList(String currentTime) throws SQLException {
// 9.导入连接Mysql dbutils c3p0的包 数据库连接池工具类 c3p0配置文件
// 10.创建queryrunner对象
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from person where birthday like ?";
List<BirthdayPerson> birthdayPersonList = runner.query(sql, new BeanListHandler<BirthdayPerson>(BirthdayPerson.class),"%"+currentTime+"%");
return birthdayPersonList;
}
}
4.实体类
package com.xiaowei.domain;
public class BirthdayPerson {
private int id;
private String username;
private String password;
private String realname;
private String birthday;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}