标签:
package com.wanhua.thread;
//定时发送任务线程
import java.util.List;
import java.util.TimerTask;
import play.db.jpa.JPAPlugin;
import controllers.News;
import models.ChildrenNew;
import models.Log;
import models.New;
public class SendNewsTask extends TimerTask {
// 选择发送的子新闻
private List<ChildrenNew> childrenNews;
// 发送时间
private String issuesTime;
// 作者
private String author;
/**
* 构造函数
*
* @param childrenNews
*/
public SendNewsTask(String author, String issuesTime, List<ChildrenNew> childrenNews) {
this.author = author;
this.issuesTime = issuesTime;
this.childrenNews = childrenNews;
}
@Override
public void run() {
// 选择发送的子新闻
List<ChildrenNew> childrenNews = this.childrenNews;
String issuesTime = this.issuesTime;
String author = this.author;
// 添加JPA环境
JPAPlugin.startTx(false);
// 发送新闻任务
New.sendNewsTask(author, issuesTime, childrenNews);
JPAPlugin.closeTx(false);
}
}
// 定时任务线程的调用,实现定时发送新闻通知
Timestamp publishTime = PrefUtil.forTimestamp(issuesTime);
//定时发送的某个时刻
Date sendDate = publishTime;
Timer timer = new Timer();
try {
// 定时发送新闻通知
timer.schedule(new SendNewsTask(author, issuesTime, childrenNews), sendDate); // 一个特定时间后运行
} catch (Exception e) {
// Timer与TimerTask是独立的两个物件,当使用Timer的schedule()方法排定TimerTask之后,
// 则必须等执行Timer的cancel()执行之后,让TimerTask与Timer脱离关系
timer.cancel();
}
标签:
原文地址:http://www.cnblogs.com/xunfang123/p/4195935.html