Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

spring - Vaadin: get reference of UI to change data

I want to change data inside a Vaadin UI. The change is invoked by a a rest call. There, i somehow need a reference to the UI class to call its method′, e.g. changeValue(string value).

I'm using vaadin-spring-boot-starter 1.0.0

Is that somehow possible?

EDIT: Another question now: I was trying to do that Server Push, mentioned by @Eric, inside of a View, so that the view will get updated on a Broadcast message. However, this is not working (no exceptions, nothing to debug, just no updates in the view). This is what i do in my View:

@UIScope
@SpringView(name = LoadWebsiteView.VIEW_NAME)
@Push
public class LoadWebsiteView extends VerticalLayout implements View, Broadcaster.BroadcastListener {
...

@Autowired
public LoadWebsiteView(ScraperMainUI scraperMainUi) {
    this.scraperMainUi = scraperMainUi;
    Broadcaster.register(this);
    initControlPane();
}

@Override
public void receiveBroadcast(String message) {
    scraperMainUi.access(new Runnable() {
        @Override
        public void run() {
            urlTxtField.setValue(message);
        }
    });
}

and here is the simple stuff i do in my restcontroller:

Broadcaster.broadcast(text);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you are looking for is Vaadin's Push feature and a way to send a message to a list of registered "clients" (in this case, the Vaadin UIs who need to known about the changes).

You can read about Vaadin Push here: Enabling Server Push and also in the article Advanced Push

The Vaadin push function allows your server to force updates to the client instead of waiting on the browser to request again.

The message component simply acts as a way to tell subscribed UIs that there is an update they need to action.

This said, I have a project that does about the same as multiple users are actioning items and there are Spring scheduled tasks that also can effect changes the user needs to know about.

Note, the below examples are based on the examples available in Enabling Server Push article.

Broadcaster.java - Acts as the mechanism that registers instances to receive broadcasts and provides a facility to send broadcasts. In the below example, I have I have a class that represents a message (BroadcastMessage) but you could simplify it of course.

public class Broadcaster implements Serializable {

    private static final long serialVersionUID = 3540459607283346649L;

    static ExecutorService executorService = Executors.newSingleThreadExecutor();

    private static LinkedList<BroadcastListener> listeners = new LinkedList<BroadcastListener>();

    public interface BroadcastListener {
        void receiveBroadcast(BroadcastMessage message);
    }   

    public static synchronized void register(BroadcastListener listener) {
        listeners.add(listener);
    }

    public static synchronized void unregister(BroadcastListener listener) {
        listeners.remove(listener);
    }

    public static synchronized void broadcast(final BroadcastMessage message) {
        for (final BroadcastListener listener: listeners)
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    listener.receiveBroadcast(message);
                }
            });
    }
}

Here is the class I defined for my BroadcastMessage. The idea is to have a way to denote what kind of message I have and also some payload in the form of a Map

public class BroadcastMessage implements Serializable {

    private static final long serialVersionUID = 5637577096751222106L;

    public BroadcastMessageType messageType;
    public Map<String, String> params;

    public BroadcastMessage() {
    }

    public BroadcastMessage(BroadcastMessageType messageType) {
        this.messageType = messageType;
        this.params = new HashMap<String, String>();
    }

    public BroadcastMessage(BroadcastMessageType messageType, Map<String, String> params) {
        this.messageType = messageType;
        this.params = params;
    }

    public BroadcastMessageType getMessageType() {
        return messageType;
    }
    public void setMessageType(BroadcastMessageType messageType) {
        this.messageType = messageType;
    }
    public Map<String, String> getParams() {
        return params;
    }
    public void setParams(Map<String, String> params) {
        this.params = params;
    }

}

This is an example Vaadin UI that wants to listen for Broadcasts. Note the @Push annotation. Without this, the client will only refresh when the browser decides to. @Push makes it immediate**

@SpringComponent
@UIScope
@Push
@SpringView(name=TaskListComponent.NAME)
public class TaskListComponent extends MyCustomComponent implements Broadcaster.BroadcastListener, View {

    /**  PRUNED DOWN, TO DEMONSTRATE THE KEY CODE  **/

    // Register this window when we enter it
    @Override
    public void enter(ViewChangeEvent event) {
        Broadcaster.register(this);

    }

     // Must also unregister when the UI expires    
    @Override
    public void detach() {
        Broadcaster.unregister(this);
        super.detach();
    }

    // Receive a broadcast
    @Override
    public void receiveBroadcast(BroadcastMessage message) {

        getUI().access(new Runnable() {
            @Override
            public void run() {
                // DO WHATEVER YOU NEED TO DO HERE.
                // I CALLED INITIALIZE BUT IT COULD BE 
                // JUST YOU FIELD UPDATE
                if ( message.getMessageType().equals(BroadcastMessageType.REFRESH_TASK_LIST) )
                    initialize();
            }
        }); 

    }

}

To send a message from your rest interface:

Broadcaster.broadcast( 
    new BroadcastMessage( 
        BroadcastMessageType.AUTO_REFRESH_LIST
    ) 
);

Hope this helps! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...