package com.product.file.util;
|
|
import java.util.HashSet;
|
import java.util.LinkedList;
|
import java.util.Set;
|
|
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
|
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.file.service.FileManagerService;
|
import com.product.lucene.service.LuceneService;
|
|
|
/**
|
* 消息服务,添加消息,读取消息
|
*
|
* @author shicf
|
*/
|
@Component
|
public class CreateDocumentIndexThread {
|
/**
|
* 自已实例
|
**/
|
private static CreateDocumentIndexThread createDocumentIndexThread;
|
@Autowired
|
public FileManagerService fileManagerService;
|
/**
|
* 内部线程
|
**/
|
private SendMailThread sendMailThread = null;
|
@Autowired
|
public LuceneService luceneService;
|
/**
|
* 生成的新消息队列
|
**/
|
private static LinkedList<FieldSetEntity> documentIndexs = new LinkedList<>();
|
|
/**
|
* 静态工厂方法 获取当前对象实例 多线程安全单例模式(使用双重同步锁)
|
*/
|
public static synchronized CreateDocumentIndexThread getInstance() {
|
|
if (createDocumentIndexThread == null) {
|
synchronized (CreateDocumentIndexThread.class) {
|
if (createDocumentIndexThread == null) {
|
createDocumentIndexThread = (CreateDocumentIndexThread) SpringBeanUtil.getBean("createDocumentIndexThread");
|
}
|
}
|
}
|
createDocumentIndexThread.start();
|
return createDocumentIndexThread;
|
}
|
|
/**
|
* 启动缓存的刷新线程
|
*/
|
public void start() {
|
if (sendMailThread == null || sendMailThread.getState() == Thread.State.TERMINATED) {
|
sendMailThread = new SendMailThread();
|
sendMailThread.start();
|
SpringMVCContextHolder.getSystemLogger().info("Thread Of Refresh Cache Is Starting.............");
|
}
|
}
|
|
/**
|
* 把附件信息添加对列中
|
*
|
* @param user
|
*/
|
|
public synchronized void appendAttaInfo(FieldSetEntity documentIndex) {
|
if (documentIndex != null) {
|
documentIndexs.add(documentIndex);
|
}
|
}
|
|
//重试文件的uuid集合
|
private Set<String> retryFileUuids = new HashSet<>();
|
|
/**
|
* 取刷新表队表的第一个
|
*
|
* @return
|
*/
|
public synchronized void pop() {
|
while (!documentIndexs.isEmpty()) {
|
FieldSetEntity ff = documentIndexs.pop();
|
if (luceneService == null) {
|
luceneService = (LuceneService) SpringBeanUtil.getBean("luceneService");
|
}
|
try {
|
ff.setValue("file", fileManagerService.getFile(ff.getString("attachment_uuid")));
|
} catch (Exception e) {
|
if (retryFileUuids.contains(ff.getString("attachment_uuid"))) {
|
//重试过的文件不再重试,删除队列中的文件
|
retryFileUuids.remove(ff.getString("attachment_uuid"));
|
continue;
|
} else {
|
retryFileUuids.add(ff.getString("attachment_uuid"));
|
//添加到队列的最后
|
appendAttaInfo(ff);
|
continue;
|
}
|
}
|
try {
|
|
luceneService.createdIndex(ff);
|
} catch (OLE2NotOfficeXmlFileException e) {
|
e.printStackTrace();
|
SpringMVCContextHolder.getSystemLogger().error(e);
|
} catch (Exception e) {
|
SpringMVCContextHolder.getSystemLogger().error(e);
|
e.printStackTrace();
|
}
|
}
|
}
|
|
/**
|
* 内部线程,定时刷新缓存
|
*
|
* @author shicf
|
*/
|
class SendMailThread extends Thread {
|
@Override
|
public void run() {
|
try {
|
while (true) {
|
sleep(2000);// 执行间隔2s
|
pop();
|
}
|
} catch (InterruptedException e) {
|
e.getStackTrace();
|
}
|
}
|
}
|
|
}
|