@@ -90,28 +90,34 @@ private void initialize() throws EmbeddedServletContainerException {
90
90
synchronized (this .monitor ) {
91
91
try {
92
92
addInstanceIdToEngineName ();
93
+ try {
94
+ // Remove service connectors to that protocol binding doesn't happen
95
+ // yet
96
+ removeServiceConnectors ();
93
97
94
- // Remove service connectors to that protocol binding doesn't happen yet
95
- removeServiceConnectors ();
98
+ // Start the server to trigger initialization listeners
99
+ this . tomcat . start ();
96
100
97
- // Start the server to trigger initialization listeners
98
- this . tomcat . start ();
101
+ // We can re-throw failure exception directly in the main thread
102
+ rethrowDeferredStartupExceptions ();
99
103
100
- // We can re-throw failure exception directly in the main thread
101
- rethrowDeferredStartupExceptions ();
104
+ Context context = findContext ();
105
+ try {
106
+ ContextBindings .bindClassLoader (context , getNamingToken (context ),
107
+ getClass ().getClassLoader ());
108
+ }
109
+ catch (NamingException ex ) {
110
+ // Naming is not enabled. Continue
111
+ }
102
112
103
- Context context = findContext ();
104
- try {
105
- ContextBindings .bindClassLoader (context , getNamingToken (context ),
106
- getClass ().getClassLoader ());
113
+ // Unlike Jetty, all Tomcat threads are daemon threads. We create a
114
+ // blocking non-daemon to stop immediate shutdown
115
+ startDaemonAwaitThread ();
107
116
}
108
- catch (NamingException ex ) {
109
- // Naming is not enabled. Continue
117
+ catch (Exception ex ) {
118
+ containerCounter .decrementAndGet ();
119
+ throw ex ;
110
120
}
111
-
112
- // Unlike Jetty, all Tomcat threads are daemon threads. We create a
113
- // blocking non-daemon to stop immediate shutdown
114
- startDaemonAwaitThread ();
115
121
}
116
122
catch (Exception ex ) {
117
123
throw new EmbeddedServletContainerException (
@@ -279,6 +285,7 @@ Map<Service, Connector[]> getServiceConnectors() {
279
285
@ Override
280
286
public void stop () throws EmbeddedServletContainerException {
281
287
synchronized (this .monitor ) {
288
+ boolean wasStarted = this .started ;
282
289
try {
283
290
this .started = false ;
284
291
try {
@@ -294,7 +301,9 @@ public void stop() throws EmbeddedServletContainerException {
294
301
"Unable to stop embedded Tomcat" , ex );
295
302
}
296
303
finally {
297
- containerCounter .decrementAndGet ();
304
+ if (wasStarted ) {
305
+ containerCounter .decrementAndGet ();
306
+ }
298
307
}
299
308
}
300
309
}
0 commit comments