2017-09-11 234 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的骆驼图书馆作为参考