首页 > 解决方案 > 如何在可运行文件中进行休息 api 调用?

问题描述

我有几个阅读器,我不断地阅读 rfid 标签。我想用数据库中的内容检查这些标签。我有一个 spring 应用程序,它使用 runnables 连接并从读者那里获取信息。我正在将标签添加到队列中。我有一个单独运行的rest api,该应用程序将使用它。我想在阅读器应用程序运行时使用这个 api 来多次检查数据库以及向数据库添加信息。我该怎么做呢?

我尝试了多种方法,例如使用 completablefuture,尝试使用 webflux,使用 resttemplate 进行 api 调用,webclient 进行 api 调用,但似乎没有任何效果。我有一个执行器服务,它从主要调用读者。每个阅读器都是用一个可运行文件创建的。runnable 创建 Reader 并调用该函数。在该函数中,启动了 2 个可运行任务。在其中一项任务中,我想调用其余 api。我将尝试包含所有必要的代码。

这是从 main 调用的类:

@Component
@Scope("prototype")
public class CSLReader{
public CSLReader(String ipAddress){
    this.ipAddress=ipAddress;
}

public CSLReader(String ipAddress, String deviceName, int power, int 
notifyPort, int dwellTime ) throws InterruptedException{
    this.ipAddress=ipAddress;
    this.deviceName=deviceName;
    this.power=power;
    this.notifyPort=notifyPort;
    this.TagBuffer = new LinkedList<TagInfo>();
    //this.taglist=new ArrayBlockingQueue<TagInfo>();
    this.dwellTime=dwellTime;
    //Start();
}

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());
                    ex.printStackTrace();
                }
            }


        };
        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();
    }

我在服务文件中有这个:

@Async
public CompletableFuture<BmwvehicleTest> findVehicle(String rfidtag) throws InterruptedException{
    log.info("trying to find a vehicle test by rfidtag");
    String getUrl=String.format("http://localhost/api/tag/", rfidtag);
    BmwvehicleTest results= restTemplate.getForObject(getUrl,BmwvehicleTest.class);
    Thread.sleep(2000L);
    return CompletableFuture.completedFuture(results);
}

然后我尝试在我的主要原型组件中调用它:

public void StartInventory() throws InterruptedException,  
   ExecutionException{      
ArrayBlockingQueue<TagInfo> taglist= new ArrayBlockingQueue<TagInfo>(10000);


    synchronized (TagBuffer) {
        if (TagBuffer.size() >= 10000){
            TagBuffer.remove();
        }
//test tag
        TagInfo tag2= new TagInfo(51.2f, 2, "192.168.68.68", "Test Reader", new Date(), 0, "E200287878787878787", "3400");
        if (tag2 != null){

            TagBuffer.add(tag2);
            System.out.println("tag added to tag buffer");
            log.info("the tag is: "+tag2.epc);  

CompletableFuture<BmwvehicleTest> num1=asyncServices.findVehicle(tag2.epc);
}
}
}

我希望该应用程序继续接收来自读者的信息,并在收到标签时将标签添加到队列中,然后检查是否在数据库中,如果是,我希望将位置添加到数据库中。虽然发生这种情况,但我仍然希望该应用程序继续接收来自读者的信息。

标签: javaspringspring-bootconcurrencyspring-async

解决方案


推荐阅读