本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发。。

只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果。。

实习立即就要结束了。总结一下几点之前没有注意过的变成习惯和问题,分享给大家。

同一时候打个广告:去哪网内审部招JavaWeb开发实习生。时间很自由,每周一天、周六周日甚至都能够。时间充裕的小伙伴给我留言啊,挣个零花钱,还能长点经验。。

。。(保研的、想工作的大四狗最合适只是了。

。。)

事实上定时任务非常easy。js事实上也做过。就是Timer类的 Timer.schedule(TimerTask task, Date time, long period)方法而已,三个參数各自是:任务、延迟、间隔。

给个完整的代码:

首先是BugXmlTimer类:

public class BugXmlTimer  {
public Timer timer; public void timerStart(){
timer = new Timer();
Date datetime=new Date();
Date midnightDate=new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
midnightDate = sdf2.parse(sdf1.format(datetime)+" 10:06:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("before task");
long time = midnightDate.getTime()-datetime.getTime();
//立马运行,然后每隔10s运行一次
timer.schedule(new BugXmlTimerTask(), 0, 10000);//time
} public void timerStop(){
if(timer!=null)
timer.cancel();
} public static void main(String[] args){
BugXmlTimer myTimer=new BugXmlTimer();
// TODO Auto-generated method stub
myTimer.timerStart();
}
}

其次是TimerTask类:

<pre name="code" class="java">public class BugXmlTimerTask extends TimerTask {
@Override
public void run() {
System.out.print("run task");
try {
<strong>sendMail();//下一篇博客教你发送邮件</strong>
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

最后加入Listener类:

public class MyTimerListener implements ServletContextListener {

<span style="white-space:pre">	</span>private BugXmlTimer  mytimer = new BugXmlTimer  ();
public void contextInitialized(ServletContextEvent event) {
mytimer.timerStart();
}
public void contextDestroyed(ServletContextEvent event) {
mytimer.timerStop();
} }

最后不要忘了配置web.xml的listener节点:

<listener>
<listener-class>com.TimeListener.MyTimerListener</listener-class>
</listener>
05-28 07:48