Extensions usually consist of a pair of s like components do. Hence, they can use the regular shared state and RPC mechanisms to communicate between the client and the server. Extensions may or may not have a UI. They can create and display widgets on the client side, but are not part of the regular layout hierarchy.
We will rewrite the add-on as an extension. The Refresher causes the client to “ping” the server at regular intervals, allowing the server to keep the client up-to-date if the application state is changed eg. by a background thread (because of the way Vaadin works, the server cannot itself initiate communication.)
We start by writing the barebones server-side class for our extension:
Java
Next, the Refresher needs an RPC interface to ping the server and a shared state to keep track of the interval. These are rather trivial:
Java
The client-side connector is just like a component connector except that we inherit from AbstractExtensionConnector
, not . We do not write a client-side widget at all, because the Refresher does not have a UI.
We create a Timer
instance that calls the refresh
RPC method when run. In , we know that either the interval, enabled state, or both have changed, so we always cancel a possible currently-running timer and schedule a new one if we’re enabled. We also remember to cancel the timer when the extension is detached.
Java
Finally, we add an event listener interface and some accessor methods to Refresher
. There is nothing extension-specific in the following code: