From c90deee5c671a306f43f890694be16cffde70a3a Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 6 Mar 2024 18:20:29 +0800 Subject: [PATCH] 1.fix schedule task issue --- luoo_music/pom.xml | 44 ++++--- .../luoo/music/config/SaticScheduleTask.java | 52 ++++---- .../com/luoo/music/ik/cfg/DefaultConfig.java | 22 ++-- .../com/luoo/music/ik/dic/Dictionary.java | 115 ++++-------------- .../music/ik/dic => resources}/main2012.dic | 0 .../music/ik/dic => resources}/quantifier.dic | 0 6 files changed, 82 insertions(+), 151 deletions(-) rename luoo_music/src/main/{java/com/luoo/music/ik/dic => resources}/main2012.dic (100%) rename luoo_music/src/main/{java/com/luoo/music/ik/dic => resources}/quantifier.dic (100%) diff --git a/luoo_music/pom.xml b/luoo_music/pom.xml index 2869c2f..52dd9e0 100644 --- a/luoo_music/pom.xml +++ b/luoo_music/pom.xml @@ -21,10 +21,6 @@ org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-data-redis - mysql mysql-connector-java @@ -85,25 +81,27 @@ 4.10.3 - net.oschina.j2cache - j2cache-spring-boot2-starter - 2.8.0-release - - - net.oschina.j2cache - j2cache-core - 2.8.5-release - - - org.slf4j - slf4j-simple - - - org.slf4j - slf4j-api - - - + net.oschina.j2cache + j2cache-spring-boot2-starter + 2.8.0-release + + + + net.oschina.j2cache + j2cache-core + 2.8.0-release + + + + org.slf4j + slf4j-simple + + + org.slf4j + slf4j-api + + + app diff --git a/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java b/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java index 723b1e5..891c69f 100644 --- a/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java +++ b/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java @@ -45,9 +45,10 @@ public class SaticScheduleTask { @Autowired private TagDao tagDao; - /* - * @PostConstruct private void init() { updateAutoComplete(); } - */ + @PostConstruct + private void init() { + updateAutoComplete(); + } // 1.凌晨4点执行更新期刊tag @Scheduled(cron = "0 0 4 * * *") @@ -60,29 +61,28 @@ public class SaticScheduleTask { } // 2.凌晨4:20点执行更新分词 - /* - * @Scheduled(cron = "0 20 4 * * *") private void updateAutoComplete() { - * Stream>> journalSongStream = - * journalSongDao.findAll().parallelStream() .map(j -> - * getJournalSongMap(j)).flatMap(m -> m.entrySet().stream()); - * - * Stream>> journalStream = - * journalDao.findValidJournals().parallelStream() .map(j -> - * getJournalMap(j)).flatMap(m -> m.entrySet().stream()); - * - * Map>>> map = - * Stream.concat(journalSongStream, journalStream) - * .collect(Collectors.groupingBy(e -> e.getKey())); - * - * int keyCount = map.size(); int cacheKeyCount = - * cacheChannel.keys(constants.Constants.J2CACHE_REGION_SEARCH_AUTO_COMPLETE). - * size(); if (keyCount != cacheKeyCount) { - * map.entrySet().parallelStream().forEach(e -> { List value = - * e.getValue().stream().flatMap(a -> a.getValue().stream()).distinct().sorted() - * .collect(Collectors.toList()); - * cacheChannel.set(constants.Constants.J2CACHE_REGION_SEARCH_AUTO_COMPLETE, - * e.getKey(), value); }); } } - */ + + @Scheduled(cron = "0 20 4 * * *") + private void updateAutoComplete() { + Stream>> journalSongStream = journalSongDao.findAll().parallelStream() + .map(j -> getJournalSongMap(j)).flatMap(m -> m.entrySet().stream()); + + Stream>> journalStream = journalDao.findValidJournals().parallelStream() + .map(j -> getJournalMap(j)).flatMap(m -> m.entrySet().stream()); + + Map>>> map = Stream.concat(journalSongStream, journalStream) + .collect(Collectors.groupingBy(e -> e.getKey())); + + int keyCount = map.size(); + int cacheKeyCount = cacheChannel.keys(constants.Constants.J2CACHE_REGION_SEARCH_AUTO_COMPLETE).size(); + if (keyCount != cacheKeyCount) { + map.entrySet().parallelStream().forEach(e -> { + List value = e.getValue().stream().flatMap(a -> a.getValue().stream()).distinct().sorted() + .collect(Collectors.toList()); + cacheChannel.set(constants.Constants.J2CACHE_REGION_SEARCH_AUTO_COMPLETE, e.getKey(), value); + }); + } + } private Map> getJournalMap(Journal j) { Map> map = new HashMap<>(); diff --git a/luoo_music/src/main/java/com/luoo/music/ik/cfg/DefaultConfig.java b/luoo_music/src/main/java/com/luoo/music/ik/cfg/DefaultConfig.java index e5422d7..c7789cf 100644 --- a/luoo_music/src/main/java/com/luoo/music/ik/cfg/DefaultConfig.java +++ b/luoo_music/src/main/java/com/luoo/music/ik/cfg/DefaultConfig.java @@ -28,10 +28,11 @@ package com.luoo.music.ik.cfg; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.InvalidPropertiesFormatException; import java.util.List; import java.util.Properties; +import org.springframework.core.io.ClassPathResource; + /** * Configuration 默认实现 * 2012-5-8 @@ -42,8 +43,8 @@ public class DefaultConfig implements Configuration{ /* * 分词器默认字典路径 */ - private static final String PATH_DIC_MAIN = "com/luoo/music/ik/dic/main2012.dic"; - private static final String PATH_DIC_QUANTIFIER = "com/luoo/music/ik/dic/quantifier.dic"; + private static final String PATH_DIC_MAIN = "main2012.dic"; + private static final String PATH_DIC_QUANTIFIER = "quantifier.dic"; /* * 分词器配置文件路径 @@ -74,16 +75,11 @@ public class DefaultConfig implements Configuration{ private DefaultConfig(){ props = new Properties(); - InputStream input = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME); - if(input != null){ - try { - props.loadFromXML(input); - } catch (InvalidPropertiesFormatException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } + try (InputStream input = new ClassPathResource(FILE_NAME).getInputStream();) { + props.loadFromXML(input); + } catch (IOException e) { + e.printStackTrace(); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/ik/dic/Dictionary.java b/luoo_music/src/main/java/com/luoo/music/ik/dic/Dictionary.java index 0c3b338..391bdfe 100644 --- a/luoo_music/src/main/java/com/luoo/music/ik/dic/Dictionary.java +++ b/luoo_music/src/main/java/com/luoo/music/ik/dic/Dictionary.java @@ -32,6 +32,8 @@ import java.io.InputStreamReader; import java.util.Collection; import java.util.List; +import org.springframework.core.io.ClassPathResource; + import com.luoo.music.ik.cfg.Configuration; /** @@ -195,13 +197,8 @@ public class Dictionary { //建立一个主词典实例 _MainDict = new DictSegment((char)0); //读取主词典文件 - InputStream is = this.getClass().getClassLoader().getResourceAsStream(cfg.getMainDictionary()); - if(is == null){ - throw new RuntimeException("Main Dictionary not found!!!"); - } - - try { - BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512); + try (InputStream is = new ClassPathResource(cfg.getMainDictionary()).getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);) { String theWord = null; do { theWord = br.readLine(); @@ -209,21 +206,10 @@ public class Dictionary { _MainDict.fillSegment(theWord.trim().toLowerCase().toCharArray()); } } while (theWord != null); - - } catch (IOException ioe) { - System.err.println("Main Dictionary loading exception."); - ioe.printStackTrace(); - - }finally{ - try { - if(is != null){ - is.close(); - is = null; - } - } catch (IOException e) { - e.printStackTrace(); - } - } + } catch (IOException e) { + System.err.println("Main Dictionary loading exception."); + e.printStackTrace(); + } //加载扩展词典 this.loadExtDict(); } @@ -235,17 +221,11 @@ public class Dictionary { //加载扩展词典配置 List extDictFiles = cfg.getExtDictionarys(); if(extDictFiles != null){ - InputStream is = null; for(String extDictName : extDictFiles){ //读取扩展词典文件 System.out.println("加载扩展词典:" + extDictName); - is = this.getClass().getClassLoader().getResourceAsStream(extDictName); - //如果找不到扩展的字典,则忽略 - if(is == null){ - continue; - } - try { - BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512); + try (InputStream is = new ClassPathResource(extDictName).getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);) { String theWord = null; do { theWord = br.readLine(); @@ -255,21 +235,10 @@ public class Dictionary { _MainDict.fillSegment(theWord.trim().toLowerCase().toCharArray()); } } while (theWord != null); - - } catch (IOException ioe) { - System.err.println("Extension Dictionary loading exception."); - ioe.printStackTrace(); - - }finally{ - try { - if(is != null){ - is.close(); - is = null; - } - } catch (IOException e) { - e.printStackTrace(); - } - } + } catch (IOException e) { + System.err.println("Extension Dictionary loading exception."); + e.printStackTrace(); + } } } } @@ -283,17 +252,11 @@ public class Dictionary { //加载扩展停止词典 List extStopWordDictFiles = cfg.getExtStopWordDictionarys(); if(extStopWordDictFiles != null){ - InputStream is = null; for(String extStopWordDictName : extStopWordDictFiles){ System.out.println("加载扩展停止词典:" + extStopWordDictName); //读取扩展词典文件 - is = this.getClass().getClassLoader().getResourceAsStream(extStopWordDictName); - //如果找不到扩展的字典,则忽略 - if(is == null){ - continue; - } - try { - BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512); + try (InputStream is = new ClassPathResource(extStopWordDictName).getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);) { String theWord = null; do { theWord = br.readLine(); @@ -303,21 +266,10 @@ public class Dictionary { _StopWordDict.fillSegment(theWord.trim().toLowerCase().toCharArray()); } } while (theWord != null); - - } catch (IOException ioe) { - System.err.println("Extension Stop word Dictionary loading exception."); - ioe.printStackTrace(); - - }finally{ - try { - if(is != null){ - is.close(); - is = null; - } - } catch (IOException e) { - e.printStackTrace(); - } - } + } catch (IOException e) { + System.err.println("Extension Stop word Dictionary loading exception."); + e.printStackTrace(); + } } } } @@ -329,12 +281,8 @@ public class Dictionary { //建立一个量词典实例 _QuantifierDict = new DictSegment((char)0); //读取量词词典文件 - InputStream is = this.getClass().getClassLoader().getResourceAsStream(cfg.getQuantifierDicionary()); - if(is == null){ - throw new RuntimeException("Quantifier Dictionary not found!!!"); - } - try { - BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512); + try (InputStream is = new ClassPathResource(cfg.getQuantifierDicionary()).getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);) { String theWord = null; do { theWord = br.readLine(); @@ -342,21 +290,10 @@ public class Dictionary { _QuantifierDict.fillSegment(theWord.trim().toLowerCase().toCharArray()); } } while (theWord != null); - - } catch (IOException ioe) { - System.err.println("Quantifier Dictionary loading exception."); - ioe.printStackTrace(); - - }finally{ - try { - if(is != null){ - is.close(); - is = null; - } - } catch (IOException e) { - e.printStackTrace(); - } - } + } catch (IOException e) { + System.err.println("Quantifier Dictionary loading exception."); + e.printStackTrace(); + } } } diff --git a/luoo_music/src/main/java/com/luoo/music/ik/dic/main2012.dic b/luoo_music/src/main/resources/main2012.dic similarity index 100% rename from luoo_music/src/main/java/com/luoo/music/ik/dic/main2012.dic rename to luoo_music/src/main/resources/main2012.dic diff --git a/luoo_music/src/main/java/com/luoo/music/ik/dic/quantifier.dic b/luoo_music/src/main/resources/quantifier.dic similarity index 100% rename from luoo_music/src/main/java/com/luoo/music/ik/dic/quantifier.dic rename to luoo_music/src/main/resources/quantifier.dic