Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

And now we have an unittest that sends a webservice request using good old Axis.

Annotations

Both Camel and Spring has annotations that can be used to configure and wire trivial settings more elegantly. Camel has the endpoint annotation @EndpointInjected that is just what we need. With this annotation we can inject the endpoint into our service. The annotation takes either a name or uri parameter. The name is the bean id in the Registry. The uri is the URI configuration for the endpoint. Using this you can actually inject an endpoint that you have not defined in the camel context. As we have defined our endpoint with the id backup we use the name parameter.

Code Block
java
java

    @EndpointInject(name = "backup")
    private ProducerTemplate template;

Camel is smart as @EndpointInject supports different kinds of object types. We like the ProducerTemplate so we just keep it as it is.
Since we use annotations on the field directly we do not need to set the property in the spring xml file so we change our service bean:

Code Block
xml
xml

    <bean id="incidentservice" class="org.apache.camel.example.axis.ReportIncidentService"/>

Running the unit test with mvn test reveals that it works nicely.

And since we use the @EndpointInject that refers to the endpoint with the id backup directly we can loose the template tag in the xml, so its shorter:

Code Block
xml
xml

    <bean id="incidentservice" class="org.apache.camel.example.axis.ReportIncidentService"/>

    <camel:camelContext id="camelContext">
        <!-- producer template exposed with this id -->
        <camel:template id="camelTemplate"/>

        <!-- endpoint named backup that is configued as a file component -->
        <camel:endpoint id="backup" uri="file://target?append=false"/>

    </camel:camelContext>

And the final touch we can do is that since the endpoint is injected with concrete endpoint to use we can remove the "backup" name parameter when we send the message. So we change from:

Code Block
java
java

        // send the data to the endpoint and the header contains what filename it should be stored as
        template.sendBodyAndHeader("backup", data, "org.apache.camel.file.name", filename);

To without the name:

Code Block
java
java

        // send the data to the endpoint and the header contains what filename it should be stored as
        template.sendBodyAndHeader(data, "org.apache.camel.file.name", filename);

Then we avoid to duplicate the name and if we rename the endpoint name then we don't forget to change it in the code also.

TODO: TODO: Use spring dependency injection instead of property
TODO: Use Camel annotations
TODO: Maybe a route?
TODO: Use Camel Test Kit
TODO: Add more links to Camel stuff
TODO: Link to other tutorials
TODO: TOC
TODO: Resources