Search code examples
vaadinvaadin7

Update UI from other view (Vaadin-version 7.7.12)


Currently I am facing issue for updating UI from another view the scenario is as follows

public class myWindow extends Window{

private UI ui;

@Autowired
private AbcPresenter abcpresenter;

@Override
    public void attach() {
        super.attach();
        ui = getUI();
    }

private void saveData(){
    ui.setPollInterval(1000);
        mainUI.showSpinner();//Vaadin progressbar setting visible true
         abcpresenter.saveData(param,param);
         UI.setCurrent(ui);
        close();
}

public void notifyUsers(){
try{
ui.access(() -> {
           mainUI. .hideSpinner();//hide vaadin progress bar
            Notification.show("Task done"));
        });
}catch(Exception e){
    e.printStackTrace();
}

}   

}

//here is my presenter
public class AbcPresenter(){

@Async
public void saveData(args...){
    //saving action done then notify again to myWindow class
    myWindow.notifyUsers();

}

The Problem still exists like It works If I am in the current view but if I Navigate to Other view I am not able to receive notification..!!

can there be a problem with @Async?

I am worried like the threads aren't able to communicate as it will be 2 threads according to code like something is missing that is not making request to server for UI update beacuse as soon as the process starts and if user navigates to other views(I have a side bar from which I navigate to other view from the current view (in which the process is running in background)I am not able to see notification.

I am doubting the following things..!! 1)The closing of window(I am calling the notify method) 2)@Async annotation 3)communication between the threads for server update


Solution

  • I solved this problem with many trial and errors.

    First the difference of push and poll is necessary to be understood in vaadin

    I used poll which means the client was requesting server every one second for the update.

    But my scenario was different like I kept process running in back thread and I need an update in any UI in my application that the process is been done..!!

    So for that I need push mechanism that the server inform client after the work is done..!!

    I tried with manual push and automatic push

    by the below code add in pom.xml

    <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-push</artifactId>
    </dependency>
    

    Next step is to add In MainUI class which extends UI

    the code is below

    @Push
    @SpringUI
    public class MainUI extends UI {
    }
    

    Then I tried with the push mode like PushMode.MANUAL and PushMode.AUTOMATIC the code is below

    @Push(value = PushMode.MANUAL)
    @SpringUI
    public class MainUI extends UI {
    }
    

    and

    @Push(value = PushMode.AUTOMATIC)
    @SpringUI
    public class MainUI extends UI {
    }
    

    this alone didn't work so the transport mode work for me transport =Transport.LONG_POLLING

    the code is as below PushMode I kept Manual its better to keep MANUAL

    @Push(value = PushMode.MANUAL, transport = Transport.LONG_POLLING)
    @SpringUI
    public class MainUI extends UI {
    }
    

    Transport.LONG_POLLING is it a long Get request is been kept between server and client for the update..!!

    and the method where I manually push is

    public void notifyUsers() {
            getUI().access(() -> {
            mainUI.hideSpinner();
            Notification.show.info("Task done");
            ui.push();
          });
       }
    

    Hope My explanation helps..!!