Aspect Werkz 2 and the road to Aspect

  • Slides: 34
Download presentation
Aspect. Werkz 2 and the road to Aspect. J™ 5 Jonas Bonér Senior Software

Aspect. Werkz 2 and the road to Aspect. J™ 5 Jonas Bonér Senior Software Engineer BEA Systems

Outline • • • Overview Plain Java™ AOP Integration Dynamicity Extensible Aspect Container AW

Outline • • • Overview Plain Java™ AOP Integration Dynamicity Extensible Aspect Container AW Bench

Overview • • Plain Java™ AOP (syntax is JLS compatible) Annotation-defined aspects Integrates well

Overview • • Plain Java™ AOP (syntax is JLS compatible) Annotation-defined aspects Integrates well in J 2 EE environments Load time, runtime and static weaving Runtime deployment and undeployment of aspects Open Source, founded Q 4 2002 Performance is comparable to ~ Aspect. J™ -Xno. Inline • Extensible Aspect Container

Overview • Pointcuts call(. . ), execution(. . ), set(. . ), get(. .

Overview • Pointcuts call(. . ), execution(. . ), set(. . ), get(. . ), cflowbelow(. . ), handler(. . ), staticinitialization(. . ), withincode(. . ), hasfield(. . ), hasmethod(. . ), args(. . ), this(. . ), target(. . ) • Life-cycle singleton, perthis(. . ), pertarget(. . ), perclass(. . ), percflow(. . ) • Intertype declarations (ITD) through Mixins (interface + methods implementing this interface) • Pointcut pattern language very similar to Aspect. J™: <annotation>* <modifier>* <type>. <name>(<type>*)

Plain Java AOP • JLS compatible • Annotation-defined aspects – Java 5 – Java

Plain Java AOP • JLS compatible • Annotation-defined aspects – Java 5 – Java 1. 3/1. 4 @Annotation(…) /** @Annotation(…) */ • Benefits – Does not break IDEs, tools etc. – Gives the impression of flattening out the learning curve (psychological effect) • Drawbacks: – JLS compatibility: can not break type-checking, f. e. ITDs – Less elegant and concise syntax

Code-style defined aspect Aspect. J 5 Plain Java AOP import foo. bar. Baz; aspect

Code-style defined aspect Aspect. J 5 Plain Java AOP import foo. bar. Baz; aspect Asynch. Aspect { private Executor. Service m_thread. Pool =. . . void around(): execution(void Baz. *(. . )) { m_thread. Pool. execute(new Runnable() { public void run() { try { // proceed the execution in a new thread proceed(); } catch (Throwable e) { // handle exception } } ); } }

Annotation-defined aspect Aspect. Werkz Plain Java AOP @Aspect class Asynch. Aspect { private Executor.

Annotation-defined aspect Aspect. Werkz Plain Java AOP @Aspect class Asynch. Aspect { private Executor. Service m_thread. Pool =. . . @Around(“execution(void foo. bar. Baz. *(. . ))”) void execute(Join. Point jp) throws Throwable { m_thread. Pool. execute(new Runnable() { public void run() { try { // proceed the execution in a new thread jp. proceed(); } catch (Throwable e) { // handle exception } } ); } }

Annotation-defined aspect Aspect. J 5 Plain Java AOP @Aspect class Asynch. Aspect { private

Annotation-defined aspect Aspect. J 5 Plain Java AOP @Aspect class Asynch. Aspect { private Executor. Service m_thread. Pool =. . . @Around(“execution(void foo. bar. Baz. *(. . ))”) void execute(Proceeding. Join. Point jp) throws Throwable { m_thread. Pool. execute(new Runnable() { public void run() { try { // proceed the execution in a new thread jp. proceed(); } catch (Throwable e) { // handle exception } } ); } }

Annotation-driven AOP Plain Java AOP pointcut transacted. Ops() : execution(public void Account. credit(. .

Annotation-driven AOP Plain Java AOP pointcut transacted. Ops() : execution(public void Account. credit(. . )) || execution(public void Account. debit(. . )) || execution(public void Customer. set. Address(. . )) || execution(public void Customer. add. Account(. . )) || execution(public void Customer. remove. Account(. . )); pointcut transacted. Ops() : execution(@Transactional * *. *(. . )); • Annnotion-based pointcuts can – Raise the abstraction level – Be more robust and predictable – Rely on standard annotations • EJB 3, JSR-250, JSR-181

Challenges • Plain Java AOP Challenges 1. Allow the user to have strong typed

Challenges • Plain Java AOP Challenges 1. Allow the user to have strong typed direct access to parameter values 2. Should be able to pass them on in the proceed(. . ) without wrapping them 3. Performance comparable with Aspect. J™ 1. 2

Challenges • Plain Java AOP Solutions 1. 2. 3. Use args(. . ) for

Challenges • Plain Java AOP Solutions 1. 2. 3. Use args(. . ) for parameter access Custom proceed: user defines a custom Join. Point interface and defines his own proceed(. . ) Implementation of the interface is generated on the fly public interface My. JP extends Join. Point { Object proceed(long[] l, String s); } @Around(. . ) Object advice(My. JP jp, long[] l, String s) {. . . // modify the values return jp. proceed(l, s); }

Integration • • Load time weaving Deployment modules (aop. xml) Proxy based weaving XML-based

Integration • • Load time weaving Deployment modules (aop. xml) Proxy based weaving XML-based pointcut definitions

Load-time weaving Integration • Classes are weaved when loaded • Pre Java 5 –

Load-time weaving Integration • Classes are weaved when loaded • Pre Java 5 – JRockit™ pre-processor – Class loader patching (credit to JMangler) • Standardized in Java 5 – JVMTI (agents) • Implemented in Aspect. J™ 5 – Java 5 using JVMTI agent – Java 1. 3/1. 4 using JRockit™ pre-processor

Deployment modules Integration • Need to control which aspects are used in which module

Deployment modules Integration • Need to control which aspects are used in which module (class loader) – Include or exclude aspects higher up in class loader hierarchy (or in my own module) => need for an AOP ‘deployment descriptor’ • Enabled through the aop. xml file • Is put in the META-INF directory in a JAR, WAR or EAR file • Implemented in Aspect. J™ 5

Proxy-based weaving Integration • Create a proxy Target target = (Target)Proxy. new. Instance( Target.

Proxy-based weaving Integration • Create a proxy Target target = (Target)Proxy. new. Instance( Target. class ); • Proxy for target class is created (on the fly) and weaved before returned • The target instance will now be advised by all the matching aspects (that are found in the class hierarchy) • Implemented for Aspect. J™ 5

Proxy-based weaving Integration • Benefits – Very transparent and lightweight – Same performance as

Proxy-based weaving Integration • Benefits – Very transparent and lightweight – Same performance as static weaving (much better than all other proxy implementations) – Same aspects and pointcuts can be used – no need for using Type+ etc. – Good enough in many situations in J 2 EE environments • Drawbacks – Only ‘method execution’ pointcuts – Can not advise: • final methods • private methods

XML-based pointcut definitions Integration • Aspect. Werkz allows defining the whole aspect in XML

XML-based pointcut definitions Integration • Aspect. Werkz allows defining the whole aspect in XML (aop. xml) – Is very widely used by users – Had many benefits (f. e. no extra compilation step) – Powerful • Drawbacks – Sometimes too powerful – Error prone – Hard to keep consistent semantics • Solved in a better way in Aspect. J™ 5 – More restricted but guarantees consistent semantics – Still does what most users want; provide definition for abstract pointcuts (e. g. “late binding”)

Sample: XML-based pointcut definitions Integration • Allows extending an abstract aspect • Needs to

Sample: XML-based pointcut definitions Integration • Allows extending an abstract aspect • Needs to provide definition for all abstract pointcuts <aspectj> <aspects> <concrete-aspect name=“myaspects. Deployment. Time. Aspect” extends=“lib. Abstract. Library. Aspect”> <pointcut name=“abstract. Pointcut” expression=“within(com. biz. . *)”/> </concrete-aspect> </aspectj>

Dynamicity • Runtime deployment of aspects (runtime weaving) • Programmable per-instance deployment of aspects

Dynamicity • Runtime deployment of aspects (runtime weaving) • Programmable per-instance deployment of aspects

Runtime weaving Dynamicity • ”Hot” deployment and undeployment of aspects • Based on Hot.

Runtime weaving Dynamicity • ”Hot” deployment and undeployment of aspects • Based on Hot. Swap API • Atomic ’change sets’ • Support for rollback • Resulting code is staticallly woven

Sample: Runtime weaving Dynamicity public void enable. Monitoring() { Deployer. deploy(Response. Time. Aspect. class);

Sample: Runtime weaving Dynamicity public void enable. Monitoring() { Deployer. deploy(Response. Time. Aspect. class); } public void disable. Monitoring() { Deployer. undeploy(Response. Time. Aspect. class); } public void enable. Monitoring(String pointcut) { String xml. Def = "<aspect>" + "<pointcut name=‘to. Monitor' expression='" + pointcut + "'/></aspect>"; Deployer. deploy(Response. Time. Aspect. class, xml. Def); }

Challenges Dynamicity • Same performance for runtime deployed aspects as for regularly woven aspects

Challenges Dynamicity • Same performance for runtime deployed aspects as for regularly woven aspects • Zero overhead for target code after an aspect has been undeployed • Ensure consistent and atomic aspect deployments – At specific join point – “all or nothing”

Architecture Dynamicity 1. Join. Point abstraction – – – Generated at runtime Contains all

Architecture Dynamicity 1. Join. Point abstraction – – – Generated at runtime Contains all data and logic for this join point Has a public method: public static final invoke(…){…} – – 2. 3. Inlined by VM� Also functions as the “closure” for around advice (can hold state etc. ) Weave in a call to this static method in the target class (replaces the join point “code block”) Can swap the logic in the Join. Point without affecting the target class

Limitations Dynamicity • Hot. Swap limitations: – New methods can not be added at

Limitations Dynamicity • Hot. Swap limitations: – New methods can not be added at runtime (supported in spec. but currently not in any impl. ) – Means that execution pointcuts for around advice needs to be prepared (we add empty method) • Need wrapper methods to access private methods and fields in target class

Programmable per-instance deployment Dynamicity • Advice can be added to specific instance – Runtime

Programmable per-instance deployment Dynamicity • Advice can be added to specific instance – Runtime deployment – Is pure per-instance (always ”this” instance) • Target classes must be ‘prepared’ – set to implement the Advisable interface • Credit to JBoss AOP • Drawbacks – Introduces a new API

Sample: Programmable per-instance deployment Dynamicity • Prepare the classes you might want to advise

Sample: Programmable per-instance deployment Dynamicity • Prepare the classes you might want to advise later <advisable pointcut-type="call|execution|set|get" expression="within(com. biz. . *)"/> • Add advice to a specific instance (only) at runtime POJO pojo = new POJO(); ((Advisable) pojo). aw_add. Advice( "execution(@javax. ejb. Transaction. Attribute)", new Before. Advice() { public void invoke(Join. Point jp) { // start a new transaction Aspects. aspect. Of(Tx. Aspect. class). begin. Tx(jp); } } );

Extensible aspect container • Pluggable weaver • Support for different ’aspect models’ • Reference

Extensible aspect container • Pluggable weaver • Support for different ’aspect models’ • Reference implementation supports – – Aspect. Werkz Spring AOP Alliance Aspect. J™ (not complete) • Dynamicity etc. to all aspect models • Weaver implementations – ASM – Javassist – JRockit™ VM weaving (prototype) Overview

Architecture overview Extensible aspect container

Architecture overview Extensible aspect container

Discussion • Extensible aspect container Why did we implement it? 1. Proof of concept

Discussion • Extensible aspect container Why did we implement it? 1. Proof of concept for the merge with Aspect. J™ 2. To ensure correct semantics when different frameworks co-exist (aspect precedence etc. ) 3. To give (almost) equally good performance for all the ‘aspect models’

Performance • Spring aspects are executed – – • up to 13 times faster

Performance • Spring aspects are executed – – • up to 13 times faster when running in the aspect container Similar for all proxy based implementations Aspect. J™ aspects are executed – – • Extensible aspect container from equally fast (before/after/non-inlined around) to 6 times slower (inlined around) For details see the AW Bench microbenchmark suite

Benefits for Aspect. J users Extensible aspect container • Production stable load-time weaving –

Benefits for Aspect. J users Extensible aspect container • Production stable load-time weaving – Implemented in Aspect. J™ 5 • • Runtime deployment of aspects Can reuse old Aspect. J™ pointcut libraries

AW Bench • Compares the performance of major (industrial) AOP implementations – • •

AW Bench • Compares the performance of major (industrial) AOP implementations – • • Aspect. J, Aspect. Werkz, JBoss AOP, JAs. Co, Spring, dynaop, cglib proxy Currently tests: – – – • Micro-benchmark suite All advice types (not ITDs) Different kinds of context exposure (reflective and static) execution pointcuts and singleton aspects only Please contribute – with tests and/or new frameworks More info: – http: //docs. codehaus. org/display/AW/AOP+Benchmark �

Links • Project home pages: – http: //aspectwerkz. codehaus. org/ – http: //eclipse. org/aspectj/

Links • Project home pages: – http: //aspectwerkz. codehaus. org/ – http: //eclipse. org/aspectj/ • Articles: – http: //www. theserverside. com/articles/article. tss? l=Aspect. Werkz. P 1 – http: //www. theserverside. com/articles/article. tss? l=Aspect. Werkz. P 2 • Benchmark: • • – http: //docs. codehaus. org/display/AW/AOP+Benchmark Aspect. J™ 5 FAQ: – http: //dev. eclipse. org/viewcvs/indextech. cgi/~checkout~/aspectjhome/aj 5 announce. html JRockit™ home page: – http: //commerce. bea. com/products/weblogicjrockit/5. 0/jr_50. jsp

Questions. . . ? Thanks for listening

Questions. . . ? Thanks for listening