SuggestionDiscoveryJob.java
3.14 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
package com.yoho.search.consumer.job;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.yoho.search.consumer.suggests.common.RetryBusinessFlowExecutor;
import com.yoho.search.consumer.suggests.common.SuggestionCache;
import com.yoho.search.consumer.suggests.common.SuggestionConstants;
import com.yoho.search.consumer.suggests.discover.AbstractSuggestionDiscoverer;
@Component
public class SuggestionDiscoveryJob implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger("FLOW_EXECUTOR");
private List<RetryBusinessFlowExecutor> flowExecutorList = new ArrayList<>();
@Autowired
private SuggestionCache suggestionCache;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, AbstractSuggestionDiscoverer> beanMap = applicationContext.getBeansOfType(AbstractSuggestionDiscoverer.class);
if (beanMap == null || beanMap.isEmpty()) {
logger.warn("There is no suggestion discoverer defined.");
return;
}
flowExecutorList = beanMap.values().stream()
.sorted((discoverer1, discoverer2) -> discoverer1.getKeywordType().compare(discoverer2.getKeywordType()))
.map(counter -> new RetryBusinessFlowExecutor(counter, SuggestionConstants.SUGGESTION_DISCOVER_BATCH_MAX_THREAD_SIZE,
SuggestionConstants.SUGGESTION_DISCOVER_BATCH_LIMIT))
.collect(Collectors.toList());
}
@Scheduled(cron = "0 30 1 * * ?")
public void execute() {
try {
long begin = System.currentTimeMillis();
logger.info("SuggestionDiscoveryJob execute start----[begin={}]", begin);
suggestionCache.init();
boolean result = true;
for (RetryBusinessFlowExecutor executor : flowExecutorList) {
boolean tempResult = executor.execute();
if (!tempResult) {
logger.warn("SuggestionDiscoveryJob execute has failure----[bean={}]", executor.getFlowName());
}
result = result && tempResult;
}
logger.info("SuggestionDiscoveryJob execute end----[cost={}][result={}]", (System.currentTimeMillis() - begin), result);
} finally {
suggestionCache.cleanup();
}
}
public void executeFlow(String flowName) {
try {
long begin = System.currentTimeMillis();
logger.info("SuggestionDiscoveryJob executeFlow start----[begin={}]", begin);
suggestionCache.init();
Optional<RetryBusinessFlowExecutor> executor = flowExecutorList.stream().filter(item -> flowName.equals(item.getFlowName())).findAny();
executor.ifPresent(RetryBusinessFlowExecutor::execute);
logger.info("SuggestionDiscoveryJob executeFlow end----[cost={}]", (System.currentTimeMillis() - begin));
} finally {
suggestionCache.cleanup();
}
}
}