/** * Submits a {@link TaskFactory} to construct its task at the entity (in a precursor task) and then to submit it. * <p> * Important if task construction relies on an entity being in scope (in tags, via {@link BrooklynTaskTags}) */ public static <T extends TaskAdaptable<?>> T submit(final Entity entity, final TaskFactory<T> taskFactory) { // TODO it is messy to have to do this, but not sure there is a cleaner way :( final Semaphore s = new Semaphore(0); final AtomicReference<T> result = new AtomicReference<T>(); final ExecutionContext executionContext = ((EntityInternal)entity).getExecutionContext(); executionContext.execute(new Runnable() { // TODO could give this task a name, like "create task from factory" @Override public void run() { T t = taskFactory.newTask(); result.set(t); s.release(); } }); try { s.acquire(); } catch (InterruptedException e) { throw Exceptions.propagate(e); } executionContext.submit(result.get().asTask()); return result.get(); }
@Override public void rebind() { super.rebind(); if (taskScan == null) { // The feed calls scanTasks, which modifies other entities.That is dangerous during // rebind, because the rebind-thread may concurrently (or subsequently) be initialising // those other entities. Similar code (e.g. DynamicMultiGroup) has caused rebind of // of those other entities to subsequently fail. // // Normal best-practice would be to use {@code feeds().addFeed(feed)} so that the entity // automatically persists its feed, but this is no ordinary feed! Therefore safer (for // now) to do it this way. getExecutionContext().execute(new Runnable() { @Override public void run() { LOG.debug("Deferring scanner for {} until management context initialisation complete", MesosFrameworkImpl.this); while (!isRebindComplete()) { Time.sleep(100); // avoid thrashing } LOG.debug("Connecting scanner for {}", MesosFrameworkImpl.this); connectSensors(); } private boolean isRebindComplete() { return !getManagementContext().getRebindManager().isAwaitingInitialRebind(); }}); } }
getExecutionContext().execute(new Runnable() { @Override public void run() { LOG.debug("Deferring scanner for {} until management context initialisation complete", DynamicMultiGroupImpl.this);