Apache Camel web service using spring

168 views Asked by At

I am very new to Apache Camel an please dont mind if I am wrong at any point. I have two questions regarding apache camel.

  1. In apache camel examples in internet, I can only see hitting a web service and routing the same to another. How can I call a WebService, unmarshall the object and use in my application instead of routing to another.
  2. In the examples, for calling camel context, we have to call

CamelContext context = new DefaultCamelContext();

context.start()

and

context.stop()

Is there any other way in spring to create a Singleton object in the application context and autowire the bean in my service class in the enterprise project?

Also It will be very helpful if anyone could point me any resources such as pdf or websites that can help me with this.

2

There are 2 answers

2
Sergey On

Just declare camel context as a normal bean

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd">

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <package>my.package.with.routebuilders</package>
    </camelContext>

And wire it in your service class

    @Autowired
    private CamelContext camelContext;
0
chresse On

to 1st:

you can create a processor to handle data from your webservice. your processor can be wired to your route builder. your routebuilder could look like the follwoing:

public class MyRouteBuilder extends SpringRouteBuilder {
    @Autowired
    private MyProcessor myProcessor;

    @Override
    public void configure() throws Exception {
        from("xy")
        .process(myProcessor);
    }
}

your processor:

public class MyProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        Message msg = exchange.getIn();
        //do something with your message
    }
}

of course you have to create a bean of your processor. you could do it with annotating it and enable the spring component scan or define it in your spring-context.xml:

<bean class="com.package.of.your.processor.MyProcessor" />

to 2nd:

it is like @Sergey said. you can instanciate your context in your spring config. with my example your springconfig would look like:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean class="com.package.of.your.processor.MyProcessor" />

    <bean id="myRouteBuilder" class="com.package.of.your.routbuilder.MyRouteBuilder" />

    <camel:camelContext id="camelContext">
        <camel:routeBuilder ref="myRouteBuilder" />
    </camel:camelContext>
</beans>