Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Call service asynchronous

baptiste prieur
3 years ago Mar 23, 2020 7:03pm
Rubén Espinoza
3 years ago Mar 23, 2020 11:52pm

Hello baptiste prieur, and why not? of course it can.

Use CompletableFuture.runAsync for example

This can be any method that you are going to process in your thread, example

public void leerAlimentosParaCrearListas()
this.lecturaAsync( AbstractFicherosAlimentos::leerAlimentosParaCrearListas );
private void lecturaAsync(final Consumer<AbstractFicherosAlimentos> consumer) {
        try {
            this.executorService = Executors.newSingleThreadExecutor();
            CompletableFuture.runAsync(() -> consumer.accept(this), executorService)
            .join();
        } finally {
            if(Objects.nonNull(executorService)) {
                executorService.shutdown();
            }
        }
        if(Objects.nonNull(executorService)) {
            try {
                executorService.awaitTermination(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                log().warn("Error");
            }
            if(executorService.isTerminated()) {
                log().info("Read ready");
            } else {
                log().warn("read incomplete");
            }
        }
    }    

More info!!! https://vaadin.com/blog/community-answer-processing-a-file-in-a-background-thread

Last updated on Mar, 24th 2020
baptiste prieur
3 years ago Mar 25, 2020 12:59pm

Thanks for your answer. I notice that I use java 7 and not yet 8.

I cannot implement this solution, even if it will probably solve my problem.

Rubén Espinoza
3 years ago Mar 25, 2020 8:22pm

baptiste prieur: Thanks for your answer. I notice that I use java 7 and not yet 8.

I cannot implement this solution, even if it will probably solve my problem.

Hello baptiste prieur, yeah yeah why not? , You can also configure your Executor to take better advantage of the hardware where it runs, perhaps a

Executors.newFixedThreadPool()
private ExecutorService executorService;
 private void processAsync() {
        try {
            this.executorService = Executors.newSingleThreadExecutor();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    callYourTask();
                }
            });
        } finally {
                if(executorService != null) {
                    executorService.shutdown();
                }
        }
        if(executorService != null) {
            try {
                executorService.awaitTermination(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                log().warn("Error");
            }
            if(executorService.isTerminated()) {
                log().info("Read ready");
            } else {
                log().warn("read incomplete");
            }
        }
    }

If you want to update any component from the server to the client, use this, and activate the @Push in your UI

ui.access(new Runnable(){
	@Override
	public void run(){}
});
Last updated on Mar, 25th 2020