package com.ps.ps.task;
import cn.hutool.aop.proxy.ProxyFactory;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.ps.ps.dao.UpcUseDao;
import com.ps.support.annotion.ScheduleAspectAnnotion;
import com.ps.support.utils.UpcEanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.net.SocksProxy;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.time.LocalDateTime;
import java.util.HashMap;
@Component
@EnableScheduling
@EnableAsync
public class MultithreadScheduleTask {
@Autowired
private UpcUseDao upcUseDao;
@Async
@Scheduled(fixedDelay = 1000) //间隔1秒
//@ScheduleAspectAnnotion(lock = "MultiThreadScheduleTask")
public void first() throws InterruptedException {
String upc_ean = UpcEanUtil.getUpcCountryCodeNumber()+UpcEanUtil.getFactoryCode(2);
System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
String upc = UpcEanUtil.getEanUpcCode(upc_ean,2);
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("__VIEWSTATE", "/wEPDwUKLTQ2MzE0NjA2Mg9kFgICAQ9kFgICCw8WAh4HVmlzaWJsZWcWBgIBDw8WAh4EVGV4dAUMNjgzNDEzMjE0NzE2ZGQCAw8PFgQfAQUBMh8AaGRkAgQPFgIfAGdkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQpSQl9Db21wYW55RHeKjTAwsdfoihZ3AolA1xOJtYWYf+Q/P+a6Hku/wMg=");
paramMap.put("__VIEWSTATEGENERATOR", "62516D83");
paramMap.put("__EVENTVALIDATION", "/wEdAAirGjy2k+LyNqnrb/rT1h8RLTHaOnjjN9ezrJXLmfOeAMgC2mH1Ur6wyTd3BUNIcyVfZuExTfFzIprnkUWzZvypQc7pjhZjNYi15Kif1wug4Y5kj4ZWwT15klcNlp3h6vfimzGB1FwnxhWwzLQmNaR9h5yZPlYu7El/UlMAx6LSmKQ7MRYJMN9auBeog2ubO4RJTxALRcv77xCdmtcw+gVf");
paramMap.put("Top$h_keyword", "");
paramMap.put("navBar$h_keyword", "");
paramMap.put("choose", "RB_Company");
paramMap.put("txtcode", upc);
paramMap.put("Btnok", "查询");
// SocketAddress address = new InetSocketAddress("114.55.103.83",8089);
// Proxy proxy = SocksProxy.create(address,1);
String result2 = HttpRequest.post("http://www.ancc.org.cn/Service/queryTools/External.aspx")
.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded")
.header(Header.COOKIE, "ASP.NET_SessionId=aqh41044ardg1ucwxpbydubw; ASP.NET_SessionId_NS_Sig=oenCV6mdyH94-Af9; Hm_lvt_9c8177c69c779c5377aa18389b4ed389=1557122571,1557373314,1557737174,1557738237; Hm_lpvt_9c8177c69c779c5377aa18389b4ed389=1557742987")
.header("cache-control", "no-cache")
.header("Postman-Token", "67df4c7b-90ef-482c-a08f-9b894cd65f6c")
.form(paramMap)//表单内容
// .setProxy(proxy)
.timeout(20000)//超时,毫秒
.execute().body();
if(result2.contains("<tbody>")){
upcUseDao.insertValue(upc);
}
Thread.sleep(1000 * 10);
}
}