Vaadin Spring Security

258 views Asked by At

I have been working from an example I found, here's the link to the Git repo:

https://github.com/basakpie/vaadin8-spring-security-sample

It works great, it's just what I need, except for one thing: I need Server Push.

Here's what I've done so far:

  • added the Vaadin Push dependency
  • added the following lines to the start of the MainUI.init() method:

    getPushConfiguration().setTransport(Transport.WEBSOCKET);
    getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
    
  • Added the following fields to the MainUI class:

    Label time = new Label();
    Timer timer;
    
  • Added the following method to the MainUI class:

    private void updateTime() {
      access(() -> time.setValue(String.format("The server-side time is %s",  LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")))));
    }
    
  • Finally, added the following to the end of the MainUI.init() method:

    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            updateTime();
        }
    }, 1000L, 1000L);
    

It mostly works. I am able to see the current system time updating every second. But when I hit refresh in the browser, the application just hangs with the vaadin loading spinner. There are no error messages.

I have tried the following alternatives:

Adding the method

public void attach() {
    getPushConfiguration().setTransport(Transport.WEBSOCKET);
    getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
}

and removing the getPushConfiguration lines from init()

This solves the hanging problem, but the push does not work - no errors, just the time is not displayed at all.

I also tried adding a @Push annotation to MainUI. This results in the same behaviour as before - freezing on refresh.

How can I fix this? Any suggestions would be welcome.

2

There are 2 answers

1
Duncan O. N. On

Try out the below procedure:

  1. Add @Push to the MainUI.java file

    @Push(transport = Transport.WEBSOCKET,value = PushMode.AUTOMATIC)

  2. Instead of :

    getPushConfiguration().setTransport(Transport.WEBSOCKET); getPushConfiguration().setPushMode(PushMode.AUTOMATIC);

  3. Add @PreDestroy to exit the timer when you navigate from the mainUI

    @PreDestroy void destroy() { timer.cancel(); }

Find the complete revised code HERE

https://gist.github.com/cansoftinc/351452ee0e616d353519f147c4a961ba

0
gkatzioura On

I Had exactly the same problem and my solution to this was to implement the vaadin servlet. Check this for more information.