public class AutogetTask extends HttpServlet {
private static Fpf_webservice dialogClient= new Fpf_webservice();
public void init() throws ServletException {
new AutogetTask();
}
private Timer t = new Timer();
private class MyTask extends TimerTask {
long period;
String workday="";
String holiday="";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();{
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("WebRoot/phoneconfg.xml");
Element root = doc.getDocumentElement();
NodeList nl = root.getElementsByTagName("times");
Element e = (Element) nl.item(0);
String times=e.getTextContent();
int time = Integer.parseInt(times);
period = 1000 * 60 * time;
NodeList wor = root.getElementsByTagName("workday");
Element work = (Element) wor.item(0);
workday=work.getTextContent();
NodeList hol = root.getElementsByTagName("holiday");
Element holi = (Element) hol.item(0);
holiday=holi.getTextContent();
}catch(Exception e){
e.printStackTrace();
}
}
public void run() {
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time=dateFormat.format(c.getTime());
String[] workdays=workday.split(",");
String[] holidays=holiday.split(",");
for(int i=0;i<holidays.length;i++)
{
if(holidays[i].equals(time))
{
return;
}
}
if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||c.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
{
for(int i=0;i<workdays.length;i++)
{
if(workdays[i].equals(time))
{
if (c.get(Calendar.HOUR_OF_DAY) >= 17) {
return;
}
dialogClient.getqueue();
t.schedule(new MyTask(), period);
}
}
}else{
if (c.get(Calendar.HOUR_OF_DAY) >= 17) {
return;
}
dialogClient.getqueue();
t.schedule(new MyTask(), period);
}
}
}
public AutogetTask() {
t.schedule(new MyTask(), getTime(8), 1000 * 60 * 60 * 24);
}
private Date getTime(int beginHour) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, c.get(Calendar.DATE) );
c.set(Calendar.HOUR_OF_DAY, beginHour);
c.clear(Calendar.MINUTE);
c.clear(Calendar.SECOND);
return c.getTime();
}
}
运行结果:
2015-10-26 10:59:40,246 INFO JdbcDao:228 - sql语句:[insert into weixin_number(get_date,get_number)values(?,?)]
2015-10-26 10:59:40,248 INFO JdbcDao:228 - sql语句:[insert into weixin_number(get_date,get_number)values(?,?)]
2015-10-26 10:59:40,247 INFO JdbcDao:228 - sql语句:[insert into weixin_number(get_date,get_number)values(?,?)]
2015-10-26 10:59:40,251 INFO JdbcDao:232 - 参数个数为:[2]
2015-10-26 10:59:40,250 INFO JdbcDao:232 - 参数个数为:[2]
2015-10-26 10:59:40,253 INFO JdbcDao:288 - 参数[0]:st=2015-10-26 10:59
2015-10-26 10:59:40,252 INFO JdbcDao:232 - 参数个数为:[2]
2015-10-26 10:59:40,256 INFO JdbcDao:288 - 参数[0]:st=2015-10-26 10:59
2015-10-26 10:59:40,255 INFO JdbcDao:288 - 参数[1]:number=0
2015-10-26 10:59:40,254 INFO JdbcDao:288 - 参数[0]:st=2015-10-26 10:59
2015-10-26 10:59:40,258 INFO JdbcDao:288 - 参数[1]:number=0
2015-10-26 10:59:40,257 INFO JdbcDao:288 - 参数[1]:number=0
2015-10-26 10:59:40,853 INFO JdbcDao:228 - sql语句:[insert into weixin_number(get_date,get_number)values(?,?)]
2015-10-26 10:59:40,853 INFO JdbcDao:232 - 参数个数为:[2]
2015-10-26 10:59:40,853 INFO JdbcDao:288 - 参数[0]:st=2015-10-26 10:59
2015-10-26 10:59:40,853 INFO JdbcDao:288 - 参数[1]:number=0
可以看到服务启动就运行了4次,执行了4次相同的语句,这是什么情况?
------解决思路----------------------
调试看看啊。