Presentation is loading. Please wait.

Presentation is loading. Please wait.

AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.

Similar presentations

Presentation on theme: "AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems."— Presentation transcript:

1 AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

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

3 Overview Plain Java AOP (syntax is JLS compatible) Annotation-defined aspects Integrates well in J2EE environments Load time, runtime and static weaving Runtime deployment and undeployment of aspects Open Source, founded Q4 2002 Performance is comparable to ~ AspectJ -XnoInline Extensible Aspect Container

4 Overview Pointcuts call(..), execution(..), set(..), get(..), cflow(..), cflowbelow(..), handler(..), staticinitialization(..), within(..), 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 AspectJ: * *. ( *)

5 Plain Java AOP JLS compatible Annotation-defined aspects –Java 5 @Annotation(…) –Java 1.3/1.4 /** @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

6 Code-style defined aspect - AspectJ 5 import; aspect AsynchAspect { private ExecutorService m_threadPool =... void around(): execution(void Baz.*(..)) { m_threadPool.execute(new Runnable() { public void run() { try { // proceed the execution in a new thread proceed(); } catch (Throwable e) { // handle exception } ); } Plain Java AOP

7 @Aspect class AsynchAspect { private ExecutorService m_threadPool =... @Around(execution(void*(..))) void execute(JoinPoint jp) throws Throwable { m_threadPool.execute(new Runnable() { public void run() { try { // proceed the execution in a new thread jp.proceed(); } catch (Throwable e) { // handle exception } ); } Annotation-defined aspect - AspectWerkz Plain Java AOP

8 @Aspect class AsynchAspect { private ExecutorService m_threadPool =... @Around(execution(void*(..))) void execute(ProceedingJoinPoint jp) throws Throwable { m_threadPool.execute(new Runnable() { public void run() { try { // proceed the execution in a new thread jp.proceed(); } catch (Throwable e) { // handle exception } ); } Annotation-defined aspect - AspectJ 5 Plain Java AOP

9 pointcut transactedOps() : execution(public void || execution(public void Account.debit(..)) || execution(public void Customer.setAddress(..)) || execution(public void Customer.addAccount(..)) || execution(public void Customer.removeAccount(..)); Annotation-driven AOP Plain Java AOP Annnotion-based pointcuts can –Raise the abstraction level –Be more robust and predictable –Rely on standard annotations EJB 3, JSR-250, JSR-181 pointcut transactedOps() : execution(@Transactional * *.*(..));

10 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 AspectJ 1.2 Challenges Plain Java AOP

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

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

13 Load-time weaving 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 AspectJ 5 –Java 5 using JVMTI agent –Java 1.3/1.4 using JRockit pre-processor Integration

14 Deployment modules 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 AspectJ 5 Integration

15 Proxy-based weaving Create a proxy Target target = (Target)Proxy.newInstance( 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 AspectJ 5 Integration

16 Proxy-based weaving 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 J2EE environments Drawbacks –Only method execution pointcuts –Can not advise: final methods private methods Integration

17 XML-based pointcut definitions AspectWerkz 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 AspectJ 5 –More restricted but guarantees consistent semantics –Still does what most users want; provide definition for abstract pointcuts (e.g. late binding) Integration

18 Sample: XML-based pointcut definitions Allows extending an abstract aspect Needs to provide definition for all abstract pointcuts <concrete-aspect name=myaspects.DeploymentTimeAspect extends=lib.AbstractLibraryAspect> <pointcut name=abstractPointcut expression=within(*)/> Integration

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

20 Runtime weaving Hot deployment and undeployment of aspects Based on HotSwap API Atomic change sets Support for rollback Resulting code is staticallly woven Dynamicity

21 Sample: Runtime weaving public void enableMonitoring() { Deployer.deploy(ResponseTimeAspect.class); } public void disableMonitoring() { Deployer.undeploy(ResponseTimeAspect.class); } public void enableMonitoring(String pointcut) { String xmlDef = " " + "<pointcut name=toMonitor' expression='" + pointcut + "'/> "; Deployer.deploy(ResponseTimeAspect.class, xmlDef); } Dynamicity

22 Challenges 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 Dynamicity

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

24 Limitations HotSwap 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 Dynamicity

25 Programmable per-instance deployment 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 Dynamicity

26 Sample: Programmable per-instance deployment Prepare the classes you might want to advise later <advisable pointcut-type="call|execution|set|get" expression="within(*)"/> Add advice to a specific instance (only) at runtime POJO pojo = new POJO(); ((Advisable) pojo).aw_addAdvice( "execution(@ javax.ejb.TransactionAttribute) ", new BeforeAdvice() { public void invoke(JoinPoint jp) { // start a new transaction } } ); Dynamicity Aspects.aspectOf(TxAspect.class).beginTx(jp);

27 Extensible aspect container Pluggable weaver Support for different aspect models Reference implementation supports –AspectWerkz –Spring AOP –AOP Alliance –AspectJ (not complete) Dynamicity etc. to all aspect models Weaver implementations –ASM –Javassist –JRockit VM weaving (prototype) Overview

28 Architecture overview Extensible aspect container

29 Discussion Why did we implement it? 1.Proof of concept for the merge with AspectJ 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 Extensible aspect container

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

31 Benefits for AspectJ users Production stable load-time weaving –Implemented in AspectJ 5 Runtime deployment of aspects Can reuse old AspectJ pointcut libraries Extensible aspect container

32 AW Bench Compares the performance of major (industrial) AOP implementations –AspectJ, AspectWerkz, JBoss AOP, JAsCo, Spring, dynaop, cglib proxy Currently tests: –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: – Micro-benchmark suite

33 Links Project home pages: – – Articles: – – Benchmark: – AspectJ 5 FAQ: – home/aj5announce.html JRockit home page: –

34 Questions...? Thanks for listening

Download ppt "AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems."

Similar presentations

Ads by Google