Application Threading and Data Streaming Questions

Hello,

I am new to Vaadin, and I’m trying to ‘port’ an existing Android based client-server application to use Vaadin. I’m making progress, but I need the following questions answered:

  1. Does Vaadin have the concept of a main/UI thread (like Swing and Android do)? If so, how can I determine if a given section of code is running on the main thread (i.e. I want to call isMainThread() and have that return true/false).

  2. If Vaadin does not have the concept of a main/UI thread, then how does updating the UI work, when calls are made on the server side from any random thread? You can ignore this question if the answer to question #1 is that Vaadin does indeed have a main/UI thread concept.

  3. The application I’m trying to port to Vaadin essentially has the client poll the server every 500ms using a custom communication protocol (socket based), and the server responds with the necessary data updates, and the client deserializes the responses and updates the UIs on the main UI thread. The data communication between client and server is done on a background thread using the aforementioned socket. How would I best accomplish this in Vaadin? I see there’s an “ICEPush” add-on, but I’m not sure it’s applicable to my case, since I really do want to poll (please don’t ask me why… in our app requirements, polling works better).

  4. Does Vaadin support Websockets, and if so, where can I find examples/documentation? Is this the answer to my data communication issues?

Thank You,
Eric

Hi,

No. Vaadin is a web application: all of the of threading is left to the Application Server. All Application Servers I know of (Websphere, Tomcat, Jetty) all work with a pool of threads, and each one is allocated to deal with a request and response and then returned to the pool. This allows for concurrent activity (i.e. several simultaneous requests) without having to create threads. This somewhat of a simple explanation, but this is standard web app stuff : I’m sure there are better generic tutorials and explanations elsewhere.

In core Vaadin, it doesn’t : to do this, you would use an add-on such as the Ice Push plugin, the Refresher or
For more details, see
this “sticky” note on the forum

Can we assume you have a separate thread on the server updating the data? Or is the “poll” getting the data and updating the components. All the add-ons in the directory (Refresher, IcePush, DontPush) are “simply” concerned with ensuring that the updated UI components are updated in the browser. None of them (to my knowledge) will actually allow you to execute any code.
Is the 500ms polling to ensure that any data changes are updated to the UI every 500ms? Or that the data is refreshed every 500ms? I would split the problem in two : in the webapp, have a 500ms polling thread on the server somewhere (one per user, one for all users? not sure : depends on the usecase), and then use one of the addons to ensure the changes are pushed to the client. If you really, really want to have a 500ms poll for the UI (even though you can’t execute any code in that poll), I’d point you towards the Refresher component. FWIW, In my (humble) opinion, a 500ms refresh is really quite high for a web application.

Not natively, no. I believe that Vaadin7 (the next big version being planned, realistically not released foranother 12 months) may have a native WebSockets push technique, but until then https://vaadin.com/directory#addon/dontpush and http://vaadin.com/directory#addon/dontpush-ozonelayer use WebSockets. Note : no polling. Note 2 : I have not used any of these add-ons, yet (I’ve been keeping track of them, as I have one use-case in the app that needs it, but I haven’t “got to it” yet!)

HTH,

Cheers,

Charles.

Thanks for the answer, but isn’t there a way I can determine if a given scope of code is running inside the user event request response cycle? Is there a boolean flag I can set somewhere that says, “inRequestReponseCycle”, and then set it back to false when the user request is done, so that I know if a given external call is going to be transmitted to the client side or not (depending on the value of that flag)? It seems like I should be able to tap into the servlet engine to find out if I’m in the middle of processing a client browser call (which would be akin to being ‘inside’ the Swing event loop in a desktop app, for example).

Hi,

There isn’t a native way to do that, no… But you can do it, yes. You could create a ThreadLocal, and then use a
ServletRequestListener
to set/unset it. On a non-interactive thread (i.e. one where the ThreadLocal is false), you wouldn’t have have access to any of the components of the UI, though, because they are stored in the HTTP Session, unless you explicitly pass the components across to the thread when it is started (you’ll need to synchronize on the component.getApplication() when updating in that case).

Cheers,

Charles.

Vaadin has an
HttpServletRequestListener
interface that lets you do this more easily, and you could avoid the thread local. Just have a boolean field in your application class and do:

    @Override
    public void onRequestStart(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) {

        inCall = true;
    }

    @Override
    public void onRequestEnd(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) {
        
        inCall = false;
    }

You could be extra safe and use an
AtomicBoolean
instead, depending on what changes are going on in your application class from the external (non request) thread.

Cheers,
Bobby