muzii
muzii

Reputation: 1

Vaadin ui ready event

in a cloud context I need to be able to find out when a spring boot vaadin service is ready for user connections. I've tried a call to /actuator/health but the service is UP well before vaadin beeing ready ... Has someone faced the same issue and found a solution ?

Just to make it clear, my issue is more at the load balanced level. I need to find a way to tell the load balancer (I'm using sticky session) that the new spawned vaadin service is ready to receive users ... this is where I've used the /actuator/health but the service is UP before vaadin is ready.

Upvotes: 0

Views: 671

Answers (1)

gruntled
gruntled

Reputation: 448

Our application (deployed on AWS with an Application Load Balancer) is based on Spring Boot and Vaadin 14. We've been using a ServletFilter to instruct the load balancer to direct traffic:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class HealthCheckFilter implements Filter {

    public static final String HEALTH_CHECK_PATH = "/healthcheck";
    private boolean vaadinIsInitialized = false;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (request.getRequestURI().endsWith(HEALTH_CHECK_PATH) && vaadinIsInitialized) {                
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @EventListener({ContextRefreshedEvent.class})
    public void contextRefreshedEvent() {
        // invoked after Spring finishes loading the application context, which is after
        // Vaadin and the embedded application server has started
        vaadinIsInitialized = true;
    }
}

There are at least two interesting events that happen as part of the initialization of our application that could be used to indicate that the application is ready to service requests.

  1. The ServiceInitEvent - This is published to any Spring bean that is implementing the VaadinServiceInitListener (you can register for this event if you are not using Spring, IIRC). According to the documentation is is published "when a VaadinService is being initialized." which means it happens before Vaadin is fully initialized. In a trivial application, I observed this happening a few milliseconds before the application was ready to receive requests.
  2. (If you are using Vaadin with Spring) The ContextRefreshedEvent - This happens after both Vaadin and Spring are fully initialized. At the time of this event, the application is ready to service Vaadin requests - and thus it is a good candidate to use to signal to the load balancer.

Upvotes: 4

Related Questions