public void activate(ComponentContext context, Map<String, Object> properties) { try { ... } catch(Exception e) { context.disableComponent((String) properties.get(Constants.SERVICE_PID)); // not sure if a CE is best here... Maybe just rethrow the original one throw new ComponentException("Some reason"); } }
@Override public void handleLeaseFailure() { Bundle bundle = context.getBundleContext().getBundle(); String bundleName = bundle.getSymbolicName(); try { // plan A: try stopping oak-store-document log.error("handleLeaseFailure: stopping {}...", bundleName); bundle.stop(Bundle.STOP_TRANSIENT); log.error("handleLeaseFailure: stopped {}.", bundleName); // plan A worked, perfect! } catch (BundleException e) { log.error("handleLeaseFailure: exception while stopping " + bundleName + ": " + e, e); // plan B: stop only DocumentNodeStoreService (to stop the background threads) log.error("handleLeaseFailure: stopping DocumentNodeStoreService..."); context.disableComponent(DocumentNodeStoreService.class.getName()); log.error("handleLeaseFailure: stopped DocumentNodeStoreService"); // plan B succeeded. } } }).
@Override public void handleLeaseFailure() { Bundle bundle = context.getBundleContext().getBundle(); String bundleName = bundle.getSymbolicName(); try { // plan A: try stopping oak-store-document log.error("handleLeaseFailure: stopping {}...", bundleName); bundle.stop(Bundle.STOP_TRANSIENT); log.error("handleLeaseFailure: stopped {}.", bundleName); // plan A worked, perfect! } catch (BundleException e) { log.error("handleLeaseFailure: exception while stopping " + bundleName + ": " + e, e); // plan B: stop only DocumentNodeStoreService (to stop the background threads) log.error("handleLeaseFailure: stopping DocumentNodeStoreService..."); context.disableComponent(DocumentNodeStoreService.class.getName()); log.error("handleLeaseFailure: stopped DocumentNodeStoreService"); // plan B succeeded. } } }).
/** * Enable the FixedCronScheduleComponent service. * * @throws Exception * */ @Tooltip(description = "Enable the FixedCronScheduleComponent component with a fixed 'cron' service property of 1-30/2 * * * * ?, firing every 2 second the first 30 seconds of the minute", deflt = "", type = "") public CancellablePromise<?> fixedSchedule(int id, String cronExpression) throws Exception { assert fsc.get() == null : "This is a singleton, so we can only have one"; if (fsc.get() != null) throw new Exception("Already started"); FixedCronScheduleComponent.id = id; componentContext.enableComponent(FixedCronScheduleComponent.class .getName()); return new Closer<Object>( () -> componentContext .disableComponent(FixedCronScheduleComponent.class .getName())); }
componentContext.disableComponent(BootstrapConfiguration.COMPONENT_NAME);
@Test public void testActivateDeactivate() throws Exception { // then test normal start with a DocumentNodeStore DocumentMK mk1 = createMK(1, 0); DocumentDiscoveryLiteService discoveryLite = new DocumentDiscoveryLiteService(); PrivateAccessor.setField(discoveryLite, "nodeStore", mk1.nodeStore); BundleContext bc = mock(BundleContext.class); ComponentContext c = mock(ComponentContext.class); when(c.getBundleContext()).thenReturn(bc); discoveryLite.activate(c); verify(c, times(0)).disableComponent(DocumentDiscoveryLiteService.COMPONENT_NAME); discoveryLite.deactivate(); }
private void assertDisableComponent(final ModuleContext ctxA, ServiceA srvA) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); ServiceListener listener = new ServiceListener() { @Override public void serviceChanged(ServiceEvent event) { int type = event.getType(); ServiceReference<?> sref = event.getServiceReference(); List<String> clazzes = Arrays.asList(((String[]) sref.getProperty(Constants.OBJECTCLASS))); if (type == ServiceEvent.UNREGISTERING && clazzes.contains(ServiceA1.class.getName())) { ctxA.removeServiceListener(this); latch.countDown(); } } }; ctxA.addServiceListener(listener); ComponentContext ccA1 = srvA.getServiceA1().getComponentContext(); ccA1.disableComponent(ServiceA1.class.getName()); Assert.assertTrue(latch.await(2, TimeUnit.SECONDS)); ServiceReference<ServiceA> srefA = ctxA.getServiceReference(ServiceA.class); Assert.assertNull("ServiceReference null", srefA); ServiceReference<ServiceA1> srefA1 = ctxA.getServiceReference(ServiceA1.class); Assert.assertNull("ServiceReference null", srefA1); } }
logger.warn("issueClusterLocalHeartbeat: could not stop bundle: "+e, e); context.disableComponent(null);