Skip to content

Commit

Permalink
优化细节
Browse files Browse the repository at this point in the history
  • Loading branch information
AylaAsia-wangxiaodong committed Sep 14, 2021
1 parent 57e40f2 commit b249124
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/github/lyrric/conf/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public class Config {

/**
* 抢购是否成功
* false表示疫苗已抢光
*/
public static boolean success = false;
public static Boolean success = null;

/**
* 加密参数st
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/github/lyrric/service/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -134,6 +135,12 @@ private String get(String path, Map<String, String> params, Header extHeader) th
if(extHeader != null){
headers.add(extHeader);
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(2500)
.setSocketTimeout(2500)
.setConnectTimeout(2500)
.build();
get.setConfig(requestConfig);
get.setHeaders(headers.toArray(new Header[0]));
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(get);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/github/lyrric/service/SecKillRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.github.lyrric.conf.Config;
import com.github.lyrric.model.BusinessException;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.SocketTimeoutException;

/**
* @author wangxiaodong
*/
Expand Down Expand Up @@ -54,13 +58,16 @@ public void run() {
} catch (BusinessException e) {
logger.info("Thread ID: {}, 抢购失败: {}", id, e.getErrMsg());
if(e.getErrMsg().contains("没抢到")){
Config.success = false;
break;
}
} catch (Exception e) {
} catch (ConnectTimeoutException | SocketTimeoutException socketTimeoutException ){
logger.error("Thread ID: {},抢购失败: 超时了", Thread.currentThread().getId());
}catch (Exception e) {
logger.warn("Thread ID: {},未知异常", Thread.currentThread().getId());
}finally {
//如果离开始时间10分钟后,或者已经成功抢到则不再继续
if (System.currentTimeMillis() > startDate + 1000 * 60 *10 || Config.success) {
if (System.currentTimeMillis() > startDate + 1000 * 60 *10 || Config.success != null) {
break;
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/github/lyrric/service/SecKillService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.github.lyrric.model.BusinessException;
import com.github.lyrric.model.VaccineList;
import com.github.lyrric.ui.MainFrame;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -28,7 +30,7 @@ public class SecKillService {

private final Logger logger = LogManager.getLogger(SecKillService.class);

ExecutorService service = Executors.newFixedThreadPool(3);
ExecutorService service = Executors.newFixedThreadPool(4);

public SecKillService() {
httpService = new HttpService();
Expand All @@ -53,6 +55,8 @@ public void startSecKill(Integer vaccineId, String startDateStr, MainFrame mainF
Config.st = httpService.getSt(vaccineId.toString());
logger.info("Thread ID:main,成功获取加密参数st:{}", Config.st);
break;
}catch (ConnectTimeoutException | SocketTimeoutException socketTimeoutException ){
logger.error("Thread ID:main,获取st失败: 超时");
}catch (BusinessException e){
logger.error("Thread ID:main,获取st失败: {}", e.getMessage());
}catch (Exception e) {
Expand All @@ -69,6 +73,8 @@ public void startSecKill(Integer vaccineId, String startDateStr, MainFrame mainF
Thread.sleep(200);
service.submit(new SecKillRunnable(true, httpService, vaccineId, startDate));
Thread.sleep(200);
service.submit(new SecKillRunnable(true, httpService, vaccineId, startDate));
Thread.sleep(200);
service.submit(new SecKillRunnable(false, httpService, vaccineId, startDate));
service.shutdown();
//等待线程结束
Expand Down

0 comments on commit b249124

Please sign in to comment.