Deleted EntityInternal.getSubscriptionContext - Added Entity.subscriptions().unsubscribe(handle) - Added EntityInternal.subscriptions().unsubscribeAll()
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/2b29795b Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/2b29795b Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/2b29795b Branch: refs/heads/master Commit: 2b29795b6ef9c37cb14242ccc28249b16a708c68 Parents: e48bdaa Author: Aled Sage <aled.s...@gmail.com> Authored: Mon Sep 21 09:22:24 2015 +0100 Committer: Aled Sage <aled.s...@gmail.com> Committed: Wed Sep 23 10:32:56 2015 +0100 ---------------------------------------------------------------------- .../org/apache/brooklyn/api/entity/Entity.java | 9 +++++++ .../brooklyn/core/entity/AbstractEntity.java | 28 ++++++++++++-------- .../brooklyn/core/entity/EntityInternal.java | 14 +--------- .../core/mgmt/internal/SubscriptionTracker.java | 16 +++++++++++ .../core/sensor/DependentConfiguration.java | 8 +++--- .../brooklyn/core/entity/EntityTypeTest.java | 4 +-- .../core/entity/hello/LocalEntitiesTest.java | 8 +++--- .../core/test/entity/TestApplicationImpl.java | 2 +- .../entity/proxy/nginx/UrlMappingImpl.java | 4 +-- .../AbstractWebAppFixtureIntegrationTest.java | 5 ++-- 10 files changed, 58 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java index 4df913b..795218c 100644 --- a/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java +++ b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java @@ -391,6 +391,15 @@ public interface Entity extends BrooklynObject { */ @Beta boolean unsubscribe(Entity producer, SubscriptionHandle handle); + + /** + * Unsubscribes the given handle. + * + * It is (currently) more efficient to also pass in the producer - + * see {@link BasicSubscriptionSupport#unsubscribe(Entity, SubscriptionHandle)} + */ + @Beta + boolean unsubscribe(SubscriptionHandle handle); } @Beta http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java index 13079d3..48a07a4 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java @@ -1069,7 +1069,7 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E if (getManagementSupport().isNoLongerManaged()) throw new IllegalStateException("Entity "+AbstractEntity.this+" is no longer managed, when trying to publish "+sensor+" "+val); - SubscriptionContext subsContext = getSubscriptionContext(); + SubscriptionContext subsContext = subscriptions().getSubscriptionContext(); if (subsContext != null) subsContext.publish(sensor.newEvent(getProxyIfAvailable(), val)); } } @@ -1390,8 +1390,23 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E return getSubscriptionTracker().unsubscribe(producer, handle); } + /** + * Unsubscribes the given handle. + * + * It is (currently) more efficient to also pass in the producer - + * see {@link BasicSubscriptionSupport#unsubscribe(Entity, SubscriptionHandle)} + */ @Override - public SubscriptionContext getSubscriptionContext() { + public boolean unsubscribe(SubscriptionHandle handle) { + return getSubscriptionTracker().unsubscribe(handle); + } + + @Override + public void unsubscribeAll() { + getSubscriptionTracker().unsubscribeAll(); + } + + protected SubscriptionContext getSubscriptionContext() { synchronized (AbstractEntity.this) { return getManagementSupport().getSubscriptionContext(); } @@ -1460,15 +1475,6 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E } /** - * @deprecated since 0.9.0; see {@code subscriptions().getSubscriptionContext()} - */ - @Override - @Deprecated - public synchronized SubscriptionContext getSubscriptionContext() { - return subscriptions().getSubscriptionContext(); - } - - /** * @deprecated since 0.9.0; for internal use only */ @Deprecated http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java index 04fa88b..4fa9c67 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java @@ -24,25 +24,19 @@ import java.util.Map; import org.apache.brooklyn.api.effector.Effector; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.Entity.SensorSupport; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.mgmt.ExecutionContext; import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.api.mgmt.SubscriptionContext; import org.apache.brooklyn.api.mgmt.rebind.RebindSupport; import org.apache.brooklyn.api.mgmt.rebind.Rebindable; import org.apache.brooklyn.api.mgmt.rebind.mementos.EntityMemento; -import org.apache.brooklyn.api.objs.Configurable; import org.apache.brooklyn.api.sensor.AttributeSensor; import org.apache.brooklyn.api.sensor.Feed; import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.config.ConfigKey.HasConfigKey; import org.apache.brooklyn.core.entity.internal.EntityConfigMap; import org.apache.brooklyn.core.mgmt.internal.EntityManagementSupport; import org.apache.brooklyn.core.objs.BrooklynObjectInternal; -import org.apache.brooklyn.core.objs.BrooklynObjectInternal.ConfigurationSupportInternal; import org.apache.brooklyn.util.core.config.ConfigBag; -import org.apache.brooklyn.util.guava.Maybe; import com.google.common.annotations.Beta; @@ -152,12 +146,6 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb */ ExecutionContext getExecutionContext(); - /** - * @deprecated since 0.9.0; see {@link SubscriptionSupportInternal#getSubscriptionContext()}, e.g. with {@code subscriptions().getSubscriptionContext()} - */ - @Deprecated - SubscriptionContext getSubscriptionContext(); - /** returns the dynamic type corresponding to the type of this entity instance */ @Beta EntityDynamicType getMutableEntityType(); @@ -243,7 +231,7 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb @Beta public interface SubscriptionSupportInternal extends Entity.SubscriptionSupport { - SubscriptionContext getSubscriptionContext(); + public void unsubscribeAll(); } @Beta http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/SubscriptionTracker.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/SubscriptionTracker.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/SubscriptionTracker.java index 3d5793c..bafa150 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/SubscriptionTracker.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/SubscriptionTracker.java @@ -27,6 +27,7 @@ import org.apache.brooklyn.api.mgmt.SubscriptionContext; import org.apache.brooklyn.api.mgmt.SubscriptionHandle; import org.apache.brooklyn.api.sensor.Sensor; import org.apache.brooklyn.api.sensor.SensorEventListener; +import org.apache.brooklyn.core.entity.AbstractEntity.BasicSubscriptionSupport; import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableList; @@ -121,6 +122,21 @@ public class SubscriptionTracker { return context.unsubscribe(handle); } + /** + * Unsubscribes the given handle. + * + * It is (currently) more efficient to also pass in the producer - + * see {@link BasicSubscriptionSupport#unsubscribe(Entity, SubscriptionHandle)} + * + * @see SubscriptionContext#unsubscribe(SubscriptionHandle) + */ + public boolean unsubscribe(SubscriptionHandle handle) { + synchronized (subscriptions) { + subscriptions.values().remove(handle); + } + return context.unsubscribe(handle); + } + /** * @return an ordered list of all subscription handles */ http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java index 01e3426..ddca05c 100644 --- a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java +++ b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java @@ -274,13 +274,13 @@ public class DependentConfiguration { List<SubscriptionHandle> abortSubscriptions = Lists.newArrayList(); try { - subscription = ((EntityInternal)entity).getSubscriptionContext().subscribe(source, sensor, new SensorEventListener<T>() { + subscription = entity.subscriptions().subscribe(source, sensor, new SensorEventListener<T>() { @Override public void onEvent(SensorEvent<T> event) { synchronized (publishedValues) { publishedValues.add(event.getValue()); } semaphore.release(); }}); for (final AttributeAndSensorCondition abortCondition : abortSensorConditions) { - abortSubscriptions.add(((EntityInternal)entity).getSubscriptionContext().subscribe(abortCondition.source, abortCondition.sensor, new SensorEventListener<Object>() { + abortSubscriptions.add(entity.subscriptions().subscribe(abortCondition.source, abortCondition.sensor, new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { if (abortCondition.predicate.apply(event.getValue())) { abortionExceptions.add(new Exception("Abort due to "+abortCondition.source+" -> "+abortCondition.sensor)); @@ -354,10 +354,10 @@ public class DependentConfiguration { throw Exceptions.propagate(e); } finally { if (subscription != null) { - ((EntityInternal)entity).getSubscriptionContext().unsubscribe(subscription); + entity.subscriptions().unsubscribe(subscription); } for (SubscriptionHandle handle : abortSubscriptions) { - ((EntityInternal)entity).getSubscriptionContext().unsubscribe(handle); + entity.subscriptions().unsubscribe(handle); } } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java index 5851c35..130327f 100644 --- a/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java @@ -87,8 +87,8 @@ public class EntityTypeTest extends BrooklynAppUnitTestSupport { super.setUp(); entity = (EntityInternal) app.createAndManageChild(EntitySpec.create(Entity.class, EmptyEntityForTesting.class)); listener = new RecordingSensorEventListener<>(); - app.getSubscriptionContext().subscribe(entity, SENSOR_ADDED, listener); - app.getSubscriptionContext().subscribe(entity, SENSOR_REMOVED, listener); + app.subscriptions().subscribe(entity, SENSOR_ADDED, listener); + app.subscriptions().subscribe(entity, SENSOR_REMOVED, listener); } @Test http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/test/java/org/apache/brooklyn/core/entity/hello/LocalEntitiesTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/hello/LocalEntitiesTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/hello/LocalEntitiesTest.java index 28ee8c1..6d64d2b 100644 --- a/core/src/test/java/org/apache/brooklyn/core/entity/hello/LocalEntitiesTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/entity/hello/LocalEntitiesTest.java @@ -94,7 +94,7 @@ public class LocalEntitiesTest extends BrooklynAppUnitTestSupport { final AtomicReference<SensorEvent<?>> evt = new AtomicReference<SensorEvent<?>>(); final CountDownLatch latch = new CountDownLatch(1); - app.getSubscriptionContext().subscribe(h, HelloEntity.AGE, new SensorEventListener<Integer>() { + app.subscriptions().subscribe(h, HelloEntity.AGE, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { evt.set(event); latch.countDown(); @@ -121,7 +121,7 @@ public class LocalEntitiesTest extends BrooklynAppUnitTestSupport { app.start(ImmutableList.of(loc)); final AtomicReference<SensorEvent<?>> evt = new AtomicReference<SensorEvent<?>>(); - app.getSubscriptionContext().subscribe(h, HelloEntity.ITS_MY_BIRTHDAY, new SensorEventListener<Object>() { + app.subscriptions().subscribe(h, HelloEntity.ITS_MY_BIRTHDAY, new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { evt.set(event); synchronized (evt) { @@ -149,7 +149,7 @@ public class LocalEntitiesTest extends BrooklynAppUnitTestSupport { final List<Integer> data = Lists.newArrayList(); final CountDownLatch latch = new CountDownLatch(5); - app.getSubscriptionContext().subscribe(h, HelloEntity.AGE, new SensorEventListener<Integer>() { + app.subscriptions().subscribe(h, HelloEntity.AGE, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { data.add(event.getValue()); Time.sleep((int)(20*Math.random())); @@ -163,7 +163,7 @@ public class LocalEntitiesTest extends BrooklynAppUnitTestSupport { } assertTrue(latch.await(5000, TimeUnit.MILLISECONDS)); - app.getSubscriptionContext().unsubscribeAll(); + app.subscriptions().unsubscribeAll(); h.setAge(6); long totalTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/core/src/test/java/org/apache/brooklyn/core/test/entity/TestApplicationImpl.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/test/entity/TestApplicationImpl.java b/core/src/test/java/org/apache/brooklyn/core/test/entity/TestApplicationImpl.java index 47faafd..7ce08d3 100644 --- a/core/src/test/java/org/apache/brooklyn/core/test/entity/TestApplicationImpl.java +++ b/core/src/test/java/org/apache/brooklyn/core/test/entity/TestApplicationImpl.java @@ -63,7 +63,7 @@ public class TestApplicationImpl extends AbstractApplication implements TestAppl @Override public <T> SubscriptionHandle subscribeToMembers(Group parent, Sensor<T> sensor, SensorEventListener<? super T> listener) { - return getSubscriptionContext().subscribeToMembers(parent, sensor, listener); + return subscriptions().subscribeToMembers(parent, sensor, listener); } @Override http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/software/webapp/src/main/java/org/apache/brooklyn/entity/proxy/nginx/UrlMappingImpl.java ---------------------------------------------------------------------- diff --git a/software/webapp/src/main/java/org/apache/brooklyn/entity/proxy/nginx/UrlMappingImpl.java b/software/webapp/src/main/java/org/apache/brooklyn/entity/proxy/nginx/UrlMappingImpl.java index 98d0de2..121219a 100644 --- a/software/webapp/src/main/java/org/apache/brooklyn/entity/proxy/nginx/UrlMappingImpl.java +++ b/software/webapp/src/main/java/org/apache/brooklyn/entity/proxy/nginx/UrlMappingImpl.java @@ -184,8 +184,8 @@ public class UrlMappingImpl extends AbstractGroupImpl implements UrlMapping { @Override public synchronized void recompute() { - if (subscriptionHandle != null) getSubscriptionContext().unsubscribe(subscriptionHandle); - if (subscriptionHandle2 != null) getSubscriptionContext().unsubscribe(subscriptionHandle2); + if (subscriptionHandle != null) subscriptions().unsubscribe(subscriptionHandle); + if (subscriptionHandle2 != null) subscriptions().unsubscribe(subscriptionHandle2); Entity t = getTarget(); if (t != null) { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b29795b/software/webapp/src/test/java/org/apache/brooklyn/entity/webapp/AbstractWebAppFixtureIntegrationTest.java ---------------------------------------------------------------------- diff --git a/software/webapp/src/test/java/org/apache/brooklyn/entity/webapp/AbstractWebAppFixtureIntegrationTest.java b/software/webapp/src/test/java/org/apache/brooklyn/entity/webapp/AbstractWebAppFixtureIntegrationTest.java index 7a66c87..9c77b45 100644 --- a/software/webapp/src/test/java/org/apache/brooklyn/entity/webapp/AbstractWebAppFixtureIntegrationTest.java +++ b/software/webapp/src/test/java/org/apache/brooklyn/entity/webapp/AbstractWebAppFixtureIntegrationTest.java @@ -369,11 +369,10 @@ public abstract class AbstractWebAppFixtureIntegrationTest { Entities.start(entity.getApplication(), ImmutableList.of(loc)); SubscriptionHandle subscriptionHandle = null; - SubscriptionContext subContext = ((EntityInternal)entity).getSubscriptionContext(); try { final List<SensorEvent> events = new CopyOnWriteArrayList<SensorEvent>(); - subscriptionHandle = subContext.subscribe(entity, WebAppService.REQUESTS_PER_SECOND_IN_WINDOW, new SensorEventListener<Double>() { + subscriptionHandle = entity.subscriptions().subscribe(entity, WebAppService.REQUESTS_PER_SECOND_IN_WINDOW, new SensorEventListener<Double>() { public void onEvent(SensorEvent<Double> event) { log.info("publishesRequestsPerSecondMetricRepeatedly.onEvent: {}", event); events.add(event); @@ -395,7 +394,7 @@ public abstract class AbstractWebAppFixtureIntegrationTest { } }}); } finally { - if (subscriptionHandle != null) subContext.unsubscribe(subscriptionHandle); + if (subscriptionHandle != null) entity.subscriptions().unsubscribe(subscriptionHandle); entity.stop(); } }