我可以从微服务中获取多个响应吗?它将查询MongoDB

时间:2019-04-23 14:08:16

标签: java spring tomcat executor

我有Service,它将根据提供的某些参数通过查询MongoDB来给我们答复。

@RequestMapping(value = "/custRef/{custRef}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> getServiceId(@PathVariable("custRef") String custRef) throws InterruptedException {
    System.out.println("Thread.currentThread().getName() :"+Thread.currentThread().getName());
    String serviceId=//calling Mongo Service and getting the result 
    if(custRef == null) {
        return new ResponseEntity<String>("No service id available for the given FO Id:" + custRef,HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<String>(serviceId,HttpStatus.OK);
}

我有另一个客户端,将通过提供适当的参数来调用上述服务。我想使用10个线程来调用上述服务。我可以从上述服务以相同的频率获得响应,还是需要在运行以上服务的服务器上进行任何配置

ExecutorService es = Executors.newFixedThreadPool(50);

for (RouterInfo router : listOfcpeRouterInfo){
Future<String> serviceIDS = es.submit(new CalculationTaskA(router.getCustomerRef(), rieClient));
}

@Override
public String call() throws Exception {
    String currentThreadName = Thread.currentThread().getName();
    log.info("##### [" + currentThreadName + "] <" + taskId + "> STARTIING #####");
    // System.out.println("[" + currentThreadName + "] <" + taskId + ">
    // Sleeping for " + sleepTime + " millis");
    // TimeUnit.MILLISECONDS.sleep(sleepTime);
    //
    String serviceId = null;
    try {

        ///
        serviceId = rieClient.getObject(customerRef);
        log.info("serviceId for given foid: " + customerRef + " is " + serviceId);
    } catch (ParseException pe) {
        log.error("error while parsing Data", pe);
    }
    log.info("****** [" + currentThreadName + "] <" + taskId + "> DONE ******");
    return serviceId;
}
 calling above service
enter code here
 Inside  getObject I am doing below
ResponseEntity<String> response=restTemplate.exchange(this.serviceIdUrl+"/{foId}",HttpMethod.GET,entity,String.class,foId);

1 个答案:

答案 0 :(得分:0)

默认情况下,Spring Boot应用程序是多线程的,因此您的代码示例

ExecutorService es = Executors.newFixedThreadPool(50);

for (RouterInfo router : listOfcpeRouterInfo){ Future serviceIDS = es.submit(new CalculationTaskA(router.getCustomerRef(), rieClient)); }

仅在您的“ Mongo”服务中需要 ,如果要通过 进行异步调用, “蒙哥”服务。