Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
229 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
hawtio-maven-plugin/src/main/java/io/hawt/maven/CountdownLatchMojoLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.hawt.maven; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
|
||
public class CountDownLatchMojoLifecycle extends DefaultMojoLifecycle { | ||
|
||
private final CountDownLatch latch; | ||
|
||
public CountDownLatchMojoLifecycle(Log log, CountDownLatch latch) { | ||
super(log); | ||
this.latch = latch; | ||
} | ||
|
||
@Override | ||
public void join(ThreadGroup threadGroup) { | ||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
// ignore | ||
} | ||
} | ||
|
||
} |
132 changes: 132 additions & 0 deletions
132
hawtio-maven-plugin/src/main/java/io/hawt/maven/DefaultMojoLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.hawt.maven; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
|
||
public class DefaultMojoLifecycle implements MojoLifecycle { | ||
|
||
private long daemonThreadJoinTimeout = 15000L; | ||
private final Log log; | ||
|
||
public DefaultMojoLifecycle(Log log) { | ||
this.log = log; | ||
} | ||
|
||
@Override | ||
public void join(ThreadGroup threadGroup) { | ||
boolean foundNonDaemon; | ||
do { | ||
foundNonDaemon = false; | ||
Collection<Thread> threads = getActiveThreads(threadGroup); | ||
for (Thread thread : threads) { | ||
if (thread.isDaemon()) { | ||
continue; | ||
} | ||
foundNonDaemon = true; // try again; maybe more threads were | ||
// created while we were busy | ||
joinThread(thread, 0); | ||
} | ||
} while (foundNonDaemon); | ||
} | ||
|
||
@Override | ||
public void terminateThreads(ThreadGroup threadGroup) { | ||
long startTime = System.currentTimeMillis(); | ||
Set<Thread> uncooperativeThreads = new HashSet<Thread>(); // these were not responsive | ||
// to interruption | ||
for (Collection<Thread> threads = getActiveThreads(threadGroup); !threads.isEmpty(); threads = getActiveThreads(threadGroup), threads | ||
.removeAll(uncooperativeThreads)) { | ||
// Interrupt all threads we know about as of this instant (harmless | ||
// if spuriously went dead (! isAlive()) | ||
// or if something else interrupted it ( isInterrupted() ). | ||
for (Thread thread : threads) { | ||
log.debug("interrupting thread " + thread); | ||
thread.interrupt(); | ||
} | ||
// Now join with a timeout and call stop() (assuming flags are set | ||
// right) | ||
for (Thread thread : threads) { | ||
if (!thread.isAlive()) { | ||
continue; // and, presumably it won't show up in | ||
// getActiveThreads() next iteration | ||
} | ||
if (daemonThreadJoinTimeout <= 0) { | ||
joinThread(thread, 0); // waits until not alive; no timeout | ||
continue; | ||
} | ||
long timeout = daemonThreadJoinTimeout - (System.currentTimeMillis() - startTime); | ||
if (timeout > 0) { | ||
joinThread(thread, timeout); | ||
} | ||
if (!thread.isAlive()) { | ||
continue; | ||
} | ||
uncooperativeThreads.add(thread); // ensure we don't process | ||
log.warn("thread " + thread + " will linger despite being asked to die via interruption"); | ||
} | ||
} | ||
if (!uncooperativeThreads.isEmpty()) { | ||
log.warn("NOTE: " | ||
+ uncooperativeThreads.size() | ||
+ " thread(s) did not finish despite being asked to " | ||
+ " via interruption. This is not a problem with exec:java, it is a problem with the running code." | ||
+ " Although not serious, it should be remedied."); | ||
} else { | ||
int activeCount = threadGroup.activeCount(); | ||
if (activeCount != 0) { | ||
Thread[] threadsArray = new Thread[1]; | ||
threadGroup.enumerate(threadsArray); | ||
log.debug("strange; " + activeCount + " thread(s) still active in the group " | ||
+ threadGroup + " such as " + threadsArray[0]); | ||
} | ||
} | ||
} | ||
|
||
private void joinThread(Thread thread, long timeoutMsecs) { | ||
try { | ||
log.info("Joining on thread " + thread); | ||
thread.join(timeoutMsecs); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); // good practice if don't throw | ||
log.warn("interrupted while joining against thread " + thread, e); // not | ||
// expected! | ||
} | ||
// generally abnormal | ||
if (thread.isAlive()) { | ||
log.warn("thread " + thread + " was interrupted but is still alive after waiting at least " | ||
+ timeoutMsecs + "msecs"); | ||
} | ||
} | ||
|
||
private Collection<Thread> getActiveThreads(ThreadGroup threadGroup) { | ||
Thread[] threads = new Thread[threadGroup.activeCount()]; | ||
int numThreads = threadGroup.enumerate(threads); | ||
Collection<Thread> result = new ArrayList<Thread>(numThreads); | ||
for (int i = 0; i < threads.length && threads[i] != null; i++) { | ||
result.add(threads[i]); | ||
} | ||
// note: result should be modifiable | ||
return result; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
hawtio-maven-plugin/src/main/java/io/hawt/maven/MojoLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.hawt.maven; | ||
|
||
public interface MojoLifecycle { | ||
|
||
void join(ThreadGroup threadGroup); | ||
|
||
void terminateThreads(ThreadGroup threadGroup); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.