Commit f64a456f by wang_jiaxing

将线程数量控制放在配置文件中,增加集群配置

parent 50b1821e
......@@ -5,12 +5,15 @@ import com.founder.file.service.XjFlwsService;
import com.founder.util.HttpUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
@Service
......@@ -18,20 +21,51 @@ public class XjFlwsServiceImpl implements XjFlwsService {
@Autowired
private XjxzFlwsMapper xjxzFlwsMapper;
@Value("${poorSize}")
private Integer poorSize;
@Value("${clusterSize}")
private Integer clusterSize;
@Value("${clusterId}")
private Integer clusterId;
private final ExecutorService cachedThreadPool;
{
cachedThreadPool = Executors.newCachedThreadPool();
}
@Override
public String getJzzjkFlwsnrToXjxz(Date date, String xxzjbh) throws IOException {
if (clusterId > clusterSize){
return "集群数量有误,集群id不能大于集群数量!";
}
Integer count = xjxzFlwsMapper.getFlwsCountWzByDate(date, xxzjbh);
int page = (count / 1000) + 1;
ArrayList<FutureTask<Map<String, Integer>>> list = new ArrayList<>();
for (int i = 0; i < page; i++) {
int limit = 1000;
int begin = i * limit;
int end = (i + 1) * limit;
int finalI = i;
int lmt = count / clusterSize;
int remainder = count % clusterSize;
if (clusterId <= remainder) {
lmt++;
}
int cou = lmt / poorSize;
int remainderCou = lmt % poorSize;
int begin = (clusterId - 1) * lmt;
if (remainder > 0 && clusterId > remainder) {
begin = begin + remainder;
}
int end;
for (int i = 0; i < poorSize; i++) {
int finalBegin = begin;
end = begin + cou;
if (i < remainderCou) {
end++;
}
begin = end;
int finalEnd = end;
FutureTask<Map<String, Integer>> xsAjTask = new FutureTask<>(() -> {
HashMap<String, Integer> map = new HashMap<>();
List<Map<String, String>> flwsList = xjxzFlwsMapper.getFlwsWzByDate(date, xxzjbh, begin, end);
List<Map<String, String>> flwsList = xjxzFlwsMapper.getFlwsWzByDate(date, xxzjbh, finalBegin, finalEnd);
ArrayList<Exception> exceptions = new ArrayList<>();
System.out.println("+++++++++++++" + flwsList.size());
int j = 1;
......@@ -40,7 +74,7 @@ public class XjFlwsServiceImpl implements XjFlwsService {
for (Map<String, String> flws : flwsList) {
try {
String flwsUrl = flws.get("FWDZ") + flws.get("TXLJ");
System.out.println(flwsUrl);
// System.out.println(flwsUrl);
InputStream is = HttpUtil.getInputStreamByUrl(flwsUrl);
if (is != null) {
byte[] bytes = IOUtils.toByteArray(is);
......@@ -62,7 +96,7 @@ public class XjFlwsServiceImpl implements XjFlwsService {
e.printStackTrace();
exceptions.add(e);
}
System.out.println("线程" + finalI + "++++++++++++++" + (j++));
System.out.println("线程" + Thread.currentThread().getName() + "++++++++++++++" + (j++));
}
map.put("exceptions", exceptions.size());
map.put("flws", flwsCount);
......@@ -70,7 +104,7 @@ public class XjFlwsServiceImpl implements XjFlwsService {
System.out.println("=============" + exceptions.size());
return map;
});
new Thread(xsAjTask).start();
cachedThreadPool.submit(xsAjTask);
list.add(xsAjTask);
}
int flsws = 0;
......
......@@ -35,6 +35,10 @@ spring:
poorSize: 1000
clusterSize: 1
clusterId: 1
ftpserverip: 47.92.129.99
nginxport: 9053
ftpport: 4546
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment