yangyoupeng

添加ESHttpClient.queryAll方法中死循环保护方法

...@@ -177,12 +177,19 @@ public class ESHttpClient { ...@@ -177,12 +177,19 @@ public class ESHttpClient {
177 QueryResponse returnResponse=new QueryResponse(); 177 QueryResponse returnResponse=new QueryResponse();
178 178
179 int size=queryBuilder.getPage().getSize(); 179 int size=queryBuilder.getPage().getSize();
180 + if(size ==0){
181 + return null;
182 + }
183 +
184 + LoopCount loopcount=null;
180 185
181 boolean readDone=false; 186 boolean readDone=false;
182 while(!readDone){ 187 while(!readDone){
183 QueryResponse queryResponse=this.query(queryBuilder); 188 QueryResponse queryResponse=this.query(queryBuilder);
184 int totalSize=queryResponse.getTotal().intValue(); 189 int totalSize=queryResponse.getTotal().intValue();
185 int returnDataSize=queryResponse.getDataList().size(); 190 int returnDataSize=queryResponse.getDataList().size();
191 +
192 +
186 returnResponse.addData(queryResponse.getDataList()); 193 returnResponse.addData(queryResponse.getDataList());
187 int alreadyReadSize=queryBuilder.getPage().getStart()+returnDataSize; 194 int alreadyReadSize=queryBuilder.getPage().getStart()+returnDataSize;
188 if(alreadyReadSize >=totalSize ){ 195 if(alreadyReadSize >=totalSize ){
...@@ -190,12 +197,24 @@ public class ESHttpClient { ...@@ -190,12 +197,24 @@ public class ESHttpClient {
190 returnResponse.setTotal(new Long(totalSize)); 197 returnResponse.setTotal(new Long(totalSize));
191 break; 198 break;
192 } 199 }
200 + //为了防止异常的出现死循环
201 + if(loopcount==null){
202 + loopcount=new LoopCount();
203 + loopcount.maxLoopcount=totalSize/size;
204 + }else if(loopcount.maxLoopcount <= 0){
205 + break;
206 + }else{
207 + loopcount.maxLoopcount=loopcount.maxLoopcount-1;
208 + }
193 queryBuilder.setPage(alreadyReadSize, size); 209 queryBuilder.setPage(alreadyReadSize, size);
210 +
194 } 211 }
195 return returnResponse; 212 return returnResponse;
196 } 213 }
197 214
198 - 215 + private class LoopCount{
216 + int maxLoopcount=0;
217 + }
199 218
200 219
201 /** 220 /**
......