ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

java web监听程序

来源:网络整理     时间:2014-12-09     关键词:

本篇文章主要介绍了"java web监听程序",主要涉及到方面的内容,对于JSPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: java web程序中实现一个监听程序, 定时执行某项业务:1. 写好监听程序.2. 在web.xml中配置一个listener,这个listener中可以设...

java web程序中实现一个监听程序, 定时执行某项业务:

1. 写好监听程序.

2.  在web.xml中配置一个listener,这个listener中可以设置一些初始化参数, 根据业务需要.

监听程序:

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

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

import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 监听程序
 * @author yangx
 *
 */
public class SendParamListener implements ServletContextListener {
	private SendErrorService sendErrorService;

	private ParamThread paramThread;

	class ParamThread extends TimerTask {
		private boolean isStop = false;

		@Override
		public void run() {
			if (!isStop) {
				// 这里处理自己的业务
				System.out.println("线程执行...");
			}
		}

		public void stopThread(){
			isStop = true;
		}
	}

	public void contextInitialized(ServletContextEvent event) {
		//ServletContext sc = event.getServletContext();
		//sendErrorService = (SendErrorService) WebApplicationContextUtils.getWebApplicationContext(sc).getBean("sendErrorService");
		paramThread = new ParamThread();
		Timer timer = new Timer();
		timer.schedule(paramThread, 1000, Constants.getPARAM_SEND_DATE()*1000);
	}

	public void contextDestroyed(ServletContextEvent event) {
		paramThread.stopThread();
	}
}

web.xml中配置程序:


		SendParamListener
	
可以在listener之前设置一些初始化参数:


		参数名
		参数值
	
然后在程序初始化方法中使用:

event.getServletContext().getInitParameter("参数名");
获取参数值处理自己的相关业务

以上就介绍了java web监听程序,包括了方面的内容,希望对JSPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_97912.html

相关图片

相关文章