在java中进行解锁调用

发布于 2024-10-30 21:11:49 字数 662 浏览 6 评论 0原文

我正在调用外部 api 来获取报告状态,一旦我的报告状态完成,那么我应该继续获取结果到目前为止我所做的是添加睡眠,但它是一个阻塞调用,并且报告需要大约半个小时才能完成。所以我需要一种方法来解除我下一步的障碍。谁能建议我如何实现这一目标?

 String reportStatus = client.ReportStatus(jobId).getStatus();
        while(reportStatus != "Completed"){
            int seconds = 5;
            System.out.println("Waiting:" + seconds);
            try {
                Thread.sleep(seconds*5);
            } 
            catch(InterruptedException e){
                System.out.println("Done");
            }
            reportStatus = client.ReportStatus(jobId).getStatus();

        }
  final FetchReportResult reportValues = client.fetchReport(jobId);

I am calling an external api to get a report status once my report status is complete then i should proceed ahead to fetch results What I have done so far is added a sleep but its a blocking call and report is taking some half an hour to complete. So I need a way to unblock my next steps. Can anyone suggest me how can i acheive that ?

 String reportStatus = client.ReportStatus(jobId).getStatus();
        while(reportStatus != "Completed"){
            int seconds = 5;
            System.out.println("Waiting:" + seconds);
            try {
                Thread.sleep(seconds*5);
            } 
            catch(InterruptedException e){
                System.out.println("Done");
            }
            reportStatus = client.ReportStatus(jobId).getStatus();

        }
  final FetchReportResult reportValues = client.fetchReport(jobId);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南烟 2024-11-06 21:11:49

如果您正在寻找异步调用,则应该考虑在单独的线程上调用此函数。您可以通过创建一个线程池并告诉它为您执行此代码来完成此操作。然后代码将在后台运行,直到完成,在这种情况下,您可以用它做一些事情。然后您唯一要做的就是将其包装在 Runnable 中,并在最后与 EventQueue 同步,然后就完成了。

要执行的代码:

CallBackFunction aFunction = null;
Runnable runnable = new MyRunnable(aFunction);
ExecutorService executorService = Executors.newFixedThreadPool( 1 );
executorService.execute( runnable );

CallBackFunction 接口:

public interface CallBackFunction {
  /**
   * Function that gets called when callback is finished
   */
  void callbackfinished();
}

和可运行:

   public class MyRunnable implements Runnable {

      private CallBackFunction fCallbackFunction;

      public MyRunnable( CallBackFunction callbackFunction ) {
        fCallbackFunction = callbackFunction;
      }

      @Override
      public void run() {

        //execute your polling code here//

        if(complete){
          EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
              fCallbackFunction.callbackfinished();
            }
          } );
       }
      }
    }

当执行 MyRunnable 对象时,它不会阻塞正在运行的线程。相反,它将在后台线程上运行。可运行程序将“轮询”服务器,就像您在代码中所描述的那样。最后,当轮询完成时,它会调用CallBackFunction来通知完整性发生变化。

我希望这有帮助。

PS:我在事件队列上运行回调函数。这对于您的应用程序来说可能是必要的,但如果您正在做 GUI 的事情,这可能会更安全。

If you're looking for asynchronous calls, you should look into calling this function on a separate thread. You can do this by creating a thread pool, and telling it to execute this code for you. The code will then run in the background, until it is finished, in which case, you can do something with it. Then the only thing you have to do is wrap it in a Runnable, and synchronize with the EventQueue at the end and you're set.

Code to execute:

CallBackFunction aFunction = null;
Runnable runnable = new MyRunnable(aFunction);
ExecutorService executorService = Executors.newFixedThreadPool( 1 );
executorService.execute( runnable );

CallBackFunction interface:

public interface CallBackFunction {
  /**
   * Function that gets called when callback is finished
   */
  void callbackfinished();
}

And the runnable:

   public class MyRunnable implements Runnable {

      private CallBackFunction fCallbackFunction;

      public MyRunnable( CallBackFunction callbackFunction ) {
        fCallbackFunction = callbackFunction;
      }

      @Override
      public void run() {

        //execute your polling code here//

        if(complete){
          EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
              fCallbackFunction.callbackfinished();
            }
          } );
       }
      }
    }

When you execute the MyRunnable object, it will not block the running thread. Instead, it will run on a backgroundthread. The runnable will "poll" the server, like you describe in your code. Finally, when the polling is complete, it will call the CallBackFunction to notify that there is a change in completeness.

I hope this helps.

P.S.: I run the callbackfunction on the eventqueue. This might be necessary for your application, but if you're doing GUI stuff, this might be safer.

听风吹 2024-11-06 21:11:49

您应该在另一个线程中执行此操作,然后您将能够将其作为异步调用

You should do this operation in another thread, then you will be able to make it as asynchronus call

魂归处 2024-11-06 21:11:49

使用 ExecutorService 来执行异步调用,从而请求由执行器中的线程池完成。检查 java.util.concurrent.Executors ,它包含一些线程池。然而,当应用程序退出时,何时关闭线程池以确保所有接受的请求都完成是一个问题

use an ExecutorService to perform asynchronous calls and thus request are finished by the thread pool in the executor.Check java.util.concurrent.Executors ,it contains some thread pools. Yet,it is a problem that when to close the thread pool to make sure all accepted request are finish when application exit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文