WebContextServlet.java 2.74 KB
/**  
* @Title: WebContextServlet.java
* @Package com.bjivt.job.robot.listener
* @Description: TODO(用一句话描述该文件做什么)
* @author Yh.T
* @date 2016年8月3日 下午3:53:07
* @version  
*/ 
package com.bjivt.web.interceptor;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.bjivt.entity.showtime.SystemNotice;
import com.bjivt.service.showtime.SystemNoticeService;
import com.bjivt.service.showtime.UserService;
import com.bjivt.web.thread.SystemNoticeThread;
import com.bjivt.web.thread.ThreadPool;

/**   
 * 项目名称:robot   
 *
 * 类描述:
 * 类名称:com.bjivt.job.robot.listener.WebContextServlet     
 * Yh.T
 * 创建时间:2016年8月3日 下午3:53:07   
 * 修改备注:   
 * @version   
 */

public class WebContextServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		
		SystemNoticeService systemNoticeService = (SystemNoticeService)ctx.getBean("systemNoticeService");
		UserService userService = (UserService)ctx.getBean("userService");
		System.out.println("==================服务器启动=====================");
		//是按循环的才调用
		List<SystemNotice> noticeList = systemNoticeService.getNoticeAsAction(2);
		ThreadPool pool = ThreadPool.getInstance();
		for(SystemNotice notice:noticeList){
			ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
			SystemNoticeThread snt = new SystemNoticeThread();
			snt.setType(notice.getType());
			snt.setUserService(userService);
			snt.setNotice(notice);
			pool.putNotice(notice.getId(), snt);
			service.scheduleAtFixedRate(snt, 5, notice.getIntervalTime(), TimeUnit.MINUTES);
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

		super.destroy();

	}

}