Bug 54945 - Add Shutdown Hook to enable trapping kill or CTRL+C signals

Bugzilla Id: 54945

git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1480762 13f79535-47bb-0310-9956-ffa450edef68

Former-commit-id: fa01ece63c
This commit is contained in:
Philippe Mouawad 2013-05-09 20:32:55 +00:00
parent ca7b5c45c5
commit 49e6b4857a
2 changed files with 44 additions and 0 deletions

View File

@ -141,6 +141,32 @@ public class ResultCollector extends AbstractListenerElement implements SampleLi
/** the summarizer to which this result collector will forward the samples */
private volatile Summariser summariser;
/**
* Shutdown Hook that ensures PrintWriter is flushed is CTRL+C or kill is called during a test
*/
private Thread shutdownHook;
private static final class JMeterShutdownHook implements Runnable {
private static final Logger log = LoggingManager.getLoggerFor(JMeterShutdownHook.class.getName());
private ResultCollector collector;
public JMeterShutdownHook(ResultCollector collector) {
this.collector = collector;
}
@Override
public void run() {
log.warn("Shutdown hook started");
if(collector.inTest) {
synchronized (LOCK) {
collector.flushFileOutput();
}
log.warn("Shutdown hook flushed file");
}
log.warn("Shutdown hook ended");
}
}
/**
* No-arg constructor.
*/
@ -153,6 +179,7 @@ public class ResultCollector extends AbstractListenerElement implements SampleLi
setSuccessOnlyLogging(false);
setProperty(new ObjectProperty(SAVE_CONFIG, new SampleSaveConfiguration()));
summariser = summer;
this.shutdownHook = new Thread(new JMeterShutdownHook(this));
}
// Ensure that the sample save config is not shared between copied nodes
@ -248,6 +275,7 @@ public class ResultCollector extends AbstractListenerElement implements SampleLi
instanceCount--;
if (instanceCount <= 0) {
finalizeFileOutput();
Runtime.getRuntime().removeShutdownHook(shutdownHook);
inTest = false;
}
}
@ -259,6 +287,7 @@ public class ResultCollector extends AbstractListenerElement implements SampleLi
@Override
public void testStarted(String host) {
Runtime.getRuntime().addShutdownHook(shutdownHook);
synchronized(LOCK){
instanceCount++;
try {
@ -567,6 +596,20 @@ public class ResultCollector extends AbstractListenerElement implements SampleLi
}
}
/**
* Flush PrintWriter, called by Shutdown Hook to ensure no data is lost
*/
private void flushFileOutput() {
for(Map.Entry<String,ResultCollector.FileEntry> me : files.entrySet()){
log.debug("Flushing: "+me.getKey());
FileEntry fe = me.getValue();
fe.pw.flush();
if (fe.pw.checkError()){
log.warn("Problem detected during use of "+me.getKey());
}
}
}
private void finalizeFileOutput() {
for(Map.Entry<String,ResultCollector.FileEntry> me : files.entrySet()){
log.debug("Closing: "+me.getKey());

View File

@ -202,6 +202,7 @@ Webservice (SOAP) Request has been removed by default from GUI as Element is dep
<li><bugzilla>54844</bugzilla> - Set the application icon on Mac Os</li>
<li><bugzilla>54834</bugzilla> - Improve Drag &amp; Drop in the jmeter tree</li>
<li><bugzilla>54864</bugzilla> - Enable multi selection drag &amp; drop in the tree without having to start dragging before releasing Shift or Control </li>
<li><bugzilla>54945</bugzilla> - Add Shutdown Hook to enable trapping kill or CTRL+C signals</li>
</ul>
<h2>Non-functional changes</h2>