Call service asynchronous

Hi,

After a search, if the number of results is to large, we propose to download these results into csv file.
But, the goal is to allow the user to continue his browsing during download without gaming performance. Can I do an asynchronous call for download csv file ? When the file is finally writed,the user has a popup for saving his file.

Is it possible to do a download in background mode in order to keep performance ?

Thanks for your service.

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

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.

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