You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by shekher awasthi <sh...@gmail.com> on 2011/08/02 18:00:20 UTC

how to get CamelContext using Spring

Hi All,

i am trying to use camel in my spring based  web-application here is the
entries from my web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>
</web-app>
my applicationContext.xml file is something like this

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd">
  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="seda:foo"/>
      <to uri="mock:results"/>
    </route>
  </camelContext>
</beans>

when i am starting my application it is showing that camel context has been
started and it has detected one route,now i want to execute a process where
camel should read this route and than start
process and complete the route as per configuration.But i am not sure how
can i get regrence of the camel context since if i create a new instance of
camelContext inside my java class
that means i am creating a new context and not the one i initiated using
spring,can any one suggest me a way to do it so that i can get refrence of
the existing camelContext being created by the Spring
so that i can take advantage of it.
Thanks in advance

Re: how to get CamelContext using Spring

Posted by anver <an...@gmail.com>.
Hi,
     Please give a "id" to the CamelContext. you can grab it using the
Spring's getBean method.
For example if you give the following camle context code inside your springs
application context xml then
you can get the "FIRST-CAMLE-CONTEXT" using the getBean() method

<camelContext id="FIRST-CAMEL-CONTEXT"
xmlns="http://camel.apache.org/schema/spring">
	<route>
		<from uri="direct:startReading" />
		<bean ref="myBean" method="readAndPint" />
	</route>
</camelContext>

<bean id="myBean" class="com.anver.test.ReadBean" />


public static void main(String[] args) throws Exception { 
   ApplicationContext context = new
ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
   CamelContext camelContext = (CamelContext)
context.getBean("FIRST-CAMEL-CONTEXT");	        
   System.out.println(camelContext.getEndpoints());
}


-Anver



--
View this message in context: http://camel.465427.n5.nabble.com/how-to-get-CamelContext-using-Spring-tp4659203p5728716.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: how to get CamelContext using Spring

Posted by Taariq Levack <ta...@gmail.com>.
Hi

Sounds like you want to implement CamelContextAware [1]

[1] http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/CamelContextAware.html

On 02 Aug 2011, at 6:00 PM, shekher awasthi <sh...@gmail.com> wrote:

> Hi All,
> 
> i am trying to use camel in my spring based  web-application here is the
> entries from my web.xml
> 
> <?xml version="1.0" encoding="UTF-8"?>
>    <web-app version="2.4">
>    <servlet>
>        <servlet-name>dispatcher</servlet-name>
>        <servlet-class>
>            org.springframework.web.servlet.DispatcherServlet
>        </servlet-class>
>        <load-on-startup>2</load-on-startup>
>    </servlet>
> <listener>
> 
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>  </listener>
> 
>    <servlet-mapping>
>        <servlet-name>dispatcher</servlet-name>
>        <url-pattern>*.*</url-pattern>
>    </servlet-mapping>
> </web-app>
> my applicationContext.xml file is something like this
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
>       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>       xmlns:context="http://www.springframework.org/schema/context"
>       xsi:schemaLocation="
>       http://www.springframework.org/schema/beans
>       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>       http://www.springframework.org/schema/context
>       http://www.springframework.org/schema/context/spring-context-2.5.xsd
>       http://camel.apache.org/schema/spring
>       http://camel.apache.org/schema/spring/camel-spring.xsd">
>  <camelContext xmlns="http://camel.apache.org/schema/spring">
>    <route>
>      <from uri="seda:foo"/>
>      <to uri="mock:results"/>
>    </route>
>  </camelContext>
> </beans>
> 
> when i am starting my application it is showing that camel context has been
> started and it has detected one route,now i want to execute a process where
> camel should read this route and than start
> process and complete the route as per configuration.But i am not sure how
> can i get regrence of the camel context since if i create a new instance of
> camelContext inside my java class
> that means i am creating a new context and not the one i initiated using
> spring,can any one suggest me a way to do it so that i can get refrence of
> the existing camelContext being created by the Spring
> so that i can take advantage of it.
> Thanks in advance