Help with async

I tryng to follow this sample https://cookbook.vaadin.com/long-running-task (But in springboot 6 the ListenableFuture is deprecated and I need to use CompletableFuture. so I create this class:

@Service
public class ConferenciaMatriculaService {
@Async // runs in a separate thread (enable with @EnableAsync in a config class)
public CompletableFuture longRunningTask() {
try {
System.out.println(“para para para para”);
Thread.sleep(6000);
} catch (InterruptedException e) {
return CompletableFuture.failedFuture(new RuntimeException(“Error”));
}

    return CompletableFuture.completedFuture("oi");
}

}

and in my main I use:

UI ui = UI.getCurrent();
conferenciaMatriculaService.longRunningTask().whenCompleteAsync((a,b)->{ui.access(() → {
btnFinalizar.setText(“foiiiii”);
});
});

When I click the button, the service starts
but the text of my Button (BtnFinalizar) only changes if I change anything at my screen (sample when I click in a Select )

You have to add the @Push annotation on your configuration class.

See here an example of correct pattern to use completable future with Push, vaadin-create23/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/BooksPresenter.java at master · TatuLund/vaadin-create23 · GitHub and vaadin-create23/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/BooksView.java at master · TatuLund/vaadin-create23 · GitHub

Another neat example is to extend the button and create async button that executes the given task This is an example of how to reduce boiler plate code in Vaadin when using Push. This is a button that runs a given task in the background thread. When task is completes, its return value is given to update function, that is run in UI.access(). Button on is disabled on clicking it and re-enabled once the task is completed. · GitHub

If you extend your async button with https://github.com/vaadin/web-components/issues/842#issuecomment-1007331620 it also looks awesome

I was forgot to put the @Push
But now I have one weird behavior

I have this code:

UI ui = UI.getCurrent();
conferenciaMatriculaService.longRunningTask(matricula.substring(0,matricula.length()-5), itensMatricula).whenComplete((a,b)->{
if(b != null){
System.out.println(“nao erro”);
ui.access(() → {
btnFinalizar.setEnabled(true);
btnFinalizar.setVisible(true);
progresso.setVisible(false);
Utils.showNotificacaoSucessoSimples("Algum erro na hora de processar: " + b.getMessage());
});
}else {
System.out.println(“erro”);
ui.access(() → {
btnFinalizar.setEnabled(true);
btnFinalizar.setVisible(true);
progresso.setVisible(false);
Utils.showNotificacaoSucessoSimples(“Indicador atualizado”);
});
}
});

so when I click for the first time in my button, works fine… but if I click a second time in my button the btnFinalizar.setEnable(true) dont works.
The button apper (.setVisible) the progress.setVisible(false) works. but the button.setEnabled(true)

** in both time that I click the button . print (“nao erro”) in my console. so the logic is correct

@yummy-rhino and @adaptable-uakari I create one project to simulate the erro: https://github.com/fabioebner/vaadin-button-error tks for help

The problem seem to be

btnFinish.setDisableOnClick(true);

You should open an issue:

IMO you don’t need this code as you can do that in the click listener