系统启动时队列自动下单--ServletContextListener
package com.liying.pear.queue; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; import com.liying.monkey.core.util.BeanFactory; import com.liying.pear.web.order.service.OrderService; /** * 进程监听器 * * @author jqlin * */ public class ProcessingListener implements ServletContextListener { private static final Logger logger = Logger.getLogger(ProcessingListener.class); @Override public void contextInitialized(ServletContextEvent servletContextEvent) { final OrderService ticketService = BeanFactory.getBean(OrderService.class); if(ticketService == null) { logger.warn("没有匹配的下单服务:" + OrderService.class.getName()); return; } new Thread(new Runnable() { @Override public void run() { ticketService.initSendOrder(); } }).start(); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
布署 ServletContextListener
你实现(implements)了 ServletContextListener 编译后,把它放在正确的WEB-INF/classes目录下,更改WEB-INF目录下的 web.xml文件,在web-app节点里添加:
<listener> <listener-class>com.liying.pear.queue.ProcessingListener</listener-class> </listener>