0

i have following class where spring create object for the class. i have created WebClient to make http request using CXF. 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 .

@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.

can somebody help me on this?

adithyan .p
  • 121
  • 1
  • 3
  • 12
  • this seems to be your solution: https://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java – Japu_D_Cret Sep 11 '17 at 12:37
  • hi, in the link they have implemented the runnable and creating object manually by passing param but in my case it's anonymous thread and spring is creating object for my class. how do i add values to my arrayList from multiple thread which i have declared in my class ? – adithyan .p Sep 11 '17 at 12:47

2 Answers2

0

You can use ExecutorService for this:

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.

Albert Bos
  • 2,012
  • 1
  • 15
  • 26
0

You need a mix and an aggregator.

  • Either you can use threadPools and have callbacks
  • or, you can use Apache Camel library as a reference
Sunil Singhal
  • 593
  • 3
  • 11