Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professional Open Source © JBoss, Inc. 2003-2005. 1 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 1 Implementing Middleware with AOP JBoss.

Similar presentations


Presentation on theme: "Professional Open Source © JBoss, Inc. 2003-2005. 1 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 1 Implementing Middleware with AOP JBoss."— Presentation transcript:

1 Professional Open Source © JBoss, Inc. 2003-2005. 1 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 1 Implementing Middleware with AOP JBoss Aspect Oriented Middleware

2 © JBoss, Inc. 2003-2005. 2 Professional Open Source © JBoss Inc., 2003-2005. 2 Topics Overview From Interceptors to Aspects –J2EE a la carte –AOP + metadata From Aspects to EJB3 –Proxy based AOP –AOP Domains –Annotations as Meta Model –XML + Annotations Dynamic AOP –Transparent caching –JBoss Cache Demo –Designing a Cache

3 © JBoss, Inc. 2003-2005. 3 Professional Open Source © JBoss Inc., 2003-2005. 3 AOP and Middleware Middleware is but a collection of cross-cutting concerns Most middleware already sort of aspectized –CORBA and JBoss heavy use of interceptors Basing designs on AOP natural step Expensive to use regular OOP techniques to mix/match configurations Application is just configuration a set of cross-cutting concerns –Natural step is to implement middleware using AOP –More modular designs –Less code clutter

4 © JBoss, Inc. 2003-2005. 4 Professional Open Source © JBoss Inc., 2003-2005. 4 AOP and Middleware Aspect Oriented Middleware brings transparency System aspects can be made orthonogal Application code can free of system programming Middleware can be made simpler to use

5 © JBoss, Inc. 2003-2005. 5 Professional Open Source © JBoss Inc., 2003-2005. 5 AOP Makes things Easier AOP to implement Ease-of-Use –EJB 3 and annotation library implemented with AOP AOP for Ease-of-Extension –Ease-of-use frameworks written on top of AOP –AOP gives easy consistent way to extend behavior

6 Professional Open Source © JBoss, Inc. 2003-2005. 6 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 6 From Interceptors To Aspects JBoss 3.x to JBoss AOP

7 © JBoss, Inc. 2003-2005. 7 Professional Open Source © JBoss Inc., 2003-2005. 7 JBoss 3.x Architecture –J2EE behavior expressed as interceptors –Detyped method invocations Client JVM JMX Microkernel Server JVM EJB Container MBean 011101010101 RemoteInvoker Typed Interface Client Proxy RemoteInvoker EJB Container MBean Service MBean Invocation Port 1234 Port 4321 Client –Internally invocation objects are routed within the kernel

8 © JBoss, Inc. 2003-2005. 8 Professional Open Source © JBoss Inc., 2003-2005. 8 Cross-cutting Concern Standard CMP 2.x EntityBean org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor org.jboss.ejb.plugins.LogInterceptor org.jboss.ejb.plugins.SecurityInterceptor org.jboss.ejb.plugins.TxInterceptorCMT org.jboss.ejb.plugins.CallValidationInterceptor org.jboss.ejb.plugins.EntityCreationInterceptor org.jboss.ejb.plugins.EntityLockInterceptor org.jboss.ejb.plugins.EntityInstanceInterceptor org.jboss.ejb.plugins.EntityReentranceInterceptor org.jboss.resource.connectionmanager.CachedConnectionInterceptor org.jboss.ejb.plugins.EntitySynchronizationInterceptor org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor Standard CMP 2.x EntityBean org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor org.jboss.ejb.plugins.LogInterceptor org.jboss.ejb.plugins.SecurityInterceptor org.jboss.ejb.plugins.TxInterceptorCMT org.jboss.ejb.plugins.CallValidationInterceptor org.jboss.ejb.plugins.EntityCreationInterceptor org.jboss.ejb.plugins.EntityLockInterceptor org.jboss.ejb.plugins.EntityInstanceInterceptor org.jboss.ejb.plugins.EntityReentranceInterceptor org.jboss.resource.connectionmanager.CachedConnectionInterceptor org.jboss.ejb.plugins.EntitySynchronizationInterceptor org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor

9 © JBoss, Inc. 2003-2005. 9 Professional Open Source © JBoss Inc., 2003-2005. 9 Standard CMP 2.x EntityBean org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor org.jboss.ejb.plugins.LogInterceptor org.jboss.ejb.plugins.SecurityInterceptor org.jboss.ejb.plugins.TxInterceptorCMT org.jboss.ejb.plugins.CallValidationInterceptor org.jboss.ejb.plugins.EntityCreationInterceptor org.jboss.ejb.plugins.EntityLockInterceptor org.jboss.ejb.plugins.EntityInstanceInterceptor org.jboss.ejb.plugins.EntityReentranceInterceptor org.jboss.resource.connectionmanager.CachedConnectionInterceptor org.jboss.ejb.plugins.EntitySynchronizationInterceptor org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor Standard CMP 2.x EntityBean org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor org.jboss.ejb.plugins.LogInterceptor org.jboss.ejb.plugins.SecurityInterceptor org.jboss.ejb.plugins.TxInterceptorCMT org.jboss.ejb.plugins.CallValidationInterceptor org.jboss.ejb.plugins.EntityCreationInterceptor org.jboss.ejb.plugins.EntityLockInterceptor org.jboss.ejb.plugins.EntityInstanceInterceptor org.jboss.ejb.plugins.EntityReentranceInterceptor org.jboss.resource.connectionmanager.CachedConnectionInterceptor org.jboss.ejb.plugins.EntitySynchronizationInterceptor org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor Cross-cutting Concern Extract Aspects

10 © JBoss, Inc. 2003-2005. 10 Professional Open Source © JBoss Inc., 2003-2005. 10 Interceptors to Aspects Aspectizing JBoss seemed like logical next step Leverage pointcut expressions Bring J2EE a la cart to plain Java Leverage our existing interceptor design

11 Professional Open Source © JBoss, Inc. 2003-2005. 11 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 11 J2EE a la carte

12 © JBoss, Inc. 2003-2005. 12 Professional Open Source © JBoss Inc., 2003-2005. 12 Transaction Demarcation Transaction demarcation (method, field, constructor) You can specify transaction boundaries within code Tags can transparently interact with Transaction Manager –Begin, suspend, commit and rollback transactions –On method, field, or constructor execution EJB adjectives used to specify transactional behavior –Required, RequiresNew, Supports, Never, NotSupported, Mandatory Complete control over when a rollback is triggered –i.e. which thrown exceptions cause a rollback

13 © JBoss, Inc. 2003-2005. 13 Professional Open Source © JBoss Inc., 2003-2005. 13 Transaction Demarcation Annotations or XML metadata can specify annotation @Tx(TxType.REQUIRED) public void somepojoMethod() { … } @Tx(TxType.REQUIRED) public void somepojoMethod() { … } RequiresNew RequiresNew

14 © JBoss, Inc. 2003-2005. 14 Professional Open Source © JBoss Inc., 2003-2005. 14 Roled-based Security Secured access to any method, field, or constructor Only users of a certain role allowed to access Authentication/Authorization integrated with JBoss Security –Various Security Domains (LDAP, RDBMS, SRP, etc…) Access to username available within other interceptors

15 © JBoss, Inc. 2003-2005. 15 Professional Open Source © JBoss Inc., 2003-2005. 15 Role-Based security JDK 5.0 Annotations are usable @SecurityDomain(other) public class POJO { @Unchecked public POJO() {} @Exclude public exlucedMethod() {…} @Permissions({admin, manager}) public void someMethod() {…} @Permissions({user}) public static int status; } @SecurityDomain(other) public class POJO { @Unchecked public POJO() {} @Exclude public exlucedMethod() {…} @Permissions({admin, manager}) public void someMethod() {…} @Permissions({user}) public static int status; }

16 © JBoss, Inc. 2003-2005. 16 Professional Open Source © JBoss Inc., 2003-2005. 16 Role-Based security XML metadata can specify annotation other allowed someMethod Methods that connect be used excluded other allowed someMethod Methods that connect be used excluded

17 Professional Open Source © JBoss, Inc. 2003-2005. 17 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 17 Metadata

18 © JBoss, Inc. 2003-2005. 18 Professional Open Source © JBoss Inc., 2003-2005. 18 Metadata J2EE a la carte examples show importance of metadata Pointcut expressions arent always the silver bullet Can be confusing to developers –Developers want simple, familiar ways of applying aspects –Some developers like annotations –Others prefer XML to externalize configuration –An AOP framework needs to be aware of both.

19 © JBoss, Inc. 2003-2005. 19 Professional Open Source © JBoss Inc., 2003-2005. 19 Metadata JBoss AOP requirements –Support annotations in pointcut expressions –Support annotation overrides at runtime –Support untyped metadata –Support metadata/annotation defaults –Support XML externalization of metadata Allow metadata/annotations to be attached without modifying bytecode Metadata changeable on a per-deployment basis through XML Allow pointcut expressions to be aware of this attached metadata.

20 © JBoss, Inc. 2003-2005. 20 Professional Open Source © JBoss Inc., 2003-2005. 20 Dynamic Metadata Aspects can resolve metadata dynamically The AOP Invocation object (ThisJoinPoint) gives hooks for this. Metadata can be attached to a invocation and propagated –Aspects can pass information to one another locally or remotely Metadata can be overridden on a per Thread basis –Change behavior per Thread –Provide simple mechanism to clear all Thread metadata (Thread Pooling) Default values can be defined/managed per VM or per Cluster InvocationThreadClassVMCluster

21 Professional Open Source © JBoss, Inc. 2003-2005. 21 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 21 EJB 3.0 and JBoss AOP An aspect-oriented EJB container

22 © JBoss, Inc. 2003-2005. 22 Professional Open Source © JBoss Inc., 2003-2005. 22 EJB 3.0 and JBoss AOP Can EJB be implemented using AOP? Traditional AspectJ like weaving not possible –EJB is a proxy architecture by definition –proxy.someMethod() different behavior than this.someMethod() –Same class can be deployed in multiple containers –Mix/match of XML and Annotations JBoss 3.x and lower showed AOP was the right approach A Proxy-based AOP container was needed.

23 © JBoss, Inc. 2003-2005. 23 Professional Open Source © JBoss Inc., 2003-2005. 23 EJB 3.0 and JBoss AOP JBoss AOP binds aspects at class load time Since binding is java.lang.reflection based Very easy to create a proxy container a.k.a an EJB container Client JVM JBoss Remoting Server JVM AOP Container 011101010101 RemoteInvoker Typed Interface Client Proxy RemoteInvoker AOP Container Invocation Port 1234 Port 4321 Client

24 © JBoss, Inc. 2003-2005. 24 Professional Open Source © JBoss Inc., 2003-2005. 24 EJB 3.0 and JBoss AOP Why re-architect within an AOP Container? –Full pointcut expressions available –Per method advice chains –Pluggable annotations –Pluggable behavior –Reuse existing Aspect Library Remember? –If middleware is easy to use… –…it should also be easy to extend –AOP allows us to easily extend EJB behavior

25 © JBoss, Inc. 2003-2005. 25 Professional Open Source © JBoss Inc., 2003-2005. 25 EJB 3.0 and JBoss AOP The next issue: EJB Containers have similar, but different aspects –Stateless, Stateful, MDB (and JBoss extensions) Proxies to these container types also similar but different Needed a way to define a template for each of these types Needed a way to define a template for proxy definition Solution: –JBoss AOP Aspect Domains

26 © JBoss, Inc. 2003-2005. 26 Professional Open Source © JBoss Inc., 2003-2005. 26 Aspect Domains Advice Stacks: –Named chain of unbound advices –Proxies use these to understand what their advices should be Aspect Domains: –Define a set of pointcuts, metadata overrides, and advice bindings –Containers reference domains for their advices –Domains can inherit from one-another

27 © JBoss, Inc. 2003-2005. 27 Professional Open Source © JBoss Inc., 2003-2005. 27 Advice stacks Containers reference advice stacks when they create proxies Stacks are predefined and configurable

28 © JBoss, Inc. 2003-2005. 28 Professional Open Source © JBoss Inc., 2003-2005. 28 *(..))"> *(..))"> *(..))"> @org.jboss.ejb3.PoolClass (value=org.jboss.ejb3.ThreadlocalPool.class, maxSize=30, timeout=10000) *(..))"> *(..))"> *(..))"> @org.jboss.ejb3.PoolClass (value=org.jboss.ejb3.ThreadlocalPool.class, maxSize=30, timeout=10000) Aspect Domains Annotation Override Annotations with behavior Default advices

29 Professional Open Source © JBoss, Inc. 2003-2005. 29 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 29 Dynamic AOP Transparent caching

30 © JBoss, Inc. 2003-2005. 30 Professional Open Source © JBoss Inc., 2003-2005. 30 What is Dynamic AOP ? Hot-deployment of aspects Bind/unbind advices arbitrarily at runtime Per-instance AOP Add advices on a per instance basis

31 © JBoss, Inc. 2003-2005. 31 Professional Open Source © JBoss Inc., 2003-2005. 31 What is JBossCache ? Transparent caching Local or replicated –Synchronous or asynchronous replication (using JGroups) Transactional or non-transactional –Transactional Replication at TX commit DB isolation levels supported Support for pluggable TxManagers –Non-Transactional Replication after each modification Pluggable eviction policies –Ships with time-based and size-based (LRU) policies Cache loader –Persistent backend store (load - store)

32 © JBoss, Inc. 2003-2005. 32 Professional Open Source © JBoss Inc., 2003-2005. 32 AOP Cache Work with POJOs public class Person { String name=null; int age=0; Map hobbies=null; Address address=null; Set skills; List languages; public String getName() { return name; } public void setName(String name) { this.name=name; }... } public class Address { String street=null; String city=null; int zip=0; public String getStreet() { return street; } public void setStreet(String street) { this.street=street; }... } public class Address { String street=null; String city=null; int zip=0; public String getStreet() { return street; } public void setStreet(String street) { this.street=street; }... }

33 © JBoss, Inc. 2003-2005. 33 Professional Open Source © JBoss Inc., 2003-2005. 33 Interaction With Cache tree = new TreeCacheAop(); config = new PropertyConfigurator(); // configure tree cache. config.configure(tree, "META-INF/replSync-service.xml"); joe = new Person(); joe.setName("Joe Black"); joe.setAge(31); addr = new Address(); addr.setCity("Sunnyvale"); addr.setStreet("123 Albert Ave"); addr.setZip(94086); joe.setAddress(addr); Use Pojos as Pojos

34 © JBoss, Inc. 2003-2005. 34 Professional Open Source © JBoss Inc., 2003-2005. 34 Interaction With Cache Joes state is automatically transactional and replicated State replicated, synchronized at transaction commit/rollback tree.start(); // kick start tree cache tree.putObject("/aop/joe", joe); // add aop sanctioned object tx.begin(); joe.setAge(41); joe.getAddress().setZip(95124); tx.commit(); tree.start(); // kick start tree cache tree.putObject("/aop/joe", joe); // add aop sanctioned object tx.begin(); joe.setAge(41); joe.getAddress().setZip(95124); tx.commit();

35 Professional Open Source © JBoss, Inc. 2003-2005. 35 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 35 Demo JBossCache

36 © JBoss, Inc. 2003-2005. 36 Professional Open Source © JBoss Inc., 2003-2005. 36 How does it work? *)" /> *)" /> Classes must be prepared for field interception This preparation creates AOP hooks Any advice can be added where hooks are instrumented

37 © JBoss, Inc. 2003-2005. 37 Professional Open Source © JBoss Inc., 2003-2005. 37 How does it work? Uses reflection to discover the structure of a POJO –Requires a jboss-aop.xml to declare user-defined class to be advisable –Uses AOP to keep track of changes to a POJO putObject() breaks an object apart and maps it to the TreeCache –Primitive fields are mapped to entries in a node's attributes –Complex fields are mapped to child nodes (recursively) Transparent handling of object hierarchies No need for one-to-one, one-to-many, etc relationship declarations POJO inheritance hierarchy is preserved –We dynamically add a field advice to each complex object to keep track of state changes

38 © JBoss, Inc. 2003-2005. 38 Professional Open Source © JBoss Inc., 2003-2005. 38 JBoss Cache - Advices Each advice remembers the node to which it maps –On field read: advice returns the value from the TreeCache –On field write: advice updates the associated node's attributes, e.g. –person.getAddress().setCity("San Jose) generates a put("/322649/addr", "city", "San Jose") On TX commit: modified fields are replicated and written back to the POJO

39 © JBoss, Inc. 2003-2005. 39 Professional Open Source © JBoss Inc., 2003-2005. 39 JBoss Cache - Design Cache design itself can aspectized Cache Aspects –CacheLoader, cache persistence –Replication –Transactionality Cannot aspectize using class-level weaving Same classes but configured differently per instance Aspects can only be assembled if AOP framework support Dynamic AOP

40 © JBoss, Inc. 2003-2005. 40 Professional Open Source © JBoss Inc., 2003-2005. 40 JBoss Strategy Middleware is the perfect application of AOP –Transparency –Flexibility Annotations for ease of use AOP for ease of extension


Download ppt "Professional Open Source © JBoss, Inc. 2003-2005. 1 2/6/2014 Professional Open Source © JBoss Inc., 2003-2005. 1 Implementing Middleware with AOP JBoss."

Similar presentations


Ads by Google