2017-09-11 232 views
0

我有以下類,其中spring爲類創建對象。我已經創建WebClient使用CXF進行http請求。我想做2個http調用,我想在2個線程中執行,以便我不必調用該方法兩次。如何在多線程中執行這一行java

@Service 
public class SalesManServiceWebClient { 

    @Value("${vehicle.api.url}") 
    private String vehicleUrl; 

    @Value("${customer.api.url}") 
    private String customerUrl; 

    private static final Logger LOG = LoggerFactory.getLogger(SalesManServiceWebClient.class); 

    public List<String> getListOfDmsId(String market,STouchServiceEnum stouchService) 
    { 
     List<String> dmsIdList= new ArrayList<>(); 
     LOG.info("---Entering getListOfDmsId webClientService---"); 

     LOG.info("customerUrl:"+customerUrl); 
     final String url = getServiceUrl(stouchService); 

     final List<Object> providers = new ArrayList<Object>(); 
     providers.add(new JacksonJsonProvider()); 



     //i want this to be execute in thread 
     CustomerServiceProxy proxy = JAXRSClientFactory.create(url, CustomerServiceProxy.class, providers); 

     Client client = WebClient.client(proxy); 
     client.type(MediaType.APPLICATION_JSON); 
     //client.accept(MediaType.APPLICATION_JSON); 
     client.header("ST_USER", "gasid3"); 
     client.header("Content-Type", MediaType.APPLICATION_JSON_TYPE); 
     LOG.info("== Invoking REST Call for service ==" + url); 

     //till this 


     //i want this to be execute in thread 
     VehicleServiceProxy proxy = JAXRSClientFactory.create(url, VehicleServiceProxy.class, providers); 

     Client client = WebClient.client(proxy); 
     client.type(MediaType.APPLICATION_JSON); 
     //client.accept(MediaType.APPLICATION_JSON); 
     client.header("ST_USER", "gasid3"); 
     client.header("Content-Type", MediaType.APPLICATION_JSON_TYPE); 
     LOG.info("== Invoking REST Call for service ==" + url); 

     //till this 

     /*Set<String> UniqueDmsId = new HashSet<>(dmsIdList); 
     dmsIdList.clear(); 
     dmsIdList.addAll(UniqueDmsId);*/ 

     return dmsIdList; 
    } 




    private String getServiceUrl(STouchServiceEnum stouchService) 
    { 
     String url=""; 
     switch(stouchService) 
     { 
     case CUSTOMER: 
      //url="http://BLRKEC327951D:8080/stouch-admin-services/api"; 
      url=customerUrl; 
      //url=customerUrl.concat("/User/dmsMap"); 
      break; 
     case VEHICLE: 
      url=vehicleUrl.concat("/User/dmsMap");; 
      break; 
     default: 
      break; 
     } 
     return url; 
    } 

} 

在上面的代碼中,我想CustomerServiceProxy到在一個線程然後vehicleServiceProxy在另一個被執行,我想聚集的結果,一旦兩個線程執行得到完成。

有人可以幫助我嗎?

+0

這似乎是您的解決方案:https://stackoverflow.com/questions/ 3489543 /如何調用java中的獨立線程的方法 –

+0

嗨,在他們已經通過傳遞參數手動實現了runnable和創建對象的鏈接,但在我的情況下它是匿名線程和彈簧正在爲我的班級創建對象。我如何從我在我的類中聲明的多個線程向我的arrayList添加值? –

回答

0

您可以使用ExecutorService這個:

ExecutorService executor = Executors.newFixedThreadPool(2); 
List<Future<String>> futures = new ArrayList<>(); 

Future<String> future1 = executor.submit(new Callable<String>() { 
    @Override 
    public String call() throws Exception { 
     // Add first proxy logic here and return the results 

     String jaxRsOutput = /* Gather output */; 
     return jaxRsOutput; 
    } 
}); 

futures.add(future1); 

Future<String> future2 = executor.submit(new Callable<String>() { 
    @Override 
    public String call() throws Exception { 
     // Add second proxy logic here and return the results 

     String jaxRsOutput = /* Gather output */; 
     return jaxRsOutput; 
    } 
}); 

futures.add(future2); 

for (Future<String> stringFuture : futures) { 

    try { 
     // .get blocks until thread is done 
     String output = stringFuture.get(); 

    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 
} 

executor.shutdown(); 

這將同時執行你的邏輯和,直到Thread進行,將彙總數據。

+0

嗨,我有兩個代理電話,我可以添加其中兩個,因爲我已經在我的代碼中標記? –

+0

@ adithyan.p是的,你可以。我更新了我的答案,以更多地匹配您所要求的內容。 –

0

您需要混合和聚合器。

  • 要麼你可以使用線程池,並有回調
  • 或者,你可以使用Apache的駱駝圖書館作爲參考