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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值