解决方式:使用线程池+队列
项目基于Spring,如果不用spring需要自己把
ThreadPoolManager.java
改成单例模式
1.写一个Controller(Spring mvc)
2.线程池管理
3.线程池中工作的线程
浏览器输入地址127.0.0.1/pool
几秒后关闭tomcat。
模拟500条数据,订单线程池处理了117条。调度线程池处理5条
关闭tomcat,后还有378条未处理(这里的实现需要用到spring监听器)。加起来一共500
OK。完毕
spring监听器,监听tomcat关闭事件:
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> { @Autowired ThreadPoolManager threadPoolManager; @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextClosedEvent) { XmlWebApplicationContext x = (XmlWebApplicationContext) event.getSource();
spring配置一下
<bean id="springStartListener" class="com.temp.MyApplicationListener"></bean> |