-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path线程
227 lines (181 loc) · 5.95 KB
/
线程
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.cpic.p17.kb.listen;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import com.cpic.p17.kb.service.ArticleIndexService;
/**
* 监听并创建索引
*
* @author dongfeng.zhang
* @author c_huoshengyu-001
* @version 1.0
*/
@Service
@DependsOn({ "articleIndexServiceImpl" })
public class ScanIndexListenImpl implements ScanIndexListen {
/**
* Logger for this class
*/
private static final Logger logger = LoggerFactory.getLogger(ScanIndexListenImpl.class);
private boolean running = true;
@Autowired
private ArticleIndexService articleIndexService;
private ExecutorService executor = Executors.newSingleThreadExecutor();
@PostConstruct
public void startListen() {
executor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(300000);
} catch (InterruptedException ex) {
logger.error("scan index listen sleep", ex);
}
while (running) {
try {
articleIndexService.indexNew();
} catch (Exception ex) {
logger.error("本次索引失败:", ex);
}
try {
Thread.sleep(10000);
} catch (Exception ex) {
logger.error("scan index listen sleep", ex);
}
}
}
});
}
@PreDestroy
public void stopListen() {
if (executor != null) {
running = false;
executor.shutdown();
}
}
}
public class QueryCallWebservice implements Callable<Response> {
public Request request;
public Invoker invoker;
public QueryCallWebservice(Request request,Invoker invoker) {
this.request = request;
this.invoker=invoker;
}
@Override
public Response call() {
Response response=(Response) invoker.doRequest(request);
return response;
}
}
@Service
public class PolicyThreadPoolExecutor {
public ExecutorService threadPool;
public int MAX_THREADS ;
public int MIN_POOLSIZE ;
public int MAX_POOLSIZE ;
public int QUEUE_SIZE ;
public int IDLE_THREAD_KEEP_ALIVE ;
static Logger logger = LoggerFactory.getLogger(PolicyThreadPoolExecutor.class);
/**
* 关闭内部线程池,调用后实际组件实例不再可用
*/
@PreDestroy
public void shutDown() {
logger.info("threadpool shutdown called");
this.threadPool.shutdown();
}
@PostConstruct
public void afterPropertiesSet() throws Exception {
logger.debug("PolicyThreadPoolExecutor afterPropertiesSet 调用开始");
CodeEntry dic =new CodeEntry();
dic = AppContext.getDic(CommonConstant.THREAD_POOL_CA,CodeContext.THREAD_POOL , AppContext.SYS_TYPE_LIFE);//写死寿险2
if(dic==null){
MAX_THREADS = 5;
MIN_POOLSIZE = 5;
MAX_POOLSIZE = 30;
QUEUE_SIZE = 5;
IDLE_THREAD_KEEP_ALIVE = 5;
}else{
MAX_THREADS = Integer.parseInt(dic.getExtProp1());
MIN_POOLSIZE = Integer.parseInt(dic.getExtProp2());
MAX_POOLSIZE = Integer.parseInt(dic.getExtProp3());
QUEUE_SIZE = Integer.parseInt(dic.getExtProp4());
IDLE_THREAD_KEEP_ALIVE = Integer.parseInt(dic.getDescribe());
}
// BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(QUEUE_SIZE);
// this.threadPool = new ThreadPoolExecutor(MIN_POOLSIZE,
// MAX_POOLSIZE,
// IDLE_THREAD_KEEP_ALIVE, TimeUnit.SECONDS,
// queue);
//TODO 更改阻塞队列为无限制连接池 @dongfeng.zhang
this.threadPool=Executors.newCachedThreadPool();
logger.debug("PolicyThreadPoolServiceImpl afterPropertiesSet 调用结束");
}
public ExecutorService getThreadPool() {
return threadPool;
}
public void setThreadPool(ExecutorService threadPool) {
this.threadPool = threadPool;
}
}
/**
* 获取生成文件名
* @param reqModel2
* @return
*/
@ResponseBody
@RequestMapping(value = "/getFileName", consumes = "application/json", produces = "application/json", method = RequestMethod.POST)
public RespModel getFileName(@RequestBody ReqModel2 reqModel2){
AppConfig config = AppContext.getConf(CommonConstant.SERVICEACTIVITY_EXCEL_PATH_CODE, CommonConstant.SERVICEACTIVITY_EXCEL_PATH_CATE_CODE, AppContext.getCurrentSysType());
String filePath = config.getValue();
if(!filePath.endsWith("/")) {
filePath += File.pathSeparator;
}
filePath +=AppContext.getCurrentUserName()+"-"+System.currentTimeMillis()+".xls";
createThread(reqModel2.getQueryParams(), filePath);
return new RespModel(filePath);
}
/**
* 创建生成文件的线程
* @param queryParams
*/
private void createThread(final QueryParams queryParams, final String filePath) {
ExcelStateMap.put(filePath, "Ready");
Runnable runnable = new Runnable() {
@Override
public void run() {
cleanPreMap();
ExcelStateMap.put(filePath, "Begin");
String actType = (String)queryParams.get("actType");
queryParams.put("export", 1);//设置导出标识
ServiceActivityVO vo = ServiceActivityQueryVO.getServiceActivityVO(actType);
List<ServiceActivityVO> list = getAllActivityList(queryParams);
List<ServiceActivityVO> resultList = activityService.getRelatedServiceRequest(list, actType);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExportExcel<ServiceActivityVO> export = new ExportExcel<ServiceActivityVO>();
export.export(vo.getTitle(), vo.getHeadColumn(), resultList, vo.getProperties(), out, true);
byte[] bytes = out.toByteArray();
File file = new File(filePath);
File parentFile = file.getParentFile();
if(!parentFile.exists()) {
parentFile.mkdirs();
}
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
ExcelStateMap.put(filePath, "Complete");
} catch (Exception e) {
log.warn("生成文件失败,具体原因如下:", e);
ExcelStateMap.put(filePath, "Error");
}
}
};
new Thread(runnable).start();
}