package com.product.org.admin.util; import java.util.LinkedList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.product.core.entity.FieldSetEntity; import com.product.core.spring.context.SpringBeanUtil; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.org.admin.service.TimeTaskChangeService; /** * 消息服务,添加消息,读取消息 * @author shicf * */ @Component public class ExecuteOtherProcessThread { /**自已实例**/ private static ExecuteOtherProcessThread executeOtherProcessThread; /**内部线程**/ private ProcessThread processThread=null; @Autowired TimeTaskChangeService timeTaskChangeServices; /** 生成的新消息队列**/ private static LinkedList datas=new LinkedList<>(); /** * 静态工厂方法 获取当前对象实例 多线程安全单例模式(使用双重同步锁) */ public static synchronized ExecuteOtherProcessThread getInstance() { if (executeOtherProcessThread == null) { synchronized (ExecuteOtherProcessThread.class) { if (executeOtherProcessThread == null) { executeOtherProcessThread=(ExecuteOtherProcessThread) SpringBeanUtil.getBean("executeOtherProcessThread"); } } } executeOtherProcessThread.start(); return executeOtherProcessThread; } /** * 启动缓存的刷新线程 */ public void start() { if(processThread ==null || processThread.getState()== Thread.State.TERMINATED ) { processThread=new ProcessThread(); processThread.start(); SpringMVCContextHolder.getSystemLogger().info("Thread Of Refresh Cache Is Starting............."); } } /** * 把用户信息添加对列中 * @param user */ public synchronized void appendFieldSetEntity(FieldSetEntity data) { if(data!=null) { datas.add(data); } } /** * 取刷新表队表的第一个 * @return */ public synchronized void pop() { while(!datas.isEmpty()) { FieldSetEntity ff=datas.pop(); if(timeTaskChangeServices ==null ) { timeTaskChangeServices=(TimeTaskChangeService) SpringBeanUtil.getBean("timeTaskChangeServices"); } timeTaskChangeServices.executeServiceChange(ff,ff.getTableName()); } } /** * 内部线程,定时刷新缓存 * @author shicf * */ class ProcessThread extends Thread { @Override public void run() { try { while(true) { sleep(2000);// 执行间隔2s pop(); } } catch (InterruptedException e) { e.getStackTrace(); } } } }