public void testTwoManagedCamelContextClash() throws Exception { // JMX tests dont work well on AIX CI servers (hangs them) if (isPlatform("aix")) { return; } camel1 = createCamelContext(); camel1.start(); assertTrue("Should be started", camel1.getStatus().isStarted()); MBeanServer mbeanServer = camel1.getManagementStrategy().getManagementAgent().getMBeanServer(); ObjectName on = ObjectName.getInstance("org.apache.camel:context=" + camel1.getManagementName() + ",type=context,name=\"camel-1\""); assertTrue("Should be registered", mbeanServer.isRegistered(on)); // now cheat and reset the counter so we can test for a clash DefaultCamelContextNameStrategy.setCounter(0); camel2 = createCamelContext(); camel2.start(); ObjectName on2 = ObjectName.getInstance("org.apache.camel:context=" + camel2.getManagementName() + ",type=context,name=\"camel-1\""); assertTrue("Should be registered", mbeanServer.isRegistered(on2)); assertTrue("Should still be registered after name clash", mbeanServer.isRegistered(on)); assertTrue("Should still be registered after name clash", mbeanServer.isRegistered(on2)); }
@Override public T produce(CreationalContext<T> ctx) { T context = super.produce(ctx); // Register the context in the OSGi registry BundleContext bundle = BundleContextUtils.getBundleContext(getClass()); context.getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundle)); if (!(context instanceof DefaultCamelContext)) { // Fail fast for the time being to avoid side effects by some methods get declared on the CamelContext interface throw new InjectionException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext"); } DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class); adapted.setRegistry(OsgiCamelContextHelper.wrapRegistry(context, context.getRegistry(), bundle)); CamelContextNameStrategy strategy = context.getNameStrategy(); OsgiCamelContextHelper.osgiUpdate(adapted, bundle); // FIXME: the above call should not override explicit strategies provided by the end user or should decorate them instead of overriding them completely if (!(strategy instanceof DefaultCamelContextNameStrategy)) { context.setNameStrategy(strategy); } return context; }
@Override public T produce(CreationalContext<T> cc) { T context = super.produce(cc); // Register the context in the OSGi registry BundleContext bundle = BundleContextUtils.getBundleContext(getClass()); context.getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundle)); if (!(context instanceof DefaultCamelContext)) // Fail fast for the time being to avoid side effects by some methods get declared on the CamelContext interface throw new DeploymentException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext"); DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class); adapted.setRegistry(OsgiCamelContextHelper.wrapRegistry(context, context.getRegistry(), bundle)); CamelContextNameStrategy strategy = context.getNameStrategy(); OsgiCamelContextHelper.osgiUpdate(adapted, bundle); // FIXME: the above call should not override explicit strategies provided by the end user or should decorate them instead of overriding them completely if (!(strategy instanceof DefaultCamelContextNameStrategy)) context.setNameStrategy(strategy); return context; }
@Override public T produce(CreationalContext<T> ctx) { T context = super.produce(ctx); // Do not override the name if it's been already set (in the bean constructor for example) if (context.getNameStrategy() instanceof DefaultCamelContextNameStrategy) { context.setNameStrategy(nameStrategy(annotated)); } // Add bean registry and Camel injector if (context instanceof DefaultCamelContext) { DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class); adapted.setRegistry(new CdiCamelRegistry(manager)); adapted.setInjector(new CdiCamelInjector(context.getInjector(), manager)); } else { // Fail fast for the time being to avoid side effects by the time these two methods get declared on the CamelContext interface throw new InjectionException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext"); } // Add event notifier if at least one observer is present Set<Annotation> qualifiers = annotated.getAnnotations().stream() .filter(isAnnotationType(Named.class).negate() .and(q -> manager.isQualifier(q.annotationType()))) .collect(toSet()); qualifiers.add(ANY); if (qualifiers.size() == 1) { qualifiers.add(DEFAULT); } qualifiers.retainAll(extension.getObserverEvents()); if (!qualifiers.isEmpty()) { context.getManagementStrategy().addEventNotifier(new CdiEventNotifier(manager, qualifiers)); } return context; }
@Override public T produce(CreationalContext<T> cc) { T context = super.produce(cc); // Do not override the name if it's been already set (in the bean constructor for example) if (context.getNameStrategy() instanceof DefaultCamelContextNameStrategy) context.setNameStrategy(nameStrategy(annotated)); // Add bean registry and Camel injector if (context instanceof DefaultCamelContext) { DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class); adapted.setRegistry(new CdiCamelRegistry(manager)); adapted.setInjector(new CdiCamelInjector(context.getInjector(), manager)); } else { // Fail fast for the time being to avoid side effects by the time these two methods get declared on the CamelContext interface throw new DeploymentException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext"); } // Add event notifier if at least one observer is present Set<Annotation> qualifiers = annotated.getAnnotations().stream() .filter(isAnnotationType(Named.class).negate() .and(q -> manager.isQualifier(q.annotationType()))) .collect(toSet()); qualifiers.add(Any.Literal.INSTANCE); if (qualifiers.size() == 1) qualifiers.add(Default.Literal.INSTANCE); qualifiers.retainAll(manager.getExtension(CdiCamelExtension.class).getObserverEvents()); if (!qualifiers.isEmpty()) context.getManagementStrategy().addEventNotifier(new CdiEventNotifier(manager, qualifiers)); return context; }
@Override protected void setupCamelContext(CamelContext camelContext) throws Exception { camelContext.setNameStrategy(new DefaultCamelContextNameStrategy("Gateway")); }