0

I took the given Vaadin 11.0.1 app Base Starter and merely added an override of the onAttach method. This method is hook invoked by the Vaadin framework when your component is attached to the display on screen. So, if running the app once, we should see this method invoked once.

package com.basilbourque.example;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
public class MainView extends VerticalLayout {

    public MainView () {
        Button button = new Button( "Click me" ,
            event -> Notification.show( "Clicked!" ) );
        add( button );
    }

    @Override
    protected void onAttach ( AttachEvent attachEvent ){
        System.out.println( "onAttach running for `MainView` class. attachEvent.toString(): " + attachEvent + ". System.identityHashCode(this): " + System.identityHashCode(this));
    }
}

When run with the bundled Jetty web container, I see:

onAttach running for MainView class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@454ff330]. System.identityHashCode(this): 1162867504

But when I run with Tomcat 9.0.12 externally to be invoked by IntelliJ Ultimate edition 2018.3, I see this:

onAttach running for MainView class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@21b91e99]. System.identityHashCode(this): 565780121

onAttach running for MainView class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@5ee75022]. System.identityHashCode(this): 1592217634

Note that the identity hash at the end of both outputs is different. So it seems that I am getting two instances of MainView created despite only having one user (a single page opened in the browser automatically by IntelliJ after deploying and launching Tomcat).

➥ Why is onAttach running twice on external Tomcat but only once on internal Jetty?

Perhaps related to this Question and this Question?


Even weirder… If I change the Application context of the IntelliJ Run/Debug Configuration (on 2nd tab, Deployment) from /bogus_war_exploded to just / , then I get three instances of MainView running.

onAttach running for MainView class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@4664fd83]. System.identityHashCode(this): 1181023619

onAttach running for MainView class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@2390403]. System.identityHashCode(this): 37291011

onAttach running for MainView class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@461aa7e4]. System.identityHashCode(this): 1176152036

Still more weirdness… If I turn off the feature to automatically open a URL to my Tomcat app in a web browser upon deployment, that is, if I uncheck the After launch checkbox in the Open browser section of the first tab Server in the Run/Debug Configurations, then I get only the single MainView instance as expected. I have to manually open a page in a browser, and paste the URL http://localhost:8080/bogus_war_exploded. Yeah, success!

But what is going on here? Is there a bug related to IntelliJ auto-opening the URL in a browser? If so, I suppose a workaround is to manually open the browser & URL. Or is something else going on?


Ironically, I quit using NetBeans and bought IntelliJ to avoid the same kind of buggy behavior there. See Stack Overflow, Tomcat deploying the same application twice in netbeans.


I am using:

  • IntelliJ Ultimate edition 2018.3
  • Java 10.0.2 via Zulu JVM by Azul Systems (OpenJDK-based)
  • Tomcat 9.0.12
  • Jetty 9.4.11.v20180605
  • Project created from Base Starter
  • Vaadin 11.0.1
  • macOS High Sierra
  • MacBook Pro (Retina, 15-inch, Late 2013), 16 gigs memory.
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Just a quick note since I don't have time right now to dig into this more thoroughly right now. I suspect you're actually seeing attach events for two separate `MainView` instances. You can verify this by also logging e.g. `System.identityHashCode(this)`. It's then another question why there would be two different `MainView` instances, but it would at least help give ideas on where to look. – Leif Åstrand Nov 05 '18 at 08:14
  • Are the stacktraces to the `onAttach` event somehow different in the tomcat scenario? There's multiple places in the initialization of a servlet container that vaadin could hook into. – Joakim Erdfelt Nov 05 '18 at 14:17
  • @LeifÅstrand I updated my Question’s code per your suggestion. The identity hash code is indeed different, meaning two instances of `MainView` are being created for a single user. – Basil Bourque Nov 08 '18 at 05:00
  • Sorry for not following up on this one. Multiple different `MainView` instances do indeed imply that there are multiple browser requests. If you want to dig deeper, you could additionally print various details about the actual requests that can be found using `VaadinServletRequest.getCurrent()`. Some particular things to look for there could be the requested path (`getPathInfo()`, `getServletPath()` and `getContextPath()`) and the user agent header (`getHeader("user-agent")`). – Leif Åstrand Feb 04 '19 at 09:55

0 Answers0