2016-06-22 8 views
0

ページ分割されたREST APIエンドポイントまたはApache Camel DSLを使用して一度に "K"アイテム/レコードをフェッチするJDBC SQLクエリから読み取る方法はありますか?同じもののきれいな例があれば、それを感謝してください。Apache camelルートのページ分割APIから読み取る

ありがとうございます。

答えて

1

私はこの使用してloopDoWhileのDSLを行っている:

from("direct:start").loopDoWhile(stopLoopPredicate()) 
         .to("bean:restAPIProcessor") 
         .to("bean:dataEnricherBean") 
        .end(); 

stopLoopPredicate()はここにある:

public Predicate stopLoopPredicate() { 
     Predicate stopLoop = new Predicate() { 
      @Override 
      public boolean matches(Exchange exchange) { 
       return exchange.getIn().getBody() != null && !exchange.getIn().getBody().toString().equalsIgnoreCase("stopLoop"); 
      } 
     }; 
     return stopLoop; 
    } 

restAPIProcessorは、REST APIの呼び出しが行われているプロセッサの実装です。

ページ分割を処理するロジックは、restAPIProcessor &に実装されています。実際のREST APIがnull応答を返す瞬間、「stopLoop」が交換アウトルートの本文に設定されます。これはかなりうまくいく。 RestAPIProcessorのコードは次のとおりです。

public class RestAPIProcessor implements Processor { 

    @Inject 
    private RestTemplate restTemplate; 

    private static final int LIMIT = 100; 

    private static final String REST_API = "<REST API URL>"; 

    @Override 
    public void process(Exchange exchange) throws Exception { 
     Integer offset = (Integer) exchange.getIn().getHeader("offset"); 
     Integer count = (Integer) exchange.getIn().getHeader("count"); 

     if (offset == null) offset = 0; 
     if (count == null) count = 0; 

     String response = ""; 
     Map<String,Object> body = new LinkedHashMap<>(); 
     body.put("offset",offset++); 
     body.put("limit",LIMIT); 
     HttpHeaders headers = new HttpHeaders(); 
     headers.setContentType(MediaType.APPLICATION_JSON); 
     HttpEntity<?> entity = new HttpEntity<Object>(body,headers); 
     ResponseEntity<String> countResponseEntity = restTemplate.exchange(REST_API, HttpMethod.POST,entity,String.class); 
     response = countResponseEntity.getBody(); 
     count += LIMIT; 
     if (response == null || response.isEmpty()) { 
      exchange.getIn().setBody("stopLoop"); 
      exchange.getOut().setHeaders(exchange.getIn().getHeaders()); 
     } else { 
      exchange.getIn().setHeader("count", count); 
      exchange.getIn().setHeader("offset", offset); 
      exchange.getOut().setHeaders(exchange.getIn().getHeaders()); 
      exchange.getOut().setBody(response); 
     } 
    } 
} 
関連する問題