Menu Close
Settings Close

Language and Page Formatting Options

Red Hat Training

A Red Hat training course is available for Red Hat Fuse

2.8. Aspect Oriented Programming

Overview

The aspect oriented programming (AOP) feature in Apache Camel enables you to apply before and after processing to a specified portion of a route. As a matter of fact, AOP does not provide anything that you could not do with the regular route syntax. The advantage of the AOP syntax, however, is that it enables you to specify before and after processing at a single point in the route. In some cases, this gives a more readable syntax. The typical use case for AOP is the application of a symmetrical pair of operations before and after a route fragment is processed. For example, typical pairs of operations that you might want to apply using AOP are: encrypt and decrypt; begin transaction and commit transaction; allocate resources and deallocate resources; and so on.

Java DSL example

In Java DSL, the route fragment to which you apply before and after processing is bracketed between aop() and end(). For example, the following route performs AOP processing around the route fragment that calls the bean methods:
from("jms:queue:inbox")
   .aop().around("log:before", "log:after")
       .to("bean:order?method=validate")
       .to("bean:order?method=handle")
   .end()
   .to("jms:queue:order");
Where the around() subclause specifies an endpoint, log:before, where the exchange is routed before processing the route fragment and an endpoint, log:after, where the exchange is routed after processing the route fragment.

AOP options in the Java DSL

Starting an AOP block with aop().around() is probably the most common use case, but the AOP block supports other subclauses, as follows:
  • around()—specifies before and after endpoints.
  • begin()—specifies before endpoint only.
  • after()—specifies after endpoint only.
  • aroundFinally()—specifies a before endpoint, and an after endpoint that is always called, even when an exception occurs in the enclosed route fragment.
  • afterFinally()—specifies an after endpoint that is always called, even when an exception occurs in the enclosed route fragment.

Spring XML example

In the XML DSL, the route fragment to which you apply before and after processing is enclosed in the aop element. For example, the following Spring XML route performs AOP processing around the route fragment that calls the bean methods:
<route>
    <from uri="jms:queue:inbox"/>
    <aop beforeUri="log:before" afterUri="log:after">
        <to uri="bean:order?method=validate"/>
        <to uri="bean:order?method=handle"/>
    </aop>
    <to uri="jms:queue:order"/>
</route>
Where the beforeUri attribute specifies the endpoint where the exchange is routed before processing the route fragment, and the afterUri attribute specifies the endpoint where the exchange is routed after processing the route fragment.

AOP options in the Spring XML

The aop element supports the following optional attributes:
  • beforeUri
  • afterUri
  • afterFinallyUri
The various use cases described for the Java DSL can be obtained in Spring XML using the appropriate combinations of these attributes. For example, the aroundFinally() Java DSL subclause is equivalent to the combination of beforeUri and afterFinallyUri in Spring XML.