Как вызвать метод @Service из @Component - PullRequest
0 голосов
/ 18 января 2019

У меня есть компонент, из которого я хотел бы вызвать метод обслуживания. Когда я пытаюсь позвонить, ничего не происходит. Служебный метод - это завершаемый метод будущего, который вызывает API отдыха. Может ли это иметь какое-либо отношение к этому?

Я пытался вызывать его из разных классов, и он работает. Только этот класс является проблемой.

Фрагмент класса, из которого я звоню:

@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);


}

Я ожидаю, что смогу получить результат от метода сервиса, который получает результаты от API отдыха.

...