Marcio Jota Coelho
Marcio Jota Coelho

Reputation: 1

Execute async methods with quarkus application

Based on this short answer, how could I execute the same method if it had more than one arguments?

Like This:

    @ApplicationScoped
    public class WorkA implements Work {
    
        @Override
        public void doWork() {
            /*
            * How to execute worker async?
            */
        }
    
        private Uni<Void> worker(UUID uuid,Integer one, List<Strings> listExample) {
            log.info("Starting work: " + uuid);
            try {
                Thread.sleep((long) Math.random() * 1000);
            } catch (InterruptedException ex) {
                log.info("Could not finish work: " + uuid);
                throw new RuntimeException(ex);
            }
            log.info("Finish work: {}.", uuid);
            return Uni.createFrom().voidItem();
        }
    }
    
    @ApplicationScoped
    public class StartWork {
    
        @Inject
        Work work;
    
        public void startWork() {
            work.do();
            System.out.println("I dont' care when and if the work finished, but it has started.");
        }
    }

I need to execute the method worker with Uni.createFrom().

Upvotes: 0

Views: 909

Answers (1)

Bevor
Bevor

Reputation: 8605

In that case, you cannot use the "method reference" syntax. Use the lambda syntax instead, so you can pass the worker with random arguments. Example:


  public void executeAsync(UUID uuid, Integer one, List<String> listExample) {
    Uni.createFrom()
        .item(() -> worker(uuid, one, listExample))
        .emitOn(Infrastructure.getDefaultWorkerPool())
        .subscribe()
        .with(
            Uni::log,
            Throwable::printStackTrace
        );
  }

  private Uni<Void> worker(UUID uuid, Integer one, List<String> listExample) {
    // do something

    return Uni.createFrom().voidItem();
  }

Upvotes: 0

Related Questions