Author: jstrachan Date: Tue Sep 4 18:28:27 2012 New Revision: 1380791 URL: http://svn.apache.org/viewvc?rev=1380791&view=rev Log: added a fix for CAMEL-5566 along the lines of Romain's use of @CamelContextId though preferred the name @CamelStartup which makes it really easy to startup a RouteBuilder on a CamelContext. Also patched the injection code to support @Inject @Uri, @EndpointInject, @Produce, @Consume to support multiple CamelContext instances too (needs testing though ;)
Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelStartup.java (with props) camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRoutes.java (contents, props changed) - copied, changed from r1380669, camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRouteConfig.java Removed: camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRouteConfig.java Modified: camel/trunk/components/camel-cdi/pom.xml camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java camel/trunk/examples/camel-example-cdi/src/test/java/org/apache/camel/example/cdi/IntegrationTest.java Modified: camel/trunk/components/camel-cdi/pom.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/pom.xml?rev=1380791&r1=1380790&r2=1380791&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/pom.xml (original) +++ camel/trunk/components/camel-cdi/pom.xml Tue Sep 4 18:28:27 2012 @@ -79,11 +79,6 @@ </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-ejb_3.1_spec</artifactId> - <version>${geronimo-ejb_3.1_spec.version}</version> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jcdi_1.0_spec</artifactId> <version>${geronimo-jcdi-1.0-spec.version}</version> </dependency> Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelStartup.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelStartup.java?rev=1380791&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelStartup.java (added) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelStartup.java Tue Sep 4 18:28:27 2012 @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; + +/** + * Used to annotate a {@link RouteBuilder} class to bind it to a + * {@link CamelContext} instance on startup so that it can startup automatically + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) +public @interface CamelStartup { + + /** + * Returns the name of the CamelContext to add the routes to. + * If no value is specified then the default CamelContext is used. + */ + String contextName() default ""; +} Propchange: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelStartup.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java?rev=1380791&r1=1380790&r2=1380791&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java Tue Sep 4 18:28:27 2012 @@ -16,6 +16,8 @@ */ package org.apache.camel.component.cdi; +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import javax.enterprise.inject.Instance; import javax.inject.Inject; @@ -51,4 +53,15 @@ public class CdiCamelContext extends Def return !instance.isUnsatisfied() && !instance.isAmbiguous(); } + @PostConstruct + @Override + public void start() throws Exception { + super.start(); + } + + @PreDestroy + @Override + public void stop() throws Exception { + super.stop(); + } } Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java?rev=1380791&r1=1380790&r2=1380791&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java Tue Sep 4 18:28:27 2012 @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import org.apache.camel.Consume; import org.apache.camel.EndpointInject; import org.apache.camel.Produce; import org.apache.camel.impl.CamelPostProcessorHelper; @@ -67,38 +68,61 @@ public class BeanAdapter { /** * Perform injections */ - public void inject(DefaultCamelBeanPostProcessor postProcessor, Object reference, + public void inject(CamelExtension camelExtension, Object reference, String beanName) { - CamelPostProcessorHelper postProcessorHelper = postProcessor.getPostProcessorHelper(); for (Method method : consumeMethods) { - postProcessorHelper.consumerInjection(method, reference, beanName); + Consume consume = method.getAnnotation(Consume.class); + if (consume != null) { + DefaultCamelBeanPostProcessor postProcessor = camelExtension.getPostProcessor( + consume.context()); + if (postProcessor != null) { + postProcessor.getPostProcessorHelper().consumerInjection(method, reference, beanName); + } + } } for (Method method : produceMethods) { Produce annotation = method.getAnnotation(Produce.class); - if (annotation != null && postProcessorHelper.matchContext(annotation.context())) { - postProcessor.setterInjection(method, reference, beanName, annotation.uri(), annotation.ref(), - annotation.property()); - + if (annotation != null) { + String contextName = annotation.context(); + DefaultCamelBeanPostProcessor postProcessor = camelExtension.getPostProcessor( + contextName); + if (postProcessor != null && postProcessor.getPostProcessorHelper().matchContext(contextName)) { + postProcessor.setterInjection(method, reference, beanName, annotation.uri(), annotation.ref(), + annotation.property()); + } } } for (Method method : endpointMethods) { EndpointInject annotation = method.getAnnotation(EndpointInject.class); - if (annotation != null && postProcessorHelper.matchContext(annotation.context())) { - postProcessor.setterInjection(method, reference, beanName, annotation.uri(), annotation.ref(), - annotation.property()); + if (annotation != null) { + String contextName = annotation.context(); + DefaultCamelBeanPostProcessor postProcessor = camelExtension.getPostProcessor( + contextName); + if (postProcessor != null && postProcessor.getPostProcessorHelper().matchContext(contextName)) { + postProcessor.setterInjection(method, reference, beanName, annotation.uri(), annotation.ref(), + annotation.property()); + } } } for (Field field : produceFields) { Produce annotation = field.getAnnotation(Produce.class); - if (annotation != null && postProcessorHelper.matchContext(annotation.context())) { - postProcessor.injectField(field, annotation.uri(), annotation.ref(), - annotation.property(), reference, beanName); + if (annotation != null) { + String contextName = annotation.context(); + DefaultCamelBeanPostProcessor postProcessor = camelExtension.getPostProcessor( + contextName); + if (postProcessor != null && postProcessor.getPostProcessorHelper().matchContext(contextName)) { + postProcessor.injectField(field, annotation.uri(), annotation.ref(), + annotation.property(), reference, beanName); + } } } for (Field field : endpointFields) { EndpointInject annotation = field.getAnnotation(EndpointInject.class); - if (annotation != null && postProcessorHelper.matchContext(annotation.context())) { + String contextName = annotation.context(); + DefaultCamelBeanPostProcessor postProcessor = camelExtension.getPostProcessor( + contextName); + if (postProcessor != null && postProcessor.getPostProcessorHelper().matchContext(contextName)) { postProcessor.injectField(field, annotation.uri(), annotation.ref(), annotation.property(), reference, beanName); } Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java?rev=1380791&r1=1380790&r2=1380791&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java Tue Sep 4 18:28:27 2012 @@ -19,17 +19,23 @@ package org.apache.camel.component.cdi.i import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.InjectionTarget; import org.apache.camel.CamelContext; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.cdi.CdiCamelContext; +import org.apache.camel.util.ObjectHelper; import org.apache.deltaspike.core.api.literal.AnyLiteral; import org.apache.deltaspike.core.api.literal.DefaultLiteral; @@ -39,10 +45,23 @@ import org.apache.deltaspike.core.api.li @SuppressWarnings("unchecked") public class CamelContextBean implements Bean<CdiCamelContext> { + private final BeanManager beanManager; + private final String name; + private final String camelContextName; + private final List<Bean<?>> routeBuilderBeans; private final InjectionTarget<CdiCamelContext> target; - public CamelContextBean(InjectionTarget<CdiCamelContext> injectionTarget) { - this.target = injectionTarget; + public CamelContextBean(BeanManager beanManager) { + this(beanManager, "CamelContext", "", Collections.EMPTY_LIST); + } + + public CamelContextBean(BeanManager beanManager, String name, String camelContextName, + List<Bean<?>> routeBuilderBeans) { + this.beanManager = beanManager; + this.name = name; + this.camelContextName = camelContextName; + this.routeBuilderBeans = routeBuilderBeans; + this.target = beanManager.createInjectionTarget(beanManager.createAnnotatedType(CdiCamelContext.class)); } @Override @@ -77,7 +96,7 @@ public class CamelContextBean implements @Override public String getName() { - return "CamelContext"; + return name; } @Override @@ -105,4 +124,29 @@ public class CamelContextBean implements return false; } + public String getCamelContextName() { + return camelContextName; + } + + protected void configureCamelContext(CdiCamelContext camelContext) { + if (ObjectHelper.isNotEmpty(camelContextName)) { + camelContext.setName(camelContextName); + } + for (Bean<?> bean : routeBuilderBeans) { + CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); + RouteBuilder routeBuilder = (RouteBuilder)beanManager.getReference(bean, RouteBuilder.class, creationalContext); + try { + camelContext.addRoutes(routeBuilder); + } catch (Exception e) { + throw new RuntimeCamelException("Could not add route builder " + routeBuilder + ". Reason: " + e, e); + } + } + } + + public CdiCamelContext configure() { + CreationalContext<CdiCamelContext> creationalContext = beanManager.createCreationalContext(this); + CdiCamelContext cdiCamelContext = create(creationalContext); + configureCamelContext(cdiCamelContext); + return cdiCamelContext; + } } Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java?rev=1380791&r1=1380790&r2=1380791&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java Tue Sep 4 18:28:27 2012 @@ -18,10 +18,11 @@ package org.apache.camel.component.cdi.i import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; -import javax.ejb.Startup; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.event.Observes; @@ -30,7 +31,6 @@ import javax.enterprise.inject.spi.After import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.BeforeShutdown; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.InjectionTarget; import javax.enterprise.inject.spi.ProcessAnnotatedType; @@ -44,9 +44,10 @@ import org.apache.camel.CamelContextAwar import org.apache.camel.Consume; import org.apache.camel.EndpointInject; import org.apache.camel.Produce; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CamelStartup; import org.apache.camel.component.cdi.CdiCamelContext; import org.apache.camel.impl.DefaultCamelBeanPostProcessor; -import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ReflectionHelper; import org.apache.deltaspike.core.api.provider.BeanProvider; import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder; @@ -56,13 +57,10 @@ import org.apache.deltaspike.core.util.m */ public class CamelExtension implements Extension { - /** - * Context instance. - */ - private CamelContext camelContext; - private DefaultCamelBeanPostProcessor postProcessor; - private Map<Bean<?>, BeanAdapter> eagerBeans = new HashMap<Bean<?>, BeanAdapter>(); + private Map<String, List<Bean<?>>> namedCamelContexts = new HashMap<String, List<Bean<?>>>(); + private List<CamelContextBean> camelContextBeans = new ArrayList<CamelContextBean>(); + private Map<String, CamelContext> camelContexts = new HashMap<String, CamelContext>(); public CamelExtension() { } @@ -103,34 +101,25 @@ public class CamelExtension implements E * @param manager Bean manager. */ protected void registerManagedCamelContext(@Observes AfterBeanDiscovery abd, BeanManager manager) { - abd.addBean(new CamelContextBean( - manager.createInjectionTarget(manager.createAnnotatedType(CdiCamelContext.class)))); - } - - /** - * Start up camel context. - * - * @param adv After deployment validation event. - * @throws Exception In case of failures. - */ - protected void validate(@Observes AfterDeploymentValidation adv) throws Exception { - getCamelContext().start(); - } - - /** - * Shutdown camel context. - * - * @param bsd Shutdown event. - * @throws Exception In case of failures. - */ - protected void shutdown(@Observes BeforeShutdown bsd) throws Exception { - if (camelContext != null) { - camelContext.stop(); + // lets ensure we have at least one camel context + if (namedCamelContexts.isEmpty()) { + abd.addBean(new CamelContextBean(manager)); + } else { + Set<Map.Entry<String, List<Bean<?>>>> entries = namedCamelContexts.entrySet(); + for (Map.Entry<String, List<Bean<?>>> entry : entries) { + String name = entry.getKey(); + List<Bean<?>> beans = entry.getValue(); + CamelContextBean camelContextBean = new CamelContextBean(manager, "CamelContext:" + name, name, beans); + camelContextBeans.add(camelContextBean); + abd.addBean(camelContextBean); + } } } /** - * Lets detect all beans annotated with @Consume + * Lets detect all beans annotated with @Consume and + * beans of type {@link RouteBuilder} which are annotated with {@link org.apache.camel.cdi.CamelStartup} + * so they can be auto-registered */ public void detectConsumeBeans(@Observes ProcessBean<?> event) { final Bean<?> bean = event.getBean(); @@ -145,30 +134,38 @@ public class CamelExtension implements E } }); - // lets force singletons and application scoped objects - // to be created eagerly to ensure they startup - if (eagerlyCreateSingletonsOnStartup() && isApplicationScopeOrSingleton(beanClass) && beanClass.getAnnotation(Startup.class) != null) { - eagerlyCreate(bean); + // detect all RouteBuilder instances + if (RouteBuilder.class.isAssignableFrom(beanClass)) { + CamelStartup annotation = beanClass.getAnnotation(CamelStartup.class); + if (annotation != null) { + String contextName = annotation.contextName(); + List<Bean<?>> beans = namedCamelContexts.get(contextName); + if (beans == null) { + beans = new ArrayList<Bean<?>>(); + namedCamelContexts.put(contextName, beans); + } + beans.add(bean); + } } } /** - * Should we eagerly startup @Singleton and @ApplicationScoped beans annotated with @Startup? - * Defaults to true which enables us to start camel contexts on startup - */ - protected boolean eagerlyCreateSingletonsOnStartup() { - return true; - } - - - /** * Lets force the CDI container to create all beans annotated with @Consume so that the consumer becomes active */ - public void startConsumeBeans(@Observes AfterDeploymentValidation event, BeanManager beanManager) { - ObjectHelper.notNull(getCamelContext(), "camelContext"); + public void startConsumeBeans(@Observes AfterDeploymentValidation event, BeanManager beanManager) throws Exception { + if (camelContextBeans.isEmpty()) { + CamelContext camelContext = BeanProvider.getContextualReference(CamelContext.class); + camelContexts.put("", camelContext); + } + for (CamelContextBean camelContextBean : camelContextBeans) { + CdiCamelContext context = camelContextBean.configure(); + camelContexts.put(camelContextBean.getCamelContextName(), context); + } + Set<Map.Entry<Bean<?>, BeanAdapter>> entries = eagerBeans.entrySet(); for (Map.Entry<Bean<?>, BeanAdapter> entry : entries) { Bean<?> bean = entry.getKey(); + BeanAdapter adapter = entry.getValue(); CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); // force lazy creation @@ -180,9 +177,10 @@ public class CamelExtension implements E /** * Lets perform injection of all beans which use Camel annotations */ - public void onInjectionTarget(@Observes ProcessInjectionTarget<Object> event) { - final InjectionTarget<Object> injectionTarget = event.getInjectionTarget(); - final Class<?> beanClass = event.getAnnotatedType().getJavaClass(); + @SuppressWarnings("unchecked") + public void onInjectionTarget(@Observes ProcessInjectionTarget event) { + final InjectionTarget injectionTarget = event.getInjectionTarget(); + final Class beanClass = event.getAnnotatedType().getJavaClass(); // TODO this is a bit of a hack - what should the bean name be? final String beanName = event.getInjectionTarget().toString(); final BeanAdapter adapter = createBeanAdapter(beanClass); @@ -194,7 +192,7 @@ public class CamelExtension implements E super.postConstruct(instance); // now lets do the post instruct to inject our Camel injections - adapter.inject(getPostProcessor(), instance, beanName); + adapter.inject(CamelExtension.this, instance, beanName); } }; event.setInjectionTarget(newTarget); @@ -212,11 +210,11 @@ public class CamelExtension implements E if (!adapter.isEmpty()) { // TODO this is a bit of a hack - what should the bean name be? final String beanName = bean.toString(); - adapter.inject(getPostProcessor(), bean, beanName); + adapter.inject(this, bean, beanName); } } - private BeanAdapter createBeanAdapter(Class<?> beanClass) { + private BeanAdapter createBeanAdapter(Class beanClass) { final BeanAdapter adapter = new BeanAdapter(); ReflectionHelper.doWithFields(beanClass, new ReflectionHelper.FieldCallback() { @Override @@ -251,11 +249,13 @@ public class CamelExtension implements E return adapter; } - protected DefaultCamelBeanPostProcessor getPostProcessor() { - if (postProcessor == null) { - postProcessor = new DefaultCamelBeanPostProcessor(getCamelContext()); + protected DefaultCamelBeanPostProcessor getPostProcessor(String context) { + CamelContext camelContext = camelContexts.get(context); + if (camelContext != null) { + return new DefaultCamelBeanPostProcessor(camelContext); + } else { + throw new IllegalArgumentException("No such CamelContext '" + context + "' available!"); } - return postProcessor; } protected BeanAdapter eagerlyCreate(Bean<?> bean) { @@ -267,14 +267,6 @@ public class CamelExtension implements E return beanAdapter; } - public CamelContext getCamelContext() { - if (camelContext == null) { - camelContext = BeanProvider.getContextualReference(CamelContext.class); - } - return camelContext; - } - - /** * Returns true if this field is annotated with @Inject */ @@ -285,7 +277,7 @@ public class CamelExtension implements E /** * Returns true for singletons or application scoped beans */ - private boolean isApplicationScopeOrSingleton(Class<?> aClass) { + protected boolean isApplicationScopeOrSingleton(Class<?> aClass) { return aClass.getAnnotation(Singleton.class) != null || aClass.getAnnotation(ApplicationScoped.class) != null; } } Copied: camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRoutes.java (from r1380669, camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRouteConfig.java) URL: http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRoutes.java?p2=camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRoutes.java&p1=camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRouteConfig.java&r1=1380669&r2=1380791&rev=1380791&view=diff ============================================================================== --- camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRouteConfig.java (original) +++ camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRoutes.java Tue Sep 4 18:28:27 2012 @@ -18,9 +18,6 @@ package org.apache.camel.example.cdi; import javax.annotation.PostConstruct; -import javax.ejb.Startup; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Produces; import javax.inject.Inject; import javax.inject.Named; @@ -28,17 +25,14 @@ import org.apache.activemq.camel.compone import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CamelStartup; import org.apache.camel.cdi.Uri; /** - * Configures all our Camel components, endpoints and beans and create the Camel routes + * Configures all our Camel routes, components, endpoints and beans */ -@ApplicationScoped -@Startup -public class MyRouteConfig { - - @Inject - private CamelContext camelContext; +@CamelStartup +public class MyRoutes extends RouteBuilder { @Inject //@Uri("activemq:test.MyQueue") @@ -49,22 +43,19 @@ public class MyRouteConfig { @Uri("file://target/testdata/result?noop=true") private Endpoint resultEndpoint; - @Produces - public RouteBuilder createRoutes() { - return new RouteBuilder() { - public void configure() { - // you can configure the route rule with Java DSL here - - // populate the message queue with some messages - from("file:src/data?noop=true"). - to(queueEndpoint); - - // consume from message queue to a result endpoint and process with a bean - from(queueEndpoint). - to(resultEndpoint). - bean(new SomeBean()); - } - }; + + @Override + public void configure() throws Exception { + // you can configure the route rule with Java DSL here + + // populate the message queue with some messages + from("file:src/data?noop=true"). + to(queueEndpoint); + + // consume from message queue to a result endpoint and process with a bean + from(queueEndpoint). + to(resultEndpoint). + bean(new SomeBean()); } /** @@ -77,14 +68,6 @@ public class MyRouteConfig { return answer; } - /** - * TODO can we avoid this bit and get CDI to automatically create a CamelContext and add its routes? - */ - @PostConstruct - public void start() throws Exception { - camelContext.addRoutes(createRoutes()); - } - public Endpoint getResultEndpoint() { return resultEndpoint; } Propchange: camel/trunk/examples/camel-example-cdi/src/main/java/org/apache/camel/example/cdi/MyRoutes.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: camel/trunk/examples/camel-example-cdi/src/test/java/org/apache/camel/example/cdi/IntegrationTest.java URL: http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-cdi/src/test/java/org/apache/camel/example/cdi/IntegrationTest.java?rev=1380791&r1=1380790&r2=1380791&view=diff ============================================================================== --- camel/trunk/examples/camel-example-cdi/src/test/java/org/apache/camel/example/cdi/IntegrationTest.java (original) +++ camel/trunk/examples/camel-example-cdi/src/test/java/org/apache/camel/example/cdi/IntegrationTest.java Tue Sep 4 18:28:27 2012 @@ -33,6 +33,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** */ @@ -40,7 +41,7 @@ import static org.junit.Assert.assertNot public class IntegrationTest { @Inject - MyRouteConfig config; + MyRoutes config; @Inject CamelContext camelContext; @@ -61,6 +62,8 @@ public class IntegrationTest { @Test public void integrationTest() throws Exception { assertNotNull("CamelContext not injected!", camelContext); + assertTrue("CamelContext is started", camelContext.getStatus().isStarted()); + assertNotNull("config not injected!", config); assertNotNull("MockEndpoint result not injected!", result); @@ -77,7 +80,7 @@ public class IntegrationTest { public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addPackage(CamelExtension.class.getPackage()) - .addPackage(MyRouteConfig.class.getPackage()) + .addPackage(MyRoutes.class.getPackage()) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } }