@Override public void apply(EntityLocal entity) { SshFeed machineMetricsFeed = createMachineMetricsFeed(entity); ((EntityInternal) entity).feeds().add(machineMetricsFeed); addMachineMetricsEnrichers(entity); LOG.info("Configured machine metrics feed and enrichers on {}", entity); }
@Test public void testKeepsReachableAdjuncts() throws Exception { MyPolicy policy = origApp.policies().add(PolicySpec.create(MyPolicy.class)); MyEnricher enricher = origApp.enrichers().add(EnricherSpec.create(MyEnricher.class)); Feed feed = origApp.feeds().add(new MyFeed()); // Double-check we have the state we expected for the assertions that it is unmodified! BrooklynMementoRawData origData = getRawData(); assertTrue(origData.getPolicies().containsKey(policy.getId())); assertTrue(origData.getEnrichers().containsKey(enricher.getId())); assertTrue(origData.getFeeds().containsKey(feed.getId())); assertTransformIsNoop(origData); } }
@Override public void addFeeds(RebindContext rebindContext, EntityMemento memento) { for (String feedId : memento.getFeeds()) { AbstractFeed feed = (AbstractFeed) rebindContext.lookup().lookupFeed(feedId); if (feed != null) { try { entity.feeds().add(feed); } catch (Exception e) { rebindContext.getExceptionHandler().onAddFeedFailed(entity, feed, e); } try { if (!rebindContext.isReadOnly(feed)) { feed.start(); } } catch (Exception e) { rebindContext.getExceptionHandler().onRebindFailed(BrooklynObjectType.ENTITY, entity, e); } } else { LOG.warn("Feed not found; discarding feed {} of entity {}({})", new Object[] {feedId, memento.getType(), memento.getId()}); } } }
@Override public void init() { super.init(); // By calling feeds().add(...), it will persist the feed, and rebind it functionFeed = feeds().add(FunctionFeed.builder() .entity(this) .period(Duration.millis(10)) .uniqueTag("MyserviceWithFeeds-functionFeed") .poll(new FunctionPollConfig<Integer, Integer>(COUNTER) .suppressDuplicates(true) .onException(Functions.constant(-1)) .callable(new Callable<Integer>() { @Override public Integer call() { if (!Entities.isManaged(MyServiceWithFeedsImpl.this)) { feedCalledWhenNotManaged = true; throw new IllegalStateException("Entity "+MyServiceWithFeedsImpl.this+" is not managed in feed.call"); } Integer oldVal = sensors().get(COUNTER); return (oldVal == null ? 0 : oldVal) + 1; } })) .build()); subscribeToServiceState(); }
@Test public void testDeletesOrphanedFeeds() throws Exception { EntityInternal entity = origApp.addChild(EntitySpec.create(TestEntity.class).impl(MyEntity.class)); Feed feed = entity.feeds().add(new MyFeed()); MementoTweaker tweaker = new MementoTweaker(new Deletions().entities(entity.getId())); assertTransformDeletes(new Deletions().feeds(feed.getId()), tweaker); }
/** * Convenience, which calls {@link EntityInternal#feeds()} and {@link FeedSupport#addFeed(Feed)}. */ @Override public <T extends Feed> T addFeed(T feed) { return feeds().add(feed); }
@Test public void testKeepsLocationsReferencedByFeed() throws Exception { Location loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); MyFeed feed = new MyFeed(); feed.config().set(ConfigKeys.newConfigKey(Object.class, "myconfig"), loc); origApp.feeds().add(feed); assertTransformIsNoop(); }