package com.cilij.dhtcrawler.task; import java.net.InetSocketAddress; import org.apache.commons.lang.StringUtils; import com.alibaba.fastjson.JSONObject; import com.cilij.dhtcrawler.main.Main; import com.cilij.dhtcrawler.structure.Torrent; import com.cilij.dhtcrawler.util.CategoryUtil; import com.cilij.entity.InfohashStoreEntity; import com.cilij.redis.RedisPool; import com.cilij.service.InfohashStoreService; import com.cilij.utils.DateUtils; import com.cilij.utils.HttpRequest; import com.cilij.utils.SnowflakeIdWorker; import com.cilij.utils.SpringContextUtils; import lombok.extern.slf4j.Slf4j; /** * * @ClassName: InWarehousTask.java * @Description: TODO(异步torrent批量入库) * * @author kaschie * @version V1.0 * @Date 2019年12月31日 上午12:47:51 */ @Slf4j public class InWarehousTask extends Thread { private Torrent torrent; private InetSocketAddress address; public InWarehousTask(Torrent torrent, InetSocketAddress address) { this.torrent = torrent; this.address = address; } @Override public void run() { try { // Torrent torrent = null; // byte[] tvalue = jedis.rpop(torrent.get.getBytes()); // if(tvalue != null) { // torrent =(Torrent)ByteUtils.toObject(tvalue); //SnowflakeIdWorker3rd idWorker = new SnowflakeIdWorker3rd(); String suffix = StringUtils.EMPTY; String input = torrent.getInfo().getName(); String infohash = torrent.getInfo_hash(); int iof = input.lastIndexOf("."); if (iof != -1) { suffix = input.substring(iof); } String stype = CategoryUtil.Extension(suffix); log.info("InWareHousTask:》》》》》》》》》》》》》》》》" + input + "《《《《《《《《《《《《《《《"); if(StringUtils.isBlank(RedisPool.get(infohash))) { InfohashStoreService infohashStoerOneService = (InfohashStoreService) SpringContextUtils .getBean("infohashStoreService"); if(!StringUtils.isBlank(input)) { if(StringUtils.isBlank(RedisPool.get(infohash))) { if (infohashStoerOneService.queryTotalByTable("movih_infohash_2", infohash) == 0) { //log.info("InWareHousTask:》》》》》》》》》》》》》》》》" + torrent.getInfo().getName() + "《《《《《《《《《《《《《《《"); InfohashStoreEntity en = new InfohashStoreEntity(); //long id = SnowflakeIdWorker.getId(); //en.setId(id); en.setInfoHash(torrent.getInfo_hash()); en.setName(torrent.getInfo().getName()); en.setCategory(stype); en.setExtension(suffix); en.setCreateTime(DateUtils.getSqlDate()); en.setClassified(0); en.setTagged(0); en.setSourceIp(String.valueOf(address.getPort())); en.setLength(torrent.getInfo().getLength()); en.setLastSeen(DateUtils.getSqlDate()); en.setRequests(0); en.setComment(StringUtils.EMPTY); en.setCreator(StringUtils.EMPTY); infohashStoerOneService.save(en); //infohashStoerOneService.setData("movih_infohash_2", en); // CONVER TO JSON BY ENTITY JSONObject joEntity = new JSONObject(); joEntity.put("id", en.getId()); joEntity.put("infoHash", en.getInfoHash()); joEntity.put("category", en.getCategory()); joEntity.put("dataHash", en.getDataHash()); joEntity.put("name", en.getName()); joEntity.put("extension", en.getExtension()); joEntity.put("classified", en.getClassified()); joEntity.put("sourceIp", en.getSourceIp()); joEntity.put("tagged", en.getTagged()); joEntity.put("length", en.getLength()); joEntity.put("createTime", en.getCreateTime()); joEntity.put("lastSeen", en.getLastSeen()); joEntity.put("requests", en.getRequests()); joEntity.put("comment", en.getComment()); joEntity.put("creator", en.getCreator()); joEntity.put("hasitems", en.getHasitems()); JSONObject jo = new JSONObject(); jo.put("table", "movih_infohash_2"); jo.put("data", joEntity); log.info(jo.toString()); String resultMsg = HttpRequest.sendPost(RedisPool.get("setes_url"), jo.toString(), StringUtils.EMPTY); log.info(resultMsg); if (!RedisPool.exists(infohash)) { RedisPool.set(infohash, input); } }else { RedisPool.set(infohash, infohash); } } //} } // 入库明细表 /* * InfohashStoreTwoItemsService infohashdtlsService = * (InfohashStoreTwoItemsService) SpringContextUtils * .getBean("infohashStoreTwoItemsService"); * * //通用JSON入库 String jsonSubFiles = * JSON.toJSONString(torrent.getInfo().getFiles()); * //System.out.println("testsssssssssssss:"+jsonSubFiles); //通用列表入库 * //for(SubFile f: torrent.getInfo().getFiles()) { * //System.out.println("@@@@@@@@@@@@@@@@@@@"+f.getPath()); * //System.out.println("###################"+f.getLength()); InfohashdtlsEntity * dtl = new InfohashdtlsEntity(); dtl.setInfohashloopid(en.getId()); * dtl.setName(""); dtl.setSize(""); * //dtl.setJsonname(JSON.parseObject(jsonSubFiles)); * dtl.setJsonname(jsonSubFiles); infohashdtlsService.save(dtl); */ // 添加索引 /*String indexName = ESConstant.INFOHASHSTORE; boolean indexIsExist = ESIndexFactory.isExists(indexName); if (indexIsExist) { long flowId = idWorker.nextId(); List<IndexInfohashStoreOnefoModel> indexmodel = new ArrayList<IndexInfohashStoreOnefoModel>(); System.out.println(en.getId() + "$" + en.getInfoHash() + "$" + en.getName() + "$" + en.getCreateTime() + "$" + en.getLength() + "$" + en.getLastSeen()); IndexInfohashStoreOnefoModel m = new IndexInfohashStoreOnefoModel(); m.setId(String.valueOf(en.getId())); m.setId_IK(Long.toString(flowId)); m.setInfohash_IK(en.getInfoHash()); m.setInfohash(en.getInfoHash()); m.setName_IK(en.getName()); m.setName_simple(en.getName()); m.setCreatedate(en.getCreateTime().toString()); m.setSize(Long.toString(((en.getLength() / 1024) / 1024))); m.setLast_seen(en.getLastSeen().toString()); m.setCategory(en.getCategory()); m.setRequests(en.getRequests().toString()); m.setFrom("C"); indexmodel.add(m); System.out.println("####################:" + torrent.getInfo().getName() + "准备索引"); // addBatchResult2Index(m); IndexService.addInfohashIndex(indexName, indexmodel); }*/ } } catch (Exception e) { if (!RedisPool.exists(torrent.getInfo_hash())) { RedisPool.set(torrent.getInfo_hash(), torrent.getInfo().getName()); } System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@" + e.getMessage()); } finally { Main.count.decrementAndGet(); } } }
标签:en,joEntity,InWarehousTask,put,import,com,torrent From: https://www.cnblogs.com/okeyl/p/17263343.html