1

I want to use Vaadin Push in my application. I am using vaadin 7.1.2 which has vaadin push built in. I have 2 question:

  1. How to push the changes from the database on change in data in the database? How can I listen to the database changes? Is there any listeners in vaadin push which can be use?
  2. Since I have many modules in my application I want to add push functionality to only selected modules. Is it possible to add push to only selected modules?

Thanks

Abhilash

Abhilash
  • 11
  • 4
  • Similar question, [Vaadin7 Container Updates from Server - To use server push or not](http://stackoverflow.com/q/18440746/642706) – Basil Bourque Jul 17 '15 at 15:28

2 Answers2

0

Abhilash, what kind of persistence layer you are using?

Common Vaadin Container implements PropertySetChangeListener listener so you can register to and receive event for every DB change. But it will work only if you will change DB via this Container.

Well, external changes won't be noticed and no event will be provided. For this case it is a much harder to get noticed about these DB changes. You should implement kind of "middleware", which will handle all DB changes and also it will notify all registered listener.

To second question, I'm not sure what you mean with modules? Could you provide more information with examples?

Milan Baran
  • 4,133
  • 2
  • 32
  • 49
0

How to push the changes from the database on change in data in the database? How can I listen to the database changes? Is there any listeners in vaadin push which can be use?

No, no silver bullet there.

Databases do not generally have an event-notification system to alert external systems about changes to the stored data. You need to code such behavior yourself.

Postgres NOTIFY

The Postgres database offers an unusual feature of NOTIFY where a client connected to the database server can be prodded from server-side code such as PL/pgSQL. The server-side code can ship an optional “payload” string to the client. The client-side database-connection implementation must be coded to accept such notifications.

If you have such a client, then you could do something like have triggers that fire when saving changes to certain tables, then use NOTIFY to tickle the client into querying the table name passed as the payload.

This is nowhere near standard SQL, and is a Postgres-specific feature.

Vaadin events

If the only source of changes to the database is within your Vaadin app, you could set up some kind of alert system within your app.

The ServletContext is required by the Servlet spec to represent your web app at runtime. You can get/set “attributes” as a way for the various threads and user sessions to communicate with each other.

You would need a way to track all your user sessions, as discussed here.

Taking a Vaadin-centric approach may not be practical if there is any chance of other sources of changes to the database outside of your app.

For more info on messaging between current Vaadin users, see the Broadcasting to Other Users section in Vaadin docs, as mentioned in this Answer to a similar Question.

Polling

One common solution is polling. Spawn a background thread to query the database, report findings, then sleep. Lather, rinse, repeat. Set the sleep time for the amount of time your users are willing to be out-of-date.

This kind of work is made much easier with a ScheduledExecutorService built into Java SE. Alternatively, Java EE offers an @Schedule annotation as discussed here, but I am unfamiliar with its usage. Either way, you are scheduling a chunk of code to be run repeatedly. By the way, never use the Timer class in a Servlet-based app or Java EE app.

I have used the ScheduledExecutorService successfully in a Vaadin 7 app running in Tomcat 8.5. Learn about the ServletContextListener as a place in your app to launch and shutdown your executor service. See my slides for my talk on the subject.

And be sure to never access the Vaadin user-inteface layouts and widgets from a background thread. Instead, always interact with the UI by calling access or accessSynchronously on the UI class for user-interface related changes or VaadinSession for non-user-interface related changes. Read the Accessing UI from Another Thread section of the Server Push page in Vaadin doc.

Push technology updates

Push technology has been a rapidly evolving field.

As I recall, Vaadin 8 may be much more efficient with Push than Vaadin 7, though I do not recall details. At the very least, Vaadin 8 may be using more recent versions of the Atmosphere library that powers Vaadin Push features. So if possible, you may want to consider migrating to Vaadin 8. Keep in mind that Vaadin 8 has a compatibility layer feature to make it easier for you to bring over Vaadin 7 code.

Most crucial, be sure to use the latest versions of your web container such as Tomcat or Jetty. The support for WebSocket in particular has had significant improvements over the years.

While perhaps not yet ready in practice, the Servlet 4 spec has major implications for the future of Push technology. The spec includes support for HTTP/2 and Request/Response multiplexing to help with server-side push.

Vaadin Push scoped to UI

Since I have many modules in my application I want to add push functionality to only selected modules. Is it possible to add push to only selected modules?

Enabling Push significantly alters your deployment situation, and so you are wise to carefully consider its use.

Vaadin’s support for Push is scoped to your subclass of UI. Your Vaadin app by default has a single UI object for each user, from a single subclass of UI class. But your are free to instantiate additional UI objects within your user’s session. The instances may come from your same UI subclass, or from additional UI subclasses you have authored.

This is precisely how the multi-window/multi-tab support works in Vaadin 7 and Vaadin 8: You instantiate a new UI subclass object and install it into the new web browser window or tab.

I am not sure, but you may be able to swap out a Push-enabled UI object for an alternative non-Push-enabled UI object within the same web browser window/tab. But I have not tried doing so, and I do not know if this is supported or recommended. Personally, I would choose to keep the same UI object installed for the entire life of the window/tab.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154