如何从@Component调用@Service方法

时间:2019-01-17 22:57:43

标签: java multithreading spring-boot resttemplate

我有一个要从中调用服务方法的组件。当我尝试打电话时,什么也没发生。服务方法是调用rest api的completablefuture方法。这可能与它有关吗?

我尝试从不同的类调用它,并且它可以工作。只有此类是一个问题。

我从以下位置调用的课程的片段:

@Component
@Scope("prototype")
public class Reader{  

///and other variable declarations used

@Autowired
private AsyncServices aService;


public void StartAsync() throws InterruptedException, IOException{

 Runnable task = new Runnable() {

@Override
public void run() {
   try {
    StartInventory();
 } catch (Exception ex) {
    System.out.print("start inventory thread could not start: 
 "+ex.getLocalizedMessage());}
            }
        };
        Runnable task2 = new Runnable() {

            @Override
            public void run() {
                try {
                    StartTCPClient();

                } catch (Exception ex) {
                    System.out.print("start tcpclient thread could not start: "+ex.getMessage());
                }
            }
        };

        tcpClientThread = new Thread(task2, "TCPClientThread");
       tcpClientThread.setDaemon(true);
        tcpClientThread.start();




      inventoryThread = new Thread(task, "InventoryThread");
        inventoryThread.setDaemon(true);
        inventoryThread.start();   

public void StartInventory() throws InterruptedException, ExecutionException, ParseException{

......

 //calling the method in @service class   
  aService.findVehicle(rfidtag);    

}

   code for service:
@Service
public class AsyncServices {   
private final RestTemplate appRestTemplate;

@Autowired
public AsyncServices(RestTemplateBuilder builder){
    this.appRestTemplate=builder.build();
}

@Async
 @Transactional
public CompletableFuture<BmwvehicleTest> findVehicle(String rfidtag) throws InterruptedException{
    log.info("trying to find a vehicle test by rfidtag"+ rfidtag);
     HttpHeaders headers = new HttpHeaders();
      headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
      HttpEntity <BmwvehicleTest> entity = new HttpEntity <BmwvehicleTest>(headers);
      BmwvehicleTest results=appRestTemplate.exchange("http://localhost/tag/"+rfidtag, HttpMethod.GET, entity, BmwvehicleTest.class).getBody();
    return CompletableFuture.completedFuture(results);


}

我希望能够从service方法获得结果,而service方法是从rest api获取结果。

0 个答案:

没有答案