Java的Timer定时执行问题

Jackie_GP 2013-12-30 10:04:37
<listener>
<listener-class>com.jackie.train.util.MessageTaskListener</listener-class>
</listener>
想定时执行一个任务,在指定的时间点执行。
tomcat启动以后,以前配置的时间点,到了以后会执行。如果新增一个任务,或者修改了时间点,为什么不执行了呢?

怎么才能动态执行呢?
...全文
565 29 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
-天天好辛勤 2014-03-26
  • 打赏
  • 举报
回复
纯属猜测.....................
-天天好辛勤 2014-03-26
  • 打赏
  • 举报
回复
我只想说 Timer new出来后就是一个独立的对象了 , 你修改TimerManager的time属性并不能修改创建出来的timer对象的属性 简单的说 你更改 了时间 就要销毁旧的timer 对象 timer.cancel() 然后重新new Timer( ,新时间)
Jackie_GP 2014-01-08
  • 打赏
  • 举报
回复
引用 26 楼 huasuan26 的回复:
[quote=引用 25 楼 Jackie_GP 的回复:] [quote=引用 24 楼 huasuan26 的回复:] [quote=引用 22 楼 Jackie_GP 的回复:] [quote=引用 20 楼 huasuan26 的回复:]
package com.founder.ec.web.TimerTask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;//定时器类

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/****
 * 
 * 类描述: 定时每天几点钟执行一次
 * 
 * @author: Janc
 * @time:2013-9-28
 */
public class SysContextListener_Return implements ServletContextListener {
	private Timer timer = null;// 重写contextInitialized  

	public void contextInitialized(ServletContextEvent event) {
		// 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能  
		timer = new Timer(true);
		// 添加日志,可在tomcat日志中查看到  
		event.getServletContext().log("定时器已启动");
		
		//指定任务在晚上23点25分执行:
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 50);
		calendar.set(Calendar.SECOND, 00);
		Date time = calendar.getTime();
		timer = new Timer();
		timer.schedule(new Times_Return_Order(), time);
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 23);
		calendar1.set(Calendar.MINUTE, 55);
		calendar1.set(Calendar.SECOND, 00);
		Date time1 = calendar1.getTime();
		timer.schedule(new Times_Result_Xml(), time1);
	}

	// 重写contextDestroyed
	public void contextDestroyed(ServletContextEvent event) {
		// 在这里关闭监听器,所以在这里销毁定时器。        timer.cancel();  
		event.getServletContext().log("定时器销毁");
	}

}
package com.founder.ec.web.TimerTask;

import java.io.IOException;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;


/***
 * 定时器,定时每天几点钟执行一次
 * 
 * @author Janc
 * 
 */
@Controller
public class Times_Return_Order extends TimerTask {

	public void run() {
		// 此处编写任务内容,定时器暂时不执行内容,需要时开启
		System.out.println("this is Timer action");
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(
				"http://localhost:8080/ec-orderdc/page/returnOrder/acceptData");
		try {
			client.executeMethod(method);

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (method.getStatusCode() == HttpStatus.SC_OK) {
		}
		method.releaseConnection();

	}
}
这个方法根本就不行,我在后台,增加一个新纪录,或者修改一个已有的纪录的执行时间,根本就不能执行。[/quote] 这方法怎么可能不行,我现在项目就是用的这个 自己再调试调试[/quote] 方便加一下我的qq吗?330805206[/quote] 已经加你qq了[/quote] 哪个呀?
留下您的微笑 2014-01-06
  • 打赏
  • 举报
回复
引用 25 楼 Jackie_GP 的回复:
[quote=引用 24 楼 huasuan26 的回复:] [quote=引用 22 楼 Jackie_GP 的回复:] [quote=引用 20 楼 huasuan26 的回复:]
package com.founder.ec.web.TimerTask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;//定时器类

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/****
 * 
 * 类描述: 定时每天几点钟执行一次
 * 
 * @author: Janc
 * @time:2013-9-28
 */
public class SysContextListener_Return implements ServletContextListener {
	private Timer timer = null;// 重写contextInitialized  

	public void contextInitialized(ServletContextEvent event) {
		// 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能  
		timer = new Timer(true);
		// 添加日志,可在tomcat日志中查看到  
		event.getServletContext().log("定时器已启动");
		
		//指定任务在晚上23点25分执行:
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 50);
		calendar.set(Calendar.SECOND, 00);
		Date time = calendar.getTime();
		timer = new Timer();
		timer.schedule(new Times_Return_Order(), time);
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 23);
		calendar1.set(Calendar.MINUTE, 55);
		calendar1.set(Calendar.SECOND, 00);
		Date time1 = calendar1.getTime();
		timer.schedule(new Times_Result_Xml(), time1);
	}

	// 重写contextDestroyed
	public void contextDestroyed(ServletContextEvent event) {
		// 在这里关闭监听器,所以在这里销毁定时器。        timer.cancel();  
		event.getServletContext().log("定时器销毁");
	}

}
package com.founder.ec.web.TimerTask;

import java.io.IOException;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;


/***
 * 定时器,定时每天几点钟执行一次
 * 
 * @author Janc
 * 
 */
@Controller
public class Times_Return_Order extends TimerTask {

	public void run() {
		// 此处编写任务内容,定时器暂时不执行内容,需要时开启
		System.out.println("this is Timer action");
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(
				"http://localhost:8080/ec-orderdc/page/returnOrder/acceptData");
		try {
			client.executeMethod(method);

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (method.getStatusCode() == HttpStatus.SC_OK) {
		}
		method.releaseConnection();

	}
}
这个方法根本就不行,我在后台,增加一个新纪录,或者修改一个已有的纪录的执行时间,根本就不能执行。[/quote] 这方法怎么可能不行,我现在项目就是用的这个 自己再调试调试[/quote] 方便加一下我的qq吗?330805206[/quote] 已经加你qq了
Jackie_GP 2014-01-05
  • 打赏
  • 举报
回复
引用 24 楼 huasuan26 的回复:
[quote=引用 22 楼 Jackie_GP 的回复:] [quote=引用 20 楼 huasuan26 的回复:]
package com.founder.ec.web.TimerTask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;//定时器类

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/****
 * 
 * 类描述: 定时每天几点钟执行一次
 * 
 * @author: Janc
 * @time:2013-9-28
 */
public class SysContextListener_Return implements ServletContextListener {
	private Timer timer = null;// 重写contextInitialized  

	public void contextInitialized(ServletContextEvent event) {
		// 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能  
		timer = new Timer(true);
		// 添加日志,可在tomcat日志中查看到  
		event.getServletContext().log("定时器已启动");
		
		//指定任务在晚上23点25分执行:
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 50);
		calendar.set(Calendar.SECOND, 00);
		Date time = calendar.getTime();
		timer = new Timer();
		timer.schedule(new Times_Return_Order(), time);
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 23);
		calendar1.set(Calendar.MINUTE, 55);
		calendar1.set(Calendar.SECOND, 00);
		Date time1 = calendar1.getTime();
		timer.schedule(new Times_Result_Xml(), time1);
	}

	// 重写contextDestroyed
	public void contextDestroyed(ServletContextEvent event) {
		// 在这里关闭监听器,所以在这里销毁定时器。        timer.cancel();  
		event.getServletContext().log("定时器销毁");
	}

}
package com.founder.ec.web.TimerTask;

import java.io.IOException;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;


/***
 * 定时器,定时每天几点钟执行一次
 * 
 * @author Janc
 * 
 */
@Controller
public class Times_Return_Order extends TimerTask {

	public void run() {
		// 此处编写任务内容,定时器暂时不执行内容,需要时开启
		System.out.println("this is Timer action");
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(
				"http://localhost:8080/ec-orderdc/page/returnOrder/acceptData");
		try {
			client.executeMethod(method);

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (method.getStatusCode() == HttpStatus.SC_OK) {
		}
		method.releaseConnection();

	}
}
这个方法根本就不行,我在后台,增加一个新纪录,或者修改一个已有的纪录的执行时间,根本就不能执行。[/quote] 这方法怎么可能不行,我现在项目就是用的这个 自己再调试调试[/quote] 方便加一下我的qq吗?330805206
留下您的微笑 2014-01-03
  • 打赏
  • 举报
回复
引用 22 楼 Jackie_GP 的回复:
[quote=引用 20 楼 huasuan26 的回复:]
package com.founder.ec.web.TimerTask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;//定时器类

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/****
 * 
 * 类描述: 定时每天几点钟执行一次
 * 
 * @author: Janc
 * @time:2013-9-28
 */
public class SysContextListener_Return implements ServletContextListener {
	private Timer timer = null;// 重写contextInitialized  

	public void contextInitialized(ServletContextEvent event) {
		// 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能  
		timer = new Timer(true);
		// 添加日志,可在tomcat日志中查看到  
		event.getServletContext().log("定时器已启动");
		
		//指定任务在晚上23点25分执行:
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 50);
		calendar.set(Calendar.SECOND, 00);
		Date time = calendar.getTime();
		timer = new Timer();
		timer.schedule(new Times_Return_Order(), time);
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 23);
		calendar1.set(Calendar.MINUTE, 55);
		calendar1.set(Calendar.SECOND, 00);
		Date time1 = calendar1.getTime();
		timer.schedule(new Times_Result_Xml(), time1);
	}

	// 重写contextDestroyed
	public void contextDestroyed(ServletContextEvent event) {
		// 在这里关闭监听器,所以在这里销毁定时器。        timer.cancel();  
		event.getServletContext().log("定时器销毁");
	}

}
package com.founder.ec.web.TimerTask;

import java.io.IOException;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;


/***
 * 定时器,定时每天几点钟执行一次
 * 
 * @author Janc
 * 
 */
@Controller
public class Times_Return_Order extends TimerTask {

	public void run() {
		// 此处编写任务内容,定时器暂时不执行内容,需要时开启
		System.out.println("this is Timer action");
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(
				"http://localhost:8080/ec-orderdc/page/returnOrder/acceptData");
		try {
			client.executeMethod(method);

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (method.getStatusCode() == HttpStatus.SC_OK) {
		}
		method.releaseConnection();

	}
}
这个方法根本就不行,我在后台,增加一个新纪录,或者修改一个已有的纪录的执行时间,根本就不能执行。[/quote] 这方法怎么可能不行,我现在项目就是用的这个 自己再调试调试
Jackie_GP 2014-01-02
  • 打赏
  • 举报
回复
引用 18 楼 niit_java 的回复:
推荐一个spring quartz的job 框架 很好用
我知道这个框架,但是具体怎么执行我这样的任务,还不好弄。
paker_ma 2014-01-02
  • 打赏
  • 举报
回复
推荐一个spring quartz的job 框架 很好用
Jackie_GP 2014-01-02
  • 打赏
  • 举报
回复
引用 21 楼 huasuan26 的回复:
方法二不用timer
<?xml version="1.0" encoding="utf-8"?>
<beans default-init-method="init" default-destroy-method="destroy"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context 
		   http://www.springframework.org/schema/context/spring-context.xsd 
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task-3.1.xsd">
           
	<!-- Enables the Spring Task @Scheduled programming model -->  
   <task:executor id="executor" pool-size="5" />  
   <task:scheduler id="scheduler" pool-size="10" />  
   <task:annotation-driven executor="executor" scheduler="scheduler" /> 
   
   <context:annotation-config/>  
   <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
   
   <context:component-scan base-package="com.yundaex.gpd.task.impl"/>  

</beans>
@Component
public class DynamicReportTaskImpl implements DynamicReportTask {

	private Logger logger = Logger.getLogger(DynamicReportTask.class);
	
	@Autowired
	private DynamicStockService dynamicStockService;
	
	
	/*
	 * CRON表达式    含义 
	 " 0 0 12 * * ?"    每天中午十二点触发 
	 " 0 15 10 ? * *"    每天早上10:15触发 
	 " 0 15 10 * * ?"    每天早上10:15触发 
	 " 0 15 10 * * ? *"    每天早上10:15触发 
	 " 0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
	 " 0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
	 " 0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
	 " 0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
	 " 0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
	 " 0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
	 " 0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 
	 * */
//	@Scheduled(cron = "0/30 * *  * * ? ") //每10秒执行一次
	@Scheduled(cron = "0 59 23 ? * *")  //每天23:59分执行生成动态报表操作
	public void generateDynamicReport() {
		logger.info("库存动态报表任务启动!");
		DynamicStock dynamicStock = new DynamicStock();
		// 获得结果集合,依次set进去 new DynamicDetailStock()
		// TODO 创建今天的任务批次
		// TODO 创建该次生成报表结果集明细
		try {
			dynamicStockService.create(dynamicStock);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("库存动态报表任务结束!");
	}
}
这个方法不知道行不行,我下来试试。
Jackie_GP 2014-01-02
  • 打赏
  • 举报
回复
引用 20 楼 huasuan26 的回复:
package com.founder.ec.web.TimerTask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;//定时器类

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/****
 * 
 * 类描述: 定时每天几点钟执行一次
 * 
 * @author: Janc
 * @time:2013-9-28
 */
public class SysContextListener_Return implements ServletContextListener {
	private Timer timer = null;// 重写contextInitialized  

	public void contextInitialized(ServletContextEvent event) {
		// 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能  
		timer = new Timer(true);
		// 添加日志,可在tomcat日志中查看到  
		event.getServletContext().log("定时器已启动");
		
		//指定任务在晚上23点25分执行:
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 50);
		calendar.set(Calendar.SECOND, 00);
		Date time = calendar.getTime();
		timer = new Timer();
		timer.schedule(new Times_Return_Order(), time);
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 23);
		calendar1.set(Calendar.MINUTE, 55);
		calendar1.set(Calendar.SECOND, 00);
		Date time1 = calendar1.getTime();
		timer.schedule(new Times_Result_Xml(), time1);
	}

	// 重写contextDestroyed
	public void contextDestroyed(ServletContextEvent event) {
		// 在这里关闭监听器,所以在这里销毁定时器。        timer.cancel();  
		event.getServletContext().log("定时器销毁");
	}

}
package com.founder.ec.web.TimerTask;

import java.io.IOException;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;


/***
 * 定时器,定时每天几点钟执行一次
 * 
 * @author Janc
 * 
 */
@Controller
public class Times_Return_Order extends TimerTask {

	public void run() {
		// 此处编写任务内容,定时器暂时不执行内容,需要时开启
		System.out.println("this is Timer action");
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(
				"http://localhost:8080/ec-orderdc/page/returnOrder/acceptData");
		try {
			client.executeMethod(method);

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (method.getStatusCode() == HttpStatus.SC_OK) {
		}
		method.releaseConnection();

	}
}
这个方法根本就不行,我在后台,增加一个新纪录,或者修改一个已有的纪录的执行时间,根本就不能执行。
留下您的微笑 2014-01-02
  • 打赏
  • 举报
回复
方法二不用timer
<?xml version="1.0" encoding="utf-8"?>
<beans default-init-method="init" default-destroy-method="destroy"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context 
		   http://www.springframework.org/schema/context/spring-context.xsd 
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task-3.1.xsd">
           
	<!-- Enables the Spring Task @Scheduled programming model -->  
   <task:executor id="executor" pool-size="5" />  
   <task:scheduler id="scheduler" pool-size="10" />  
   <task:annotation-driven executor="executor" scheduler="scheduler" /> 
   
   <context:annotation-config/>  
   <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
   
   <context:component-scan base-package="com.yundaex.gpd.task.impl"/>  

</beans>
@Component
public class DynamicReportTaskImpl implements DynamicReportTask {

	private Logger logger = Logger.getLogger(DynamicReportTask.class);
	
	@Autowired
	private DynamicStockService dynamicStockService;
	
	
	/*
	 * CRON表达式    含义 
	 " 0 0 12 * * ?"    每天中午十二点触发 
	 " 0 15 10 ? * *"    每天早上10:15触发 
	 " 0 15 10 * * ?"    每天早上10:15触发 
	 " 0 15 10 * * ? *"    每天早上10:15触发 
	 " 0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
	 " 0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
	 " 0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
	 " 0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
	 " 0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
	 " 0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
	 " 0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 
	 * */
//	@Scheduled(cron = "0/30 * *  * * ? ") //每10秒执行一次
	@Scheduled(cron = "0 59 23 ? * *")  //每天23:59分执行生成动态报表操作
	public void generateDynamicReport() {
		logger.info("库存动态报表任务启动!");
		DynamicStock dynamicStock = new DynamicStock();
		// 获得结果集合,依次set进去 new DynamicDetailStock()
		// TODO 创建今天的任务批次
		// TODO 创建该次生成报表结果集明细
		try {
			dynamicStockService.create(dynamicStock);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("库存动态报表任务结束!");
	}
}
留下您的微笑 2014-01-02
  • 打赏
  • 举报
回复
package com.founder.ec.web.TimerTask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;//定时器类

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/****
 * 
 * 类描述: 定时每天几点钟执行一次
 * 
 * @author: Janc
 * @time:2013-9-28
 */
public class SysContextListener_Return implements ServletContextListener {
	private Timer timer = null;// 重写contextInitialized  

	public void contextInitialized(ServletContextEvent event) {
		// 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能  
		timer = new Timer(true);
		// 添加日志,可在tomcat日志中查看到  
		event.getServletContext().log("定时器已启动");
		
		//指定任务在晚上23点25分执行:
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 50);
		calendar.set(Calendar.SECOND, 00);
		Date time = calendar.getTime();
		timer = new Timer();
		timer.schedule(new Times_Return_Order(), time);
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 23);
		calendar1.set(Calendar.MINUTE, 55);
		calendar1.set(Calendar.SECOND, 00);
		Date time1 = calendar1.getTime();
		timer.schedule(new Times_Result_Xml(), time1);
	}

	// 重写contextDestroyed
	public void contextDestroyed(ServletContextEvent event) {
		// 在这里关闭监听器,所以在这里销毁定时器。        timer.cancel();  
		event.getServletContext().log("定时器销毁");
	}

}
package com.founder.ec.web.TimerTask;

import java.io.IOException;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;


/***
 * 定时器,定时每天几点钟执行一次
 * 
 * @author Janc
 * 
 */
@Controller
public class Times_Return_Order extends TimerTask {

	public void run() {
		// 此处编写任务内容,定时器暂时不执行内容,需要时开启
		System.out.println("this is Timer action");
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(
				"http://localhost:8080/ec-orderdc/page/returnOrder/acceptData");
		try {
			client.executeMethod(method);

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (method.getStatusCode() == HttpStatus.SC_OK) {
		}
		method.releaseConnection();

	}
}
Jackie_GP 2014-01-01
  • 打赏
  • 举报
回复
引用 15 楼 bill0605030109 的回复:
<listener>只有tomcat启动的时候才会调用吧,而且只调用一次。之后还想调用只能在重启了。你可以把时间配置写在一个文件或者数据库里,然后只需要修改文件或数据库就行了,timer每次都是从文件或数据库里读配置。手动启动的会啊也可以啊,创建一个jsp上面加个按钮之类的,然后到servlet里启动就行。不过那个timer必须是同一个才行。
我的就是在数据库设置时间的呀
  • 打赏
  • 举报
回复
<listener>只有tomcat启动的时候才会调用吧,而且只调用一次。之后还想调用只能在重启了。你可以把时间配置写在一个文件或者数据库里,然后只需要修改文件或数据库就行了,timer每次都是从文件或数据库里读配置。手动启动的会啊也可以啊,创建一个jsp上面加个按钮之类的,然后到servlet里启动就行。不过那个timer必须是同一个才行。
Jackie_GP 2014-01-01
  • 打赏
  • 举报
回复
新年第一天,需要高人帮我解决这个问题啊。
Jackie_GP 2014-01-01
  • 打赏
  • 举报
回复
public void updateObject(Train t) { trainDao.updateObject(t); sendMessage(t); logDao.Log("修改培训ID= " + t.getId() + " 名称 " + t.getTitle()); } 修改的操作也是不执行。
Jackie_GP 2014-01-01
  • 打赏
  • 举报
回复
定时器代码:

监听器加载的时候,配置任务,这个是执行的。代码如下


我现在在添加记录的时候,也加上定时器,但是不执行,代码如下:


真是郁闷,请高手帮忙呀。

YAOQINGGG 2013-12-30
  • 打赏
  • 举报
回复
在java里面,schedule(TimerTask task, Date firstTime, long period) 这个函数你用对了么 可以参看下这个帖子http://blog.csdn.net/yanmei_yao/article/details/8131282
Jackie_GP 2013-12-30
  • 打赏
  • 举报
回复
比如在这样的时间点去执行任务:2013-12-30 10:10,tomcat启动以后,到时间了他会执行。修改这个时间点以后,到那个时间以后,就不执行。
Jackie_GP 2013-12-30
  • 打赏
  • 举报
回复
用的是Java的Timer定时器。
加载更多回复(8)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧