11package com .xwintop .xJavaFxTool .services .littleTools ;
22
3- import cn .hutool .core .thread .ThreadUtil ;
43import com .xwintop .xJavaFxTool .controller .littleTools .FileSearchToolController ;
54import com .xwintop .xJavaFxTool .utils .ConfigureUtil ;
65import com .xwintop .xcore .util .javafx .TooltipUtil ;
1615import org .apache .lucene .store .Directory ;
1716import org .apache .lucene .store .FSDirectory ;
1817import org .apache .lucene .store .NoLockFactory ;
18+ import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
1919
2020import java .io .File ;
2121import java .io .IOException ;
2424import java .nio .file .Path ;
2525import java .nio .file .Paths ;
2626import java .util .*;
27+ import java .util .concurrent .ThreadPoolExecutor ;
2728
2829/**
2930 * @ClassName: FileSearchToolService
3839public class FileSearchToolService {
3940 private FileSearchToolController fileSearchToolController ;
4041 private static Timer autoRefreshIndexTimer = null ;
42+ private static ThreadPoolTaskExecutor refreshIndexPoolTaskExecutor ;
4143 private static final String searchIndexDir = ConfigureUtil .getConfigurePath ("searchIndexDir/" );
4244
4345 private static Directory directory ;
@@ -61,6 +63,13 @@ public class FileSearchToolService {
6163 } catch (Exception e ) {
6264 log .error ("创建文件失败!" , e );
6365 }
66+ refreshIndexPoolTaskExecutor = new ThreadPoolTaskExecutor ();
67+ refreshIndexPoolTaskExecutor .setCorePoolSize (5 );
68+ refreshIndexPoolTaskExecutor .setMaxPoolSize (20 );
69+ refreshIndexPoolTaskExecutor .setQueueCapacity (30 );
70+ // 设置拒绝策略
71+ refreshIndexPoolTaskExecutor .setRejectedExecutionHandler (new ThreadPoolExecutor .CallerRunsPolicy ());
72+ refreshIndexPoolTaskExecutor .initialize ();
6473 }
6574
6675 public static void getIndexWriter (Directory directory ) throws IOException {
@@ -185,7 +194,7 @@ public void refreshIndexAction() throws Exception {
185194 }
186195
187196 public void addSearchIndexFile (Path path ) {
188- ThreadUtil .execute (() -> {
197+ refreshIndexPoolTaskExecutor .execute (() -> {
189198 try {
190199 DirectoryStream <Path > stream = Files .newDirectoryStream (path );
191200 Iterator <Path > pathIterator = stream .iterator ();
0 commit comments