Global object and Vaadin 7

I wonder if anyone can help me.
I have a scenario where a scheduled job being run in a TimerTask.
It update an set of objects every 30min.
I need this arraylist of objects to be visible to all vaadin sessions.
Can anyone say how best this may be achieved in vaadin ?

Use EventBus from vaadin4spring or other publisher/subscriber library like: akka or guice.

If you’re not working with a database or some other external data storage and you don’t mind if the data doesn’t persist a server/application restart/redeploy you could use the Java Singleton pattern to save the Arraylist to a static variable (If you don’t know what it is just google Java Singleton there should be many examples/tutorials for it).
Static variables are shared between every User session and as a result are unique only to the Application context.

I can’t use the database for those data.
The reason is that my Timertask, using a library, gets those objects that are not serializable.

Marius, you mean that if I simple create a singleton, this will be avaiable to all the sessions ?
Is not Vaadin Server Push an alternative solution ?

Yes static Variables are -like i said- shared between sessions. You actually might not even need a Singleton. You could just make the ArrayList static to make it shared. You have to make sure though that the data is synchronized probably as you’re using a background Thread (the TimerTask). Vaadin Server Push can help in this task as it automatically pushes changes on the server to client. So if you’re changing any UI component when the data changes from a background thread these changes aren’t displayed immediately if you don’t use either Push or Poll.
For the actual “sharing” of the ArrayList Server Push doesn’t really help though.
Also note that with this technic no “event” gets fired in the other sessions. They will not be notified when the data changes. They only get the new variable when they access it the next time.

Another way you could share it which might be better in your use case (because i don’t know your use case) is this:

Using this you can send data from one user/session to other users/sessions (needs Push or Poll i think though not sure). This means though that if a new user opens the app after the message got broadcasted he will not receive it which might be desired in your case as it fires an event in the other sessions.

Thanks Marius and Lukas.
I will try using threadsafe stuf like AtomicReference and static variables.
I’ll let you know.

It works perfectly well with a static AtomicReference :slight_smile: