December 22, 2014
Hot Topics:

AspectJ Benefits for Java Development, Page 2

  • April 27, 2011
  • By Shekhar Gulati
  • Send Email »
  • More Articles »

AspectJ Offers Another Way to Look at Problems

Some time back, I wrote a piece of code which made a connection with a Web service and made a retry attempt if an exception was thrown.

public class MyService {

private WebService webService;

private final int NUMBER_OF_RETRIES = 5;

public List process(ProcessRequest request) {
int retryCount = 0;
return fetchResult(retryCount);
}

private List fetchResult(int retryCount) {
List result = null;
try {
result = webService.find();
} catch (WebServiceConnectionException e) {
System.out.println("Exception" + e);
if (++retryCount > NUMBER_OF_RETRIES) {
throw e;
}
System.out.println("Retrying…");
fetchResult(retryCount);
}
return result;
}

}

The pseudo code shown above makes a retry attempt if WebServiceConnectionException is thrown. It makes five retries, and after that it throws the exception. Retrying a network failure or Web service call is a very common problem, and most of the times it gets mixed with the business logic because developers do not find a way to separate the core business concern from the concern like a connection retry. Knowing AspectJ gives you another way to look at the problem and helps to avoid the code coupling between different concerns. As shown below we can write a simple Retry aspect, which can do the work for us, and we can use it at other places also.

public aspect RetryAspect {

private final int NUMBER_OF_RETRIES = 5;

pointcut retry() : call(public * *(..) throws com.developer.aspectj.part1.WebServiceConnectionException);

Object around() : retry() {
int retryCount = 0;
while (true) {
try {
return proceed();
} catch (WebServiceConnectionException e) {
System.out.println("Exception" + e);
if (++retryCount > NUMBER_OF_RETRIES) {
throw e;
}
System.out.println("Retrying…");
}
}
}

}

AspectJ Leads to Clean Code

If you have followed the article until now, you also have realized that AspectJ does not make code complex; rather, it simplifies your code and makes it cleaner. I think the code becomes more maintainable and easier to understand. AOP tries to reduce code scattering and code tangling. Code is tangled when its crosscutting concern code is intermixed with code that implements other concerns, and code is scattered when crosscutting code is spread out over multiple modules. Code scattering and tangling makes code difficult to maintain and evolve. Learning AOP will help you make your code more modular.

AspectJ Is Powerful, Mature, and Used in a Lot of Frameworks

AspectJ is not a new technology trend, as it has existed for more than 10 years. AspectJ is very mature, powerful and widely used in today's enterprise Java frameworks like Spring. Spring uses AspectJ to make it easy for the developers to use Transaction Management, or applying security (using Spring Security) in your application. Most Java developers use Spring or other frameworks that support AspectJ, but they do not know much about AspectJ. Knowing AspectJ will not only help in understanding how Spring framework uses AspectJ, but it can also help developers modularize the cross-cutting concerns specific to their application.

AspectJ is also used in a lot of other application frameworks (e.g. Spring Roo, Magma, and contract4j), monitoring tools (perf4j and glassbox), application servers (SpringSource dm server), and so on. If you know AspectJ, you can very easily understand the new frameworks which are using AspectJ and use them more effectively. For example, Spring Roo is a RAD tool that helps you build applications by generating code against the commands fired on the Roo shell. In Spring Roo, code generation artifacts are ITDs ("inter type declarations"), an AspectJ feature that provides static crosscutting. Static crosscutting means that it can change the structure of a class at compile time. It lets you add to your classes at compile time. So all the code is generated in ITDs, and at compile time that code is weaved into the target class. Hence, it helps in keeping generated artifacts away from the code that developer will change.

Shekhar Gulati -- Contributing Editor, Java -- is a Java consultant with over 5 years experience. He currently works with Xebia India, an Agile Software Development company. The opinions in this article and on his blog are his own and do not necessarily represent the opinions of his employer. His own blog is at and you can follow him on twitter here.


Tags: AspectJ, aspect-oriented programming

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel