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): 565780121onAttach 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): 1181023619onAttach running for
MainView
class. attachEvent.toString(): com.vaadin.flow.component.AttachEvent[source=com.basilbourque.example.MainView@2390403]. System.identityHashCode(this): 37291011onAttach 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.