提交 87fb68a0 编写于 作者: C Christoph Kutzinski

make futures list private and provide accessor methods

上级 731c9777
......@@ -24,7 +24,7 @@
package hudson.maven;
import hudson.model.BuildListener;
import jenkins.model.Jenkins;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.remoting.Channel;
import hudson.remoting.DelegatingCallable;
......@@ -35,6 +35,10 @@ import java.io.Serializable;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import jenkins.model.Jenkins;
/**
* @author Olivier Lamy
......@@ -61,7 +65,7 @@ public abstract class AbstractMavenBuilder implements DelegatingCallable<Result,
* Record all asynchronous executions as they are scheduled,
* to make sure they are all completed before we finish.
*/
protected transient /*final*/ List<Future<?>> futures;
private transient /*final*/ List<Future<?>> futures;
protected AbstractMavenBuilder(BuildListener listener, List<String> goals, Map<String, String> systemProps) {
this.listener = listener;
......@@ -109,10 +113,53 @@ public abstract class AbstractMavenBuilder implements DelegatingCallable<Result,
return Jenkins.getInstance().getPluginManager().uberClassLoader;
}
/**
* Initialize the collection of the asynchronous executions.
* The method must be called in the Maven jail process i.e. inside the call method!
*/
protected void initializeAsynchronousExecutions() {
futures = new CopyOnWriteArrayList<Future<?>>();
}
/**
* Records a new asynchronous exection.
*/
protected void recordAsynchronousExecution(Future<?> future) {
futures.add(future);
}
/**
* Waits until all asynchronous executions are finished.
*
* @return null in success case; returns an ABORT result if we were interrupted while waiting
*/
protected Result waitForAsynchronousExecutions() {
try {
boolean messageReported = false;
for (Future<?> f : futures) {
try {
if(!f.isDone() && !messageReported) {
messageReported = true;
listener.getLogger().println(Messages.MavenBuilder_Waiting());
}
f.get();
} catch (InterruptedException e) {
// attempt to cancel all asynchronous tasks
for (Future<?> g : futures)
g.cancel(true);
listener.getLogger().println(Messages.MavenBuilder_Aborted());
return Executor.currentExecutor().abortResult();
} catch (ExecutionException e) {
e.printStackTrace(listener.error(Messages.MavenBuilder_AsyncFailed()));
}
}
return null;
} finally {
futures.clear();
}
}
protected class FilterImpl extends MavenBuildProxy2.Filter<MavenBuildProxy2> implements Serializable {
private MavenBuildInformation mavenBuildInformation;
......
......@@ -27,11 +27,9 @@ import hudson.Launcher;
import hudson.maven.MavenBuild.ProxyImpl2;
import hudson.maven.util.ExecutionEventLogger;
import hudson.model.BuildListener;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.remoting.Channel;
import hudson.remoting.DelegatingCallable;
import hudson.remoting.Future;
import hudson.util.IOException2;
import java.io.IOException;
......@@ -48,7 +46,6 @@ import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import org.apache.maven.cli.PrintStreamLogger;
......@@ -101,7 +98,7 @@ public class Maven3Builder extends AbstractMavenBuilder implements DelegatingCal
MavenExecutionListener mavenExecutionListener = new MavenExecutionListener( this );
try {
futures = new CopyOnWriteArrayList<Future<?>>( );
initializeAsynchronousExecutions();
Maven3Launcher.setMavenExecutionListener( mavenExecutionListener );
......@@ -115,27 +112,14 @@ public class Maven3Builder extends AbstractMavenBuilder implements DelegatingCal
int r = Maven3Main.launch( goals.toArray(new String[goals.size()]));
// now check the completion status of async ops
boolean messageReported = false;
long startTime = System.nanoTime();
for (Future<?> f : futures) {
try {
if(!f.isDone() && !messageReported) {
messageReported = true;
listener.getLogger().println(Messages.MavenBuilder_Waiting());
}
f.get();
} catch (InterruptedException e) {
// attempt to cancel all asynchronous tasks
for (Future<?> g : futures)
g.cancel(true);
listener.getLogger().println(Messages.MavenBuilder_Aborted());
return Executor.currentExecutor().abortResult();
} catch (ExecutionException e) {
e.printStackTrace(listener.error(Messages.MavenBuilder_AsyncFailed()));
}
Result waitForAsyncExecutionsResult = waitForAsynchronousExecutions();
if (waitForAsyncExecutionsResult != null) {
return waitForAsyncExecutionsResult;
}
mavenExecutionListener.overheadTime += System.nanoTime()-startTime;
futures.clear();
if(profile) {
NumberFormat n = NumberFormat.getInstance();
......
......@@ -36,7 +36,6 @@ import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Environment;
import hudson.model.Executor;
import jenkins.model.Jenkins;
import hudson.model.Node;
import hudson.model.Result;
import hudson.model.Run;
......@@ -319,7 +318,7 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
@Override
public void executeAsync(final BuildCallable<?,?> program) throws IOException {
futures.add(Channel.current().callAsync(new AsyncInvoker(core,program)));
recordAsynchronousExecution(Channel.current().callAsync(new AsyncInvoker(core,program)));
}
public MavenBuildInformation getMavenBuildInformation()
......
......@@ -29,12 +29,10 @@ import hudson.maven.agent.Main;
import hudson.maven.agent.PluginManagerListener;
import hudson.maven.reporters.SurefireArchiver;
import hudson.model.BuildListener;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.DelegatingCallable;
import hudson.remoting.Future;
import hudson.util.IOException2;
import java.io.IOException;
......@@ -42,11 +40,8 @@ import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.apache.maven.BuildFailureException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
......@@ -137,7 +132,7 @@ public abstract class MavenBuilder extends AbstractMavenBuilder implements Deleg
try {
futures = new ArrayList<Future<?>>();
initializeAsynchronousExecutions();
Adapter a = new Adapter(this);
callSetListenerWithReflectOnInterceptors( a, mavenJailProcessClassLoader );
......@@ -154,27 +149,14 @@ public abstract class MavenBuilder extends AbstractMavenBuilder implements Deleg
int r = Main.launch(goals.toArray(new String[goals.size()]));
// now check the completion status of async ops
boolean messageReported = false;
long startTime = System.nanoTime();
for (Future<?> f : futures) {
try {
if(!f.isDone() && !messageReported) {
messageReported = true;
listener.getLogger().println(Messages.MavenBuilder_Waiting());
}
f.get();
} catch (InterruptedException e) {
// attempt to cancel all asynchronous tasks
for (Future<?> g : futures)
g.cancel(true);
listener.getLogger().println(Messages.MavenBuilder_Aborted());
return Executor.currentExecutor().abortResult();
} catch (ExecutionException e) {
e.printStackTrace(listener.error(Messages.MavenBuilder_AsyncFailed()));
}
Result waitForAsyncExecutionsResult = waitForAsynchronousExecutions();
if (waitForAsyncExecutionsResult != null) {
return waitForAsyncExecutionsResult;
}
a.overheadTime += System.nanoTime()-startTime;
futures.clear();
if(profile) {
NumberFormat n = NumberFormat.getInstance();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册