Skip to content

Commit 208b150

Browse files
authored
Issue #8088 Add STOP.EXIT System property to configure ShutdownMonitor.exitVm (#8089)
* Issue #8088 Add STOP.EXIT System property to configure ShutdownMonitor.exitVM * Ensure missing STOP.EXIT doesn't override default exitVm=true * Disable another test
1 parent 4b4abfd commit 208b150

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.function.Predicate;
3636

3737
import org.eclipse.jetty.util.IO;
38+
import org.eclipse.jetty.util.StringUtil;
3839
import org.eclipse.jetty.util.component.Destroyable;
3940
import org.eclipse.jetty.util.component.LifeCycle;
4041
import org.eclipse.jetty.util.thread.ShutdownThread;
@@ -90,7 +91,7 @@ public static boolean isRegistered(LifeCycle lifeCycle)
9091
private final String host;
9192
private int port;
9293
private String key;
93-
private boolean exitVm;
94+
private boolean exitVm = true;
9495
private boolean alive;
9596

9697
/**
@@ -107,7 +108,8 @@ private ShutdownMonitor()
107108
this.host = System.getProperty("STOP.HOST", "127.0.0.1");
108109
this.port = Integer.getInteger("STOP.PORT", -1);
109110
this.key = System.getProperty("STOP.KEY", null);
110-
this.exitVm = true;
111+
//only change the default exitVm setting if STOP.EXIT is explicitly set
112+
this.exitVm = Boolean.valueOf(System.getProperty("STOP.EXIT", "true"));
111113
}
112114

113115
private void addLifeCycles(LifeCycle... lifeCycles)
@@ -308,6 +310,8 @@ private ServerSocket listen()
308310
// establish the port and key that are in use
309311
debug("STOP.PORT=%d", port);
310312
debug("STOP.KEY=%s", key);
313+
//also show if we're exiting the jvm or not
314+
debug("STOP.EXIT=%b", exitVm);
311315
}
312316
}
313317

jetty-server/src/test/java/org/eclipse/jetty/server/ShutdownMonitorTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.jupiter.api.Test;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
3536
import static org.junit.jupiter.api.Assertions.assertNull;
3637
import static org.junit.jupiter.api.Assertions.assertTrue;
3738

@@ -120,6 +121,80 @@ private void testStartStop(boolean reusePort) throws Exception
120121
assertTrue(!monitor.isAlive());
121122
}
122123

124+
@Test
125+
public void testNoExitSystemProperty() throws Exception
126+
{
127+
System.setProperty("STOP.EXIT", "false");
128+
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
129+
monitor.setPort(0);
130+
assertFalse(monitor.isExitVm());
131+
monitor.start();
132+
133+
try (CloseableServer server = new CloseableServer())
134+
{
135+
server.setStopAtShutdown(true);
136+
server.start();
137+
138+
//shouldn't be registered for shutdown on jvm
139+
assertTrue(ShutdownThread.isRegistered(server));
140+
assertTrue(ShutdownMonitor.isRegistered(server));
141+
142+
String key = monitor.getKey();
143+
int port = monitor.getPort();
144+
145+
stop("stop", port, key, true);
146+
monitor.await();
147+
148+
assertTrue(!monitor.isAlive());
149+
assertTrue(server.stopped);
150+
assertTrue(!server.destroyed);
151+
assertTrue(!ShutdownThread.isRegistered(server));
152+
assertTrue(!ShutdownMonitor.isRegistered(server));
153+
}
154+
}
155+
156+
/*
157+
* Disable these config tests because ShutdownMonitor is a
158+
* static singleton that cannot be unset, and thus would
159+
* need each of these methods executed it its own jvm -
160+
* current surefire settings only fork for a single test
161+
* class.
162+
*
163+
* Undisable to test individually as needed.
164+
*/
165+
@Disabled
166+
@Test
167+
public void testExitVmDefault() throws Exception
168+
{
169+
//Test that the default is to exit
170+
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
171+
monitor.setPort(0);
172+
assertTrue(monitor.isExitVm());
173+
}
174+
175+
@Disabled
176+
@Test
177+
public void testExitVmTrue() throws Exception
178+
{
179+
//Test setting exit true
180+
System.setProperty("STOP.EXIT", "true");
181+
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
182+
monitor.setPort(0);
183+
assertTrue(monitor.isExitVm());
184+
}
185+
186+
@Disabled
187+
@Test
188+
public void testExitVmFalse() throws Exception
189+
{
190+
//Test setting exit false
191+
System.setProperty("STOP.EXIT", "false");
192+
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
193+
monitor.setPort(0);
194+
assertFalse(monitor.isExitVm());
195+
}
196+
197+
@Disabled
123198
@Test
124199
public void testForceStopCommand() throws Exception
125200
{

0 commit comments

Comments
 (0)