Java Web项目中的定时器实现

在Java的6.0版本中新增了concurrent包,这个包是用作线程同步的包,提供了很多工具类。 
其中定时器就是其中之一。近期在做一个项目,在项目中要实现启动Web服务的同时启动一个任务,要求此任务为守护进程,定时调用一些任务操作代码。在网上搜索相关的实现,综合考虑得出一个解决方案。方案是:使用tomcat服务器的监听器类作守护进程,调用concurrent包提供的定时器,最后在定时器中调用操作实现代码。具体实现类Translator在这里不再给出,将其替换为自己需要的实现类即可。 

定时器类: 

package me.ele.mercuris.svr.web.utils;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.inject.Named;
import me.ele.elog.Log;
import me.ele.elog.LogFactory;
import me.ele.mercuris.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("CheckChargeTimer")
public class CheckChargeTimer {

	private final static Log LOGGER = LogFactory.getLog(CheckChargeTimer.class);
	private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

	@Autowired(required = true)
	@Named("paymentService")
	private PaymentService paymentService;

	public void executeTranslateTimer() {

		Runnable task = new Runnable() {

			public void run() {
				LOGGER.info("开始执行Timer");

				try {
					// ClassPathXmlApplicationContext
					// classPathXmlApplicationContext = new
					// ClassPathXmlApplicationContext("application-context.xml");
					// paymentService =
					// classPathXmlApplicationContext.getBean(PaymentService.class);
					String str = paymentService.test();

					System.out.println(str);
				} catch (Exception e) {
					LOGGER.error(e);
				}

				LOGGER.info("执行Timer成功");
			}
		};
		if (scheduler.isShutdown()) {
			scheduler = Executors.newScheduledThreadPool(1);
			scheduler.scheduleWithFixedDelay(task, 1, 30, TimeUnit.SECONDS);
		} else {
			scheduler.scheduleWithFixedDelay(task, 1, 30, TimeUnit.SECONDS); // 延迟1秒,每隔30秒一次
		}
	}

	// 停止任务,不再提交新任务,已提交任务会继续执行以致完成
	public void stop() {
		scheduler.shutdown();
	}
}



监听器类: 
<pre name="code" class="java">package me.ele.mercuris.svr.web.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import me.ele.mercuris.svr.web.utils.CheckChargeTimer;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class StartCheckChargeListener implements ServletContextListener {

	public StartCheckChargeListener() {
		super();
	}

	public void contextDestroyed(ServletContextEvent arg0) {

	}

	public void contextInitialized(ServletContextEvent e) {
		System.out.println("-------------StartAutoStopListener.init-------------");
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(e.getServletContext());
		CheckChargeTimer checkChargeTimer = (CheckChargeTimer) ac.getBean("CheckChargeTimer");
		checkChargeTimer.executeTranslateTimer();

	}
}


 

 

在Java Web项目的web.xml文件中加入以下几行,注意包路径,向tomcat服务器声明监听器。 
  <listener>  
    <listener-class>com.caiex.servlet.StartAutoStopListener</listener-class>  
</listener>  



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值