1821349743@qq.com
2023-02-20 6bc78be53ddf8a6474ea71477fbc9e92149d7938
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.product.file.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.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);
        }
    }
    /**
     * 取刷新表队表的第一个
     * @return
     */
    public synchronized void pop() {
        while(!documentIndexs.isEmpty()) {
            FieldSetEntity ff=documentIndexs.pop();
            if(luceneService ==null ) {
                luceneService=(LuceneService) SpringBeanUtil.getBean("luceneService");
            }
            ff.setValue("file", fileManagerService.getFile(ff.getString("attachment_uuid")));
            luceneService.createdIndex(ff);
        }
    }
    /**
     * 内部线程,定时刷新缓存
     * @author shicf
     *
     */
    class SendMailThread extends Thread {    
        @Override
        public void run() {
            try {
                while(true) {
                    sleep(2000);// 执行间隔2s
                    pop();
                }
            } catch (InterruptedException e) {
                e.getStackTrace();
            }
        }
    }
 
}