| src/main/java/com/nq/service/ISiteNewsService.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/nq/service/impl/SiteNewsServiceImpl.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/nq/utils/http/HttpRequest.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/nq/utils/task/news/NewsTask.java | ●●●●● patch | view | raw | blame | history |
src/main/java/com/nq/service/ISiteNewsService.java
@@ -42,7 +42,7 @@ /** * 抓取新闻 */ int grabNews(); void grabNews(); /** * 新闻资讯-修改新闻浏览量 src/main/java/com/nq/service/impl/SiteNewsServiceImpl.java
@@ -130,24 +130,20 @@ return ServerResponse.createBySuccess(pageInfo); } /*新闻资讯-抓取*/ @Override public int grabNews() { int ret = 0; addNews(1, EStockType.IN.getStockUrl() +"stock-markets?key="+ EStockType.IN.getStockKey() +"&type=4"); return ret; public void grabNews() { addNews(1, EStockType.IN.getStockUrl() + "stock-markets?key=" + EStockType.IN.getStockKey() + "&type=4"); } private int addNews(Integer type, String url){ int k = 0; private void addNews(Integer type, String url) { try { String newlist = HttpRequest.doGrabGet(url); JSONArray jsonArray = JSONArray.fromObject(newlist); if(jsonArray.size()>0){ if (jsonArray.size() > 0) { for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String newsId = jsonObject.getString("id"); if(siteNewsMapper.getNewsBySourceIdCount(newsId) == 0){ if (siteNewsMapper.getNewsBySourceIdCount(newsId) == 0) { SiteNews siteNews = new SiteNews(); siteNews.setSourceId(newsId); siteNews.setSourceName(""); @@ -165,10 +161,11 @@ } } catch (Exception e) { e.printStackTrace(); log.error("添加新闻出现异常: " + e.getMessage()); } return k; } public static String convertBase64ToImage(String base64Str, String path) { byte[] imageBytes = Base64.getDecoder().decode(base64Str); try { src/main/java/com/nq/utils/http/HttpRequest.java
@@ -68,50 +68,40 @@ /* *抓數據專用, *signature:授權碼 * timestamp:時間戳 * url */ public static String doGrabGet(String url) throws Exception { // Request URL: http://gateway.jinyi999.cn/rjhy-news/api/1/tcy/news/hotlist?columnCodes=cjyw&appCode=tcy&showPermission=0&limit=20&hasContent=0&pageNo=1 URL localURL = new URL(url); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) connection; httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setConnectTimeout(6000); InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader reader = null; httpURLConnection.setConnectTimeout(12000); // 设置连接超时时间 httpURLConnection.setReadTimeout(12000); // 设置读取超时时间 StringBuffer resultBuffer = new StringBuffer(); String tempLine = null; if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } try { inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); try (InputStream inputStream = httpURLConnection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader reader = new BufferedReader(inputStreamReader)) { String tempLine; while ((tempLine = reader.readLine()) != null) { resultBuffer.append(tempLine); } } finally { if (reader != null) { reader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (inputStream != null) { inputStream.close(); } httpURLConnection.disconnect(); // 断开连接,释放资源 } return resultBuffer.toString(); } public static String doPost(String url, Map<String, String> params) throws Exception { HttpPost httpPost = new HttpPost(url); src/main/java/com/nq/utils/task/news/NewsTask.java
@@ -1,13 +1,19 @@ package com.nq.utils.task.news; import com.nq.enums.EStockType; import com.nq.pojo.SiteNews; import com.nq.service.ISiteArticleService; import com.nq.service.ISiteNewsService; import com.nq.utils.http.HttpRequest; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; //import org.springframework.scheduling.annotation.Scheduled; @@ -27,27 +33,29 @@ private final Lock NewsInfoTaskLock = new ReentrantLock(); /* * 新聞資訊抓取 * */ * 新聞資訊抓取 * */ @Scheduled(cron = "0 0/5 * * * ?") public void NewsInfoTask() { if (NewsInfoTask) NewsInfoTask = false; else return; if (NewsInfoTaskLock.tryLock()) { if (NewsInfoTask) // 定义一个布尔变量,代表新闻任务是否在处理中 return; NewsInfoTask = true; // 设置处理中标识为 true if (NewsInfoTaskLock.tryLock()) { // 加锁 try { System.out.println("新闻定时任务---->开始"); this.iSiteNewsService.grabNews(); iSiteNewsService.grabNews(); } catch (Exception e) { e.printStackTrace(); System.err.println("新闻抓取出现异常: " + e.getMessage()); // 打印具体的异常信息 } finally { NewsInfoTaskLock.unlock(); NewsInfoTask = true; NewsInfoTaskLock.unlock(); // 释放锁 NewsInfoTask = false; // 设置处理中标识为 false System.out.println("新闻定时任务---->结束"); } System.out.println("新闻定时任务---->结束"); } } /* * 新聞公告抓取 * */