最近在
架构系统的时候,其中用到一些交易是对交易的开关的控制,因为交易量较大,考虑不能是一直读取数据库,所以决定使用定时器把数据库的信息读到
内存然后调用。
但是为了可以在更改数据库里的信息后能即时的让定时器更新,暂时想到的是通过url接受到的参数。
import java.util.TimerTask;
/**
* 定时任务类
* @author Jack.
* @Time: 2011-10-12 11:31:21.
* @FileName: MyTimerTask.java.
* @Version 1.0
*/
public
class MyTimerTask extends TimerTask {
/**
* 判断任务是否在执行
*/
private static boolean isRunning = false;
/**
* 计数
*/
private static long count = 1;
/**
* 任务执行
*/
public void run() {
//TODO: 以下可添加相应的定时任务
if (!isRunning) {
isRunning = true;
System.out.println("定时任务执行到了第" + count + "次");
count++;
//TODO: Add any other things you want to do here.
isRunning = false;
} else {
System.out.println("定时任务已在执行中了...");
}
}
}
/**
*
*/
package TimerTest;
import java.util.Timer;
import javax.servlet.
ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 定时任务Servlet类
* @author Jack.
* @Time: 2011-10-12 11:36:01.
* @FileName: MyTimer.java.
* @Version 1.0
*/
public class MyTimerServlet extends HttpServlet implements ServletContextListener {
/**
*
*/
private static final long serialVersionUID = 5743600041518326783L;
/**
* 定时任务
*/
private static MyTimerTask task = new MyTimerTask();;
/**
* 定时器
*/
private static Timer timer = null;
/**
* Web服务器启动后执行
*/
public void contextInitialized(ServletContextEvent event) {
System.out.println("定时任务已启动");
timer = new Timer();
timer.schedule(task, 1000, 10 * 1000); //1000-表示启动后隔1秒才执行,10*1000表示每次执行的间隔为10秒.
}
/**
* Web服务器关闭后执行
*/
public void contextDestroyed(ServletContextEvent event) {
timer.
cancel();
System.out.println("定时任务已取消");
}
/**
*
* @param request
* @param response
* @param event
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
processRequest(request, response);
}
/**
*
* @param request
* @param response
* @param event
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
processRequest(request, response);
}
/**
* 通过
HTTP-Servlet控制定时器的开关.
* @param request
* @param response
* @param event
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
String action = request.getParameter("action");
if ("shutdown".equals(action)) {
timer.cancel();
this.getServletContext().log("定时任务已取消");
} else if ("start".equals(action)) {
timer.cancel();
timer = new Timer();
this.getServletContext().log("定时任务已启动");
timer.schedule(task, 1000, 10 * 1000);
}
}
}
在Web.xml中加入
<listener>
<listener-class>com.jack.timer.MyTimerServlet</listener-class>
</listener>
和
<servlet>
<servlet-name>MyTimer</servlet-name>
<servlet-class>com.jack.timer.MyTimerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyTimer</servlet-name>
<url-pattern>/myTimer</url-pattern>
</servlet-mapping>
就可以在web服务器启动时执行定时任务,并可通过web去控制定时,例如:http://localhost:8080/myTimer?action=shutdown