案例
package com.zxwa.ntmss.img2text; import com.alibaba.fastjson.JSONObject; import com.zxwa.ntmss.img2text.utils.FileUtils; import com.zxwa.ntmss.process.common.util.sql.JdbcUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; import java.net.HttpURLConnection; import java.net.URL; import java.text.MessageFormat; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import static com.zxwa.ntmss.img2text.thread.Img2TextThread.readString; public class OtherNtmssV3ContentImgProcesser3 { private static JdbcTemplate zxCrawlerDbJdbcTemplate = JdbcUtils.getZxCrawlerDbJdbcTemplate(); private static Logger LOG = LoggerFactory.getLogger(OtherNtmssV3ContentImgProcesser3.class); public static void main(String[] args) throws InterruptedException {
int tableNum = 10;
ConcurrentLinkedQueue<Map<String, Object>> queue = new ConcurrentLinkedQueue<>();
ExecutorService executorService = Executors.newFixedThreadPool(tableNum); while (true) { CountDownLatch count = new CountDownLatch(tableNum); List<Map<String, Object>> mapList = zxCrawlerDbJdbcTemplate.queryForList("SELECT * FROM `cxyxproduct` s WHERE s.text is NULL LIMIT 100;"); queue.addAll(mapList); System.out.println("-----------------------------------开饭了-----------------------------------"); long start = System.currentTimeMillis();
for (int i = 0; i < tableNum; i++) { executorService.submit(new Dinner("00" + (i + 1), queue, count)); } //计数器等待,知道队列为空(所有人吃完) count.await(); long time = System.currentTimeMillis() - start; System.out.println("-----------------------------------所有人已经吃完-----------------------------------"); System.out.println("共耗时:" + time); //停止线程池 // executorService.shutdown(); } } private static class Dinner implements Runnable { private String name; private ConcurrentLinkedQueue<Map<String, Object>> queue; private CountDownLatch count; public Dinner(String name, ConcurrentLinkedQueue<Map<String, Object>> queue, CountDownLatch count) { this.name = name; this.queue = queue; this.count = count; } @Override public void run() { while (!queue.isEmpty()) { try { Map<String, Object> map = queue.poll(); StringBuilder builder = new StringBuilder(); Object productImg = map.get("productimg"); if (productImg != null) { String strImg = productImg.toString(); String[] split = strImg.split(","); for (String url : split) { String fileUrl = MessageFormat.format("http://", FileUtils.formatUrl(url), "4", map.get("id")); URL urlReal = new URL(fileUrl); //打开链接 HttpURLConnection conn = (HttpURLConnection) urlReal.openConnection(); //设置请求方式为"GET" conn.setRequestMethod("GET"); //超时响应时间为5秒 conn.setConnectTimeout(15 * 1000); String response = readString(conn.getInputStream()); String text = JSONObject.parseObject(response).getString("text"); if (text == null) { text = ""; } builder.append(text).append("#"); } } String str = builder.toString(); String id = map.get("id").toString(); zxCrawlerDbJdbcTemplate.update("UPDATE `spider_data_15f`.`cxyxproduct` s SET s.text=?,s.productimg_new=s.productimg WHERE (s.id=?);", str, id); LOG.info(id + " " + str); } catch (Exception e) { System.out.println(e.getMessage()); } } count.countDown();//计数器-1 } } }