|
@@ -0,0 +1,92 @@
|
|
|
+package com.tzld.longarticle.recommend.server.service.score;
|
|
|
+
|
|
|
+
|
|
|
+import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
|
|
|
+import com.tzld.longarticle.recommend.server.service.score.strategy.SimilarityStrategy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Future;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author dyp
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ScoreService implements ApplicationContextAware {
|
|
|
+
|
|
|
+ private final Map<String, ScoreStrategy> strategyMap = new HashMap<>();
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
+ private final ExecutorService pool = ThreadPoolFactory.scorePool();
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ Map<String, ScoreStrategy> type = applicationContext.getBeansOfType(ScoreStrategy.class);
|
|
|
+ for (Map.Entry<String, ScoreStrategy> entry : type.entrySet()) {
|
|
|
+ ScoreStrategy value = entry.getValue();
|
|
|
+ strategyMap.put(value.getClass().getSimpleName(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScoreResult score(ScoreParam param) {
|
|
|
+ List<ScoreStrategy> strategies = getScoreStrategy(param);
|
|
|
+ CountDownLatch cdl = new CountDownLatch(strategies.size());
|
|
|
+ List<Future<List<Score>>> futures = new ArrayList<>();
|
|
|
+ for (final ScoreStrategy strategy : strategies) {
|
|
|
+ Future<List<Score>> future = pool.submit(() -> {
|
|
|
+ List<Score> result = new ArrayList<>();
|
|
|
+ result.add(strategy.score(param);
|
|
|
+ cdl.countDown();
|
|
|
+ return result;
|
|
|
+ });
|
|
|
+ futures.add(future);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ cdl.await(30000, TimeUnit.MILLISECONDS);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("rank error", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Map<Class<? extends ScoreStrategy>, Double>> scoreMap = new HashMap<>();
|
|
|
+ for (Future<List<Score>> f : futures) {
|
|
|
+ try {
|
|
|
+ List<Score> data = f.get();
|
|
|
+ for (Score score : data) {
|
|
|
+ Map<Class<? extends ScoreStrategy>, Double> map
|
|
|
+ = scoreMap.computeIfAbsent(score.getContentId(), k -> new HashMap<>());
|
|
|
+ map.put(score.getStrategy(), score.getScore());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("future get error ", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return new ScoreResult(scoreMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ScoreStrategy> getScoreStrategy(ScoreParam param) {
|
|
|
+ List<ScoreStrategy> strategies = new ArrayList<>();
|
|
|
+ strategies.add(strategyMap.get(SimilarityStrategy.class.getSimpleName()));
|
|
|
+ strategies.add(strategyMap.get(ContentPoolStrategy.class.getSimpleName()));
|
|
|
+
|
|
|
+ return strategies;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|