[英]how to execute the set of line in multiple thread java
i have following class where spring create object for the class. 我有以下课程,春季为课程创建了对象。 i have created WebClient to make http request using CXF. 我创建了WebClient以使用CXF发出http请求。 i want to make 2 http call which i want to execute in 2 thread so that, i dont have to call the method twice . 我想进行2个我想在2个线程中执行的http调用,所以我不必两次调用该方法。
@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;
}
}
In above code i want CustomerServiceProxy to be executed in one thread then vehicleServiceProxy in another and i want to aggregate the result once the two threads execution got completed. 在上面的代码中,我希望在一个线程中执行CustomerServiceProxy,然后在另一个线程中执行vehicleServiceProxy,一旦两个线程执行完成,我想汇总结果。
can somebody help me on this? 有人可以帮我吗?
You can use ExecutorService
for this: 您可以为此使用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();
This will execute your logic concurrently and will aggregate the data until the Thread
is done. 这将同时执行您的逻辑并将聚合数据,直到Thread
完成。
You need a mix and an aggregator. 您需要一个混合和一个聚合器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.