简体   繁体   中英

Apache Camel Dynamic Consumers

I have created this Camel routes

from("direct:pageExtraction")
            .bean(PageManager.class, "setProperties(*, ${headers})")
            .filter().method(PageManager.class, "exists").to("seda:pagePostProcessing").end()
            .to("seda:pageImposition");

            from("seda:pagePostProcessing")
            .bean(PageManager.class, "extractThumbnail(*, ${headers})")
            .bean(PageManager.class, "extractCMYKSeparation(*, ${headers})")
            .bean(PageManager.class, "persist(*, ${headers})")
            .bean(PageManager.class, "cleanUp(${headers})")
            .to("seda:pageImposition");

            from("seda:pageImposition")
            .bean(PageManager.class, "extractImposition(*, ${headers})")
            .to("seda:printQueue");

At the end, the seda:printQueue has no consumers, sending a message in a route like this apparently works fine. Now I want to introduce a new consumer after the routes have been initialized, I thought it would be possible to create a Spring bean programmatically and let Camel pick up the bean using the @Consume(uri="seda:printQueue") annotation, but as soon as I create the consumer Camel complains

org.apache.camel.RuntimeCamelException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '4965d710-b5c7-41cf-97e9-a42bdfcea894' is defined]

Any thoughts?

[UPDATE]

I have traced back the error to the class where this new consumer is created, I'm instantiating the PrintQueue class and then integrating it to the Spring context using an AutowireCapableBeanFactory doing a factory.autowireBean(printQueueInstance) followed by factory.initializeBean(printQueueInstance, id) where id is the 4965d710-b5c7-41cf-97e9-a42bdfcea894 that appears in the exception above, so I think this has to be some kind of context scope problem, may be I am creating this bean in the main or web Spring context and it can't be access by the Camel context, is this possible?

Since this route is invoked synchronously via use of the "direct:" component, it does not appear to require "seda:" for asynchronous invocation of another bean. In this situation, it would appear simplest to invoke a bean with Camel's bean methods for the Java DSL. As an example shown in the Camel bean documentation at:

http://camel.apache.org/bean.html

I would simply do:

// Send message to the bean endpoint
// and invoke given method.
from("direct:start")
  // do other stuff in your route
   .beanRef("beanName", "methodName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM