How to show task steps from log in a control(text area) ? Java, 23 (for now)

Hi all! I have an UI package, and a “core” package where all the codes for my tasks are. The UI has the vaadin dependency, but the core does not include it. The user can start the tasks by pressing a specific button on the UI. These tasks have predefined steps, which they are logging with log4j. Is there a way to show the user after pressing the start button what step the task is currently doing? Example could the core task write back to the UI to a text area the same line as it writes to the log?.. Or can we pull up the logged rows somehow to the UI? And only while the task is running… Thank you for reading :)

You can achieve this by defining an interface class in your core project where you define the methods to be called when each of the steps are done, and then implement said interface in the UI. This way, the core task does not need any vaadin dependency at all.

Here’s a small example to show what I mean.:

public class CoreTask {
    
    public static doCoreTask1(CoreTask1Listener listener) {
        // implement first step here
        listener.firstStepFinished();

        // implement second step here
        listener.secondStepFinished();
    }

    public interface CoreTask1Listener {
        void firstStepFinished();
        void secondStepFinished();
    }
}
@Route("tasks")
@Push // this annotation is needed to use `ui.access(() -> {...}`
public class UiTasksView extends VerticalLayout implements CoreTask.CoreTask1Listener {

    private UI ui;

    public UiTasksView() {

        // since you want to show updates in the UI before the whole task has finished, 
        // you'll need to do the core task inside another Thread, and then update the ui using 
        // ui.access(...)
        // for that, you need to get the current UI outside of (!!) the background thread
        this.ui = UI.getCurrent();

        add(new Button("perform Core Task 1", click -> {
            Thread backgroundThread = new Thread(() -> {
                CoreTask.doCoreTask1(this);
            }
            backgroundThread.start();
        }
    }

    public void firstStepFinished() {
        // you can of course also add a Div below the button and update its text here, or anything else, really
        // the sky is the limit, I'm only demonstrating the architectural approach
        ui.access(() -> {
            Notification.show("step 1 complete!");
        }
    }

    public void secondStepFinished() {
        ui.access(() -> {
            Notification.show("step 2 complete!");
        }
    }

}

Related documentations:

Thank you very much @Kaspar4 :)