import java.util.Calendar;
import java.util.Date;
/**
* A DailyIterator class returns a sequence of dates on subsequent days
* representing the same time each day.
*/
public class DailyIterator implements ScheduleIterator {
// private int dayOfWeek = 0;
// private int hourOfDay = 0;
// private int minute = 0;
// private int second = 0;
private final Calendar calendar = Calendar.getInstance();
public DailyIterator(int dayOfWeek,int hourOfDay, int minute, int second) {
this(dayOfWeek,hourOfDay, minute, second, new Date());
}
public DailyIterator(int dayOfMonth,int hourOfDay, int minute, int second, Date date) {
// this.dayOfWeek = dayOfWeek;
// this.hourOfDay = hourOfDay;
// this.minute = minute;
// this.second = second;
calendar.setTime(date);
// calendar.set(Calendar.DAY_OF_WEEK, dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, 0);
if (!calendar.getTime().before(date)) {
calendar.add(Calendar.DATE, -1);
}
}
public Date next() {
calendar.add(Calendar.DATE, 1);
return calendar.getTime();
}
}
public class ExecuteTimerTask {
private static final Logger logger = Logger.getRootLogger();
private final Scheduler scheduler = new Scheduler();
private final SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd MMM yyyy HH:mm:ss.SSS");
private int dayOfMonth = 0;
private int hourOfDay = 0;
private int minute = 0;
private int second = 0;
private static ApplicationContext context = null;
private int siteId = 10;
private SiteCategoryDao siteCategoryDao = (SiteCategoryDao)(context.getBean("siteCategoryDao"));
static{
context = new ClassPathXmlApplicationContext(
new String[] { "/spring-bean.xml", "/spring-dao.xml", });
logger.info("------> init program .....");
}
public ExecuteTimerTask(int dayOfWeek,int hourOfDay, int minute, int second) {
this.dayOfMonth = dayOfWeek;
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
}
public void start() {
scheduler.schedule(new SchedulerTask() {
public void run() {
// Start a new thread to sound an alarm...
try {
excuteTimerTask();
} catch (Exception e) {
e.printStackTrace();
}
}
private void excuteTimerTask() {
logger.info("Wake up! " + "It"s; " + dateFormat.format(new Date()));
//每天把当前的商品价格加入到库中
try {
TreeNode[] treeNodes;
String url1="http://www.suning.cn/";
String anResult="";
String finalReString="";
String d ="";
try {
URL b=new URL(url1);
URLConnection urlConnection = b.openConnection();
urlConnection.setReadTimeout(10000);
InputStream inputStream =urlConnection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String rString =in.readLine();
while (rString!=null) {
anResult+=rString;
rString=in.readLine();
}
anResult=anResult.trim();
} catch (Exception e) {
}
Parser parser = Parser.createParser(anResult, "utf-8");
NodeFilter filter = new HasAttributeFilter("id", "SNmenuNav");;
NodeList children = parser.extractAllNodesThatMatch(filter);
NodeList dlList = children.elementAt(0).getChildren();
int count =0;
siteCategoryDao.clearSiteCategories(siteId);
for (int i = 0; i < dlList.size(); i++) {
Node node = dlList.elementAt(i);
if (node.getText().equals("dl")) {
getallLink(node,i);//传递一级目录的
}
}
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
}
}
}, new DailyIterator(dayOfMonth,hourOfDay, minute, second));
}
int nextid=0;//标志二级目录ID
int firstid=0;//标志一级目录ID
public TreeNode getallLink(Node d,int n){
if (d.getText().indexOf("dl")>=0) {
//System.out.println("1");
}else if (d.getText().indexOf("dt")>=0) {
getallLink(d.getChildren().elementAt(0),100);
return null;
}else if (d.getText().indexOf("dd")>=0) {
//System.out.println("12");
}else if (d.getText().indexOf("ul class=\"sideleft\"")>=0) {
//System.out.println("121");
}else if (d.getText().indexOf("li")>=0) {
if(d.getChildren().size()>1){
SiteCategoryDto siteCategoryDto =new SiteCategoryDto();
//siteCategoryDto.setCategoryName(((LinkTag) d).getLinkText());
System.out.println("二级目录:"+d.getChildren().elementAt(2).getText());
siteCategoryDto.setCategoryName(d.getChildren().elementAt(2).getText());
siteCategoryDto.setCategoryUrl("#");
siteCategoryDto.setSiteId(siteId);
siteCategoryDto.setParentCategoryId(firstid);
siteCategoryDto.setCategoryLevel(2);
siteCategoryDto.setFetchSize(0);
siteCategoryDao.addSiteCategory(siteCategoryDto);
nextid = siteCategoryDao.getMaxId();
}
//System.out.println("二级目录:"+d.getChildren().elementAt(2).getText());}
//System.out.println("1211");
}
// else if (d.getText().equals("b")) {
// //System.out.println("二级目录:"+d.toHtml());
// return null;
// }
else if (d.getText().equals("div")) {
//System.out.println("12111");
}
else if (d.getText().indexOf("a href=")>=0) {
SiteCategoryDto siteCategoryDto =new SiteCategoryDto();
siteCategoryDto.setCategoryName(((LinkTag) d).getLinkText());
siteCategoryDto.setSiteId(siteId);
if (n==100) {
siteCategoryDto.setParentCategoryId(0);
siteCategoryDto.setCategoryLevel(1);
siteCategoryDto.setCategoryUrl("http://www.suning.cn"+((LinkTag) d).getLink());
siteCategoryDto.setFetchSize(0);
siteCategoryDao.addSiteCategory(siteCategoryDto);
firstid = siteCategoryDao.getMaxId();
//System.out.println("121111");
//System.out.println("链接是:"+((LinkTag) d).getLink()+"一级级目录:"+((LinkTag) d).getLinkText());
} else {
//System.out.println("121111");
siteCategoryDto.setParentCategoryId(nextid);
siteCategoryDto.setCategoryLevel(3);
siteCategoryDto.setCategoryUrl("http://www.suning.cn"+((LinkTag) d).getLink());
siteCategoryDto.setFetchSize(0);
siteCategoryDao.addSiteCategory(siteCategoryDto);
//System.out.println("链接是:"+((LinkTag) d).getLink()+"三级目录:"+((LinkTag) d).getLinkText());
}
}else{
//System.out.println(d.getText());
return null;
}
NodeList dlist = d.getChildren();
for (int i = 0; i < dlist.size(); i++) {
getallLink(dlist.elementAt(i),n);
}
return null;
}
public static void main(String[] args) {
logger.info("daily price backup task start !*********** ");
//每天凌晨一点半触发 new ExecuteTimerTask(Calendar.HOUR_OF_DAY,1, 30,0);
ExecuteTimerTask timerTask = new ExecuteTimerTask(Calendar.HOUR_OF_DAY,16, 58,0);
timerTask.start();
}
}
import java.util.Date;
public interface ScheduleIterator {
public Date next();
}
package com.yihaodian.pis.timer;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Scheduler {
class SchedulerTimerTask extends TimerTask {
private SchedulerTask schedulerTask;
private ScheduleIterator iterator;
public SchedulerTimerTask(SchedulerTask schedulerTask,
ScheduleIterator iterator) {
this.schedulerTask = schedulerTask;
this.iterator = iterator;
}
public void run() {
schedulerTask.run();
reschedule(schedulerTask, iterator);
}
}
private final Timer timer = new Timer();
public Scheduler() {
}
public void cancel() {
timer.cancel();
}
public void schedule(SchedulerTask schedulerTask, ScheduleIterator iterator) {
Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
} else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.VIRGIN) {
throw new IllegalStateException("Task already scheduled "
+ "or cancelled");
}
schedulerTask.state = SchedulerTask.SCHEDULED;
schedulerTask.timerTask = new SchedulerTimerTask(schedulerTask,
iterator);
timer.schedule(schedulerTask.timerTask, time);
}
}
}
private void reschedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {
Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
} else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.CANCELLED) {
schedulerTask.timerTask = new SchedulerTimerTask(
schedulerTask, iterator);
timer.schedule(schedulerTask.timerTask, time);
}
}
}
}
}
package com.yihaodian.pis.timer;
import java.util.TimerTask;
public abstract class SchedulerTask implements Runnable {
final Object lock = new Object();
int state = VIRGIN;
static final int VIRGIN = 0;
static final int SCHEDULED = 1;
static final int CANCELLED = 2;
TimerTask timerTask;
protected SchedulerTask() {
}
public abstract void run();
public boolean cancel() {
synchronized (lock) {
if (timerTask != null) {
timerTask.cancel();
}
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}
public long scheduledExecutionTime() {
synchronized (lock) {
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
}
}
}
如果要改成在每周的星期六早上6点执行,可做如下修改
//设置在每周的星期六早上六点执行,注意WEEK_OF_MONTH和一个月每周的区别,WEEK_OF_MONTH是按天数分周,一个月每周是从星期日到星期六
ExecuteTimerTask timerTask = new ExecuteTimerTask(Calendar.WEEK_OF_MONTH,7,6,0,0);
public DailyIterator(int weekOfMonth, int dayOfWeek, int hour, int minute, int second, Date date) {
// this.dayOfWeek = dayOfWeek;
// this.hourOfDay = hourOfDay;
// this.minute = minute;
// this.second = second;
calendar.setTime(date);
calendar.set(Calendar.WEEK_OF_MONTH, weekOfMonth);
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, 0);
System.out.println(calendar.getTime());
System.out.println(date);
if (!calendar.getTime().before(date)) {
calendar.add(Calendar.DATE, -1);
}
}
其他的修改可按照上面一次类推