Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package brooklyn.util.task;
import static brooklyn.util.JavaGroovyEquivalents.asString;
import static brooklyn.util.JavaGroovyEquivalents.elvisString;
import static brooklyn.util.JavaGroovyEquivalents.join;
import groovy.lang.Closure;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.LockInfo;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brooklyn.management.ExecutionManager;
import brooklyn.management.Task;
import brooklyn.util.GroovyJavaMethods;
import brooklyn.util.exceptions.Exceptions;
import com.google.common.base.Throwables;
/**
* The basic concrete implementation of a {@link Task} to be executed.
*
* A {@link Task} is a wrapper for an executable unit, such as a {@link Closure} or a {@link Runnable} or
* {@link Callable} and will run in its own {@link Thread}.
*
* The task can be given an optional displayName and description in its constructor (as named
* arguments in the first {@link Map} parameter). It is guaranteed to have {@link Object#notify()} called
* once whenever the task starts running and once again when the task is about to complete. Due to
* the way executors work it is ugly to guarantee notification after completion, so instead we
* notify just before then expect the user to call {@link #get()} - which will throw errors if the underlying job
* did so - or {@link #blockUntilEnded()} which will not throw errors.
*
* @see BasicTaskStub
*/
public class BasicTask extends BasicTaskStub implements Task {
protected static final Logger log = LoggerFactory.getLogger(BasicTask.class);
protected Callable job;
public final String displayName;
public final String description;
protected final Set tags = new LinkedHashSet();
protected String blockingDetails = null;
Object extraStatusText = null;
/**
* Constructor needed to prevent confusion in groovy stubs when looking for default constructor,
*
* The generics on {@link Closure} break it if that is first constructor.
*/
protected BasicTask() { this(Collections.emptyMap()); }
protected BasicTask(Map flags) { this(flags, (Callable) null); }
public BasicTask(Callable job) { this(Collections.emptyMap(), job); }
public BasicTask(Map flags, Callable job) {
this.job = job;
if (flags.containsKey("tag")) tags.add(flags.remove("tag"));
Object ftags = flags.remove("tags");
if (ftags!=null) {
if (ftags instanceof Collection) tags.addAll((Collection)ftags);
else {
log.info("discouraged use of non-collection argument for 'tags' ("+ftags+") in "+this, new Throwable("trace of discouraged use of non-colleciton tags argument"));
tags.add(ftags);
}
}
description = elvisString(flags.remove("description"), "");
String d = asString(flags.remove("displayName"));
if (d==null) d = join(tags, "-");
displayName = d;
}
public BasicTask(Runnable job) { this(GroovyJavaMethods.callableFromRunnable(job)); }
public BasicTask(Map flags, Runnable job) { this(flags, GroovyJavaMethods.callableFromRunnable(job)); }
public BasicTask(Closure job) { this(GroovyJavaMethods.callableFromClosure(job)); }
public BasicTask(Map flags, Closure job) { this(flags, GroovyJavaMethods.callableFromClosure(job)); }
@Override
public String toString() {
return "Task["+(displayName!=null && displayName.length()>0?displayName+
(tags!=null && !tags.isEmpty()?"":";")+" ":"")+
(tags!=null && !tags.isEmpty()?tags+"; ":"")+getId()+"]";
}
// housekeeping --------------------
/*
* These flags are set by BasicExecutionManager.submit.
*
* Order is guaranteed to be as shown below, in order of #. Within each # line it is currently in the order specified by commas but this is not guaranteed.
* (The spaces between the # section indicate longer delays / logical separation ... it should be clear!)
*
* # submitter, submit time set, tags and other submit-time fields set, task tag-linked preprocessors onSubmit invoked
*
* # thread set, ThreadLocal getCurrentTask set
* # start time set, isBegun is true
* # task tag-linked preprocessors onStart invoked
* # task end callback run, if supplied
*
* # task runs
*
* # task end callback run, if supplied
* # task tag-linked preprocessors onEnd invoked (in reverse order of tags)
* # end time set
* # thread cleared, ThreadLocal getCurrentTask set
* # Task.notifyAll()
* # Task.get() (result.get()) available, Task.isDone is true
*
* Few _consumers_ should care, but internally we rely on this so that, for example, status is displayed correctly.
* Tests should catch most things, but be careful if you change any of the above semantics.
*/
protected long submitTimeUtc = -1;
protected long startTimeUtc = -1;
protected long endTimeUtc = -1;
protected Task> submittedByTask;
protected volatile Thread thread = null;
private volatile boolean cancelled = false;
protected volatile Future result = null;
/** discouraged, but used in tests. not always set (e.g. if it is a scheduled task)
* @deprecated in 0.4.0; use current execution context, as per CompoundTask.submitXxx */
@Deprecated
protected ExecutionManager em;
@SuppressWarnings("deprecation")
void initExecutionManager(ExecutionManager em) {
this.em = em;
}
synchronized void initResult(Future result) {
if (this.result != null)
throw new IllegalStateException("task "+this+" is being given a result twice");
this.result = result;
notifyAll();
}
// metadata accessors ------------
public Set