/** 
 * @Title: CrawlerListener.java 
 * @Package com.zhiweidata.bjfzxWebapp.listener 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午9:10:34 
 * @version V1.0 
 */ /**
 * 
 */
package com.zhiweidata.bjfzxWebapp.listener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.zhiweidata.bjfzxWebapp.mongoDao.MediaAndWechatEventDAO;
import com.zhiweidata.bjfzxWebapp.mongoDao.WeiboBusinessDao;
import com.zhiweidata.bjfzxWebapp.mongoDao.impl.MediaAndWechatEventDAOImpl;
import com.zhiweidata.bjfzxWebapp.mongoDao.impl.WeiboBusinessDAOImpl;
import com.zhiweidata.bjfzxWebapp.service.ESThreeDataService;
import com.zhiweidata.bjfzxWebapp.service.KeysWordSerivce;
import com.zhiweidata.bjfzxWebapp.service.Impl.ESThreeDataServiceImpl;
import com.zhiweidata.bjfzxWebapp.service.Impl.KeysWordSerivceImpl;
import com.zhiweidata.bjfzxWebapp.timerTask.ESSearchTimer;
import com.zhiweidata.bjfzxWebapp.timerTask.MediaBaiduSearchTimerTask;
import com.zhiweidata.bjfzxWebapp.timerTask.SendMessageTimerTask;
import com.zhiweidata.bjfzxWebapp.timerTask.WeiboEventSearchByBusinessTimerTask;


/** 
 * @ClassName: CrawlerListener 
 * @Description: TODO(爬虫监听) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午9:10:34  
 */
public class CrawlerListener implements ServletContextListener{
	
	private static Logger log = LoggerFactory.getLogger(CrawlerListener.class);
//	@Autowired
//	WeiboBusinessDao weiboDao;
//	@Autowired
//	MediaAndWechatEventDAO mediaDao;
//	@Autowired
//	KeysWordSerivce keysWordSerivce;
//	@Autowired
//	ESThreeDataService esThreeDataService;
	
//	protected static ApplicationContext ctx = new ClassPathXmlApplicationContext(
//			"applicationContext.xml");
	
	private static Timer media_time = null;
	private static Timer weibo_time = null;
	private static Timer wxSolr_time = null;
	private static Timer send_time = null;

	private static int INTERVAL = 60; // 60分钟

	private static String weibo_token = "2.00FPiRfF3_jZ8E2dfe53d4d90D2doF";

	@Override
	public void contextInitialized(ServletContextEvent event)
	{
		WeiboBusinessDao weiboDao = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()).getBean(WeiboBusinessDAOImpl.class);
		MediaAndWechatEventDAO mediaDao  =  WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()).getBean(MediaAndWechatEventDAOImpl.class);
		KeysWordSerivce keysWordSerivce  =  WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()).getBean(KeysWordSerivceImpl.class);
		ESThreeDataService esThreeDataService =  new ESThreeDataServiceImpl();
		
		
		WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext())
				.getAutowireCapableBeanFactory().autowireBean(this);
		
		MediaBaiDuSerach mediaBaiduSeach = new MediaBaiDuSerach();
		WeiboEvetSearchByBusiness weiboSearch = new WeiboEvetSearchByBusiness(weibo_token);

		log.info("CrawlerListener监听器初始化");
		
		Date now = new Date();
		// 转日历类进行处理
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        // 减一个小时
        calendar.add(Calendar.HOUR_OF_DAY, 1);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time_next = sf.format(calendar.getTime())+":00:00";
		long delay = 0;
		try
		{
			Date date_next = sf2.parse(time_next);
			delay = date_next.getTime()-now.getTime();
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		// 启动任务
		log.info("--------百度网媒 采集：启动--------");
		Timer media_time = new Timer();
		MediaBaiduSearchTimerTask media_task = new MediaBaiduSearchTimerTask(mediaBaiduSeach, mediaDao, keysWordSerivce);
		media_time.schedule(media_task, delay+1 * 60 * 1000, 1000 * 60 * INTERVAL);// interval分钟
		log.info("--------百度网媒 采集：启动成功--------");

		log.info("--------微博接口 采集：启动--------");
		Timer weibo_time = new Timer();
		WeiboEventSearchByBusinessTimerTask weibo_task = new WeiboEventSearchByBusinessTimerTask(weiboSearch, weiboDao,
				keysWordSerivce);
		weibo_time.schedule(weibo_task, delay+1 * 60 * 1000, 1000 * 60 * INTERVAL);// 延时三分钟启动
		log.info("--------微博接口 采集：启动成功--------");

		log.info("--------es接口 采集：启动-----------");
		Timer es_time = new Timer();
		ESSearchTimer es_task = new ESSearchTimer(keysWordSerivce, esThreeDataService, 
				mediaDao, weiboDao);
		es_time.schedule(es_task, delay+1 * 60 * 1000, 1000 * 60 * INTERVAL);
		log.info("--------es接口 采集：启动成功-----------");
		
		log.info("--------发送信息接口：启动--------");
		Timer send_time = new Timer();
		SendMessageTimerTask send_task = new SendMessageTimerTask(keysWordSerivce, mediaDao, weiboDao);
		send_time.schedule(send_task, delay+15 * 60 * 1000, 1000 * 60 * INTERVAL);// 整点延时15分钟启动,
//		send_time.schedule(send_task, 0, 1000 * 60 * INTERVAL);// 定时器测试,不延迟启动,
		log.info("--------发送信息接口：启动成功--------");
	}

	@Override
	public void contextDestroyed(ServletContextEvent event)
	{
		media_time.cancel();
		weibo_time.cancel();
		wxSolr_time.cancel();
		send_time.cancel();
		System.out.println("定时器销毁");
		event.getServletContext().log("定时器销毁");

	}

	public void initKeyword()
	{
		try
		{
			log.info("初始化关键字");
			log.info("初始化关键字成功");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
}
