AspectJ: syntax basics in Java

Maker QR Code ISO/IEC18004 in Java AspectJ: syntax basics

AspectJ: syntax basics
Make Quick Response Code In Java
Using Barcode generator for Java Control to generate, create QR Code JIS X 0510 image in Java applications.
www.OnBarcode.com
Recognize QR Code In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Invoking proceed() returns the value returned by the join point. Unless you need to manipulate the returned value, around advice will simply return the value that was returned by the proceed() statement within it. If you do not invoke proceed() , you will still have to return a value appropriate for the advice s logic. There are cases when an around advice applies to join points with different return types. For example, if you advise all the methods needing transaction support, the return values of all those methods are likely to be different. To resolve such situations, the around advice may declare its return value as Object. In those cases, if around returns a primitive type after it calls proceed(), the primitive type is wrapped in its corresponding wrapper type and performs the opposite, unwrapping after returning from the advice. For instance, if a join point returns an integer and the advice declares that it will return Object, the integer value will be wrapped in an Integer object and it will be returned from the advice. When such a value is assigned, the object is first unwrapped to an integer. Similarly, if a join point returns a non-primitive type, appropriate typecasts are performed before the return value is assigned. The scheme of returning the Object type works even when a captured join point returns a void type. 3.2.8 An example using around advice: failure handling Let s look at an example that uses around advice to handle system failures. In a distributed environment, dealing with a network failure is often an important task. If the network is down, clients often reattempt operations. In the following example, we examine how an aspect with around advice can implement the functionality to handle a network failure. In listing 3.1, we simulate the network and other failures by simply making the method throw an exception randomly.
Encode Barcode In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
GTIN - 12 Generation In Java
Using Barcode creation for Java Control to generate, create UPC Symbol image in Java applications.
www.OnBarcode.com
Listing 3.1 RemoteService.java
Code 128A Creation In Java
Using Barcode printer for Java Control to generate, create Code 128B image in Java applications.
www.OnBarcode.com
Creating 1D In Java
Using Barcode generation for Java Control to generate, create 1D image in Java applications.
www.OnBarcode.com
import java.rmi.RemoteException; public class RemoteService { public static int getReply() throws RemoteException { if(Math.random() > 0.25) { throw new RemoteException("Simulated failure occurred"); } System.out.println("Replying"); return 5; } }
Creating Code39 In Java
Using Barcode generation for Java Control to generate, create Code 39 Extended image in Java applications.
www.OnBarcode.com
EAN8 Generator In Java
Using Barcode printer for Java Control to generate, create UPC - 8 image in Java applications.
www.OnBarcode.com
Advice
Generating Denso QR Bar Code In None
Using Barcode generator for Software Control to generate, create QR Code 2d barcode image in Software applications.
www.OnBarcode.com
QR Reader In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
The getReply() method simulates the service offered. By checking against a randomly generated number, it simulates a failure resulting in an exception (statistically, the method will fail approximately 75 percent of the time a really high failure rate!). When it does not fail, it prints a message and returns 5. Next let s write a simple client (listing 3.2) that invokes the only method in RemoteService.
Read UPC-A Supplement 5 In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
EAN 13 Creator In .NET
Using Barcode creation for Reporting Service Control to generate, create EAN-13 Supplement 5 image in Reporting Service applications.
www.OnBarcode.com
Listing 3.2 RemoteClient.java
UPCA Decoder In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Paint UCC.EAN - 128 In Objective-C
Using Barcode generation for iPhone Control to generate, create UCC-128 image in iPhone applications.
www.OnBarcode.com
public class RemoteClient { public static void main(String[] args) throws Exception { int retVal = RemoteService.getReply(); System.out.println("Reply is " + retVal); } }
Make Code 39 Extended In Java
Using Barcode generation for Android Control to generate, create USS Code 39 image in Android applications.
www.OnBarcode.com
Read Data Matrix ECC200 In Visual Basic .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Now let s write an aspect to handle failures by reattempting the operation three times before giving up and propagating the failure to the caller (listing 3.3).
GS1 - 12 Encoder In VS .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create UPCA image in .NET applications.
www.OnBarcode.com
Make GS1 128 In .NET Framework
Using Barcode maker for .NET Control to generate, create GS1 128 image in VS .NET applications.
www.OnBarcode.com
Listing 3.3 FailureHandlingAspect.java
Scan Barcode In .NET Framework
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
Decoding QR-Code In Visual C#
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
import java.rmi.RemoteException; public aspect FailureHandlingAspect { final int MAX_RETRIES = 3;
of advice Object around() throws RemoteException : call(* RemoteService.get*(..) throws RemoteException) { int retry = 0; Pointcut while(true){ (anonymous) try{ part of advice return proceed(); } catch(RemoteException ex){ Execution of System.out.println("Encountered " + ex); captured if (++retry > MAX_RETRIES) { join point throw ex; } System.out.println("\tRetrying..."); } } }
Method part
We declare that the around advice will return Object to accommodate the potential different return value types in the captured join points. We also declare that
AspectJ: syntax basics
it may throw RemoteException to allow the propagating of any exception thrown by the execution of captured join points. The pointcut part of the advice uses an anonymous pointcut to capture all the getter methods in RemoteService that throw RemoteException. We simply return the value returned by the invocation of proceed(). Although the join point is returning an integer, AspectJ will take care of wrapping and unwrapping the logic. When we compile and run the program, we get output similar to the following:
> ajc RemoteService.java RemoteClient.java FailureHandlingAspect.java > java RemoteClient Encountered java.rmi.RemoteException: Simulated failure occurred Retrying... Encountered java.rmi.RemoteException: Simulated failure occurred Retrying... Replying Reply is 5
The output shows a few failures, retries, and eventual success. (Your output may be a little different due to the randomness introduced.) It also shows the correct assignment to the retVal member in the RemoteClient class, even though the advice returned the Object type. 3.2.9 Context collection example: caching The goal of this example is to understand how to collect context in arguments, execution objects, and return values. First, we write a method for a simple factorial computation, and then we write an aspect to cache the computed value for later use. We want to insert a result into the cache for values passed on to only nonrecursive calls (to limit the amount of caching). Before any calls to the factorial() method, including the recursive ones, we check the cache and print the value if a precomputed value is found. Otherwise, we proceed with the normal computation flow. Let s start with creating the factorial computation in listing 3.4.
Copyright © OnBarcode.com . All rights reserved.