Khangharoth

September 26, 2007

Tutorial: Profiling With AspectJ

Filed under: Java,JEE — khangharoth @ 8:00 am

Just to have background about what is Aspect Oriented Programming :Well we are in Object Oriented programming world.

Everything is object and concepts of OOP’s can applied very easily.Things are easy to maintain as things are very well encapsulated under objects and hence code changes are easy to implement and maintain.

This happens because each Object has a specific task ,and in well written OOP’s code change in some functionality involves changes at few places or to say only concerned objects change.

This is generally applicable for Business requirements which can be easily modularized,but what about the cross-cutting concerns.

Cross-Cutting Concerns: The concerns which are to be taken care of ,in almost every part of code or in other words,code for these concerns is scattered all around the code base.In short these requirement cross-cut at many places and hence regular concepts of OOP’s are not very useful in handling them.

These are generally requirements not specified by the user , but are needed for over-all well being of the projects.

So what are these kinds of requirements:

  • Security: Security checks may be need at each and every class.
  • Logging: Logging is a typical example of cross-cutting concerns.
  • Profiling:Need to know bottleneck in the system,so typically present at everywhere.

Now as this type of code will scattered everywhere in the main code.Other then reason’s like maintenance etc ,it also reduces the code readability and make code ugly.

We imagine if you have to change something ,then we need to go and change thousand’s of classes,ex:If we decide to change the way logging has been implemented,then probably we will need to open each and every file in the project and do the change.And we all know changes are the main cause of getting infected with new bugs.So again the goal is to minimize the effect or localize the effect of change.

Q)So how these cross-cutting features come’s into play?

A) We define pointCuts:These are basically some conditions which when fulfilled starts the magic.

so a pointcut in annotation will be defined as @Pointcut(“execution(* com.model.service.EmployeeService.*(..))”)
public void employeeService()
{}

This means employeeService pointcut is applicable to all methods of com.model.service.EmployeeService implementations.

for specific methods we write like

@Pointcut(“execution(* com.model.service.EmployeeService.doSomeStuff(..))”)
public void doSomeStuff()
{
}

This pointcut will pick only this specific method.

And then you define advice where you define actual code about what you want to do,advice is then linked to a pointcut.

   @SuppressWarnings(“unchecked”)
@Before(“employeeService()”)
public void methodEnter(JoinPoint pjp )
{
Object obj = pjp.getTarget();
LOG.debug( “[” + obj .toString + “] ” );

}

So suppose we need to introduce logging into our existing code-base.What are the steps needed?

  • Define  pointcut’s  specifying where these should be invoked. You can use wild-cards and this make pointcut definition very powerful.
  • Define an advice as what needs to be done when program flow reaches these pointcuts,i.e our logging code. Now it should be very clear as how we have modularize the cross-cutting concerns.No more we have code like Log.Debug all across out code-base.
  • We also has to define as which kind of advice it is.It means when this advice code will get executed.                                                 1) @Before :Runs before the actual method starts running,just after the arguments to the method call are evaluated.                                                                                                                                                                                                             2) After: similarly we have @AfterRetruning,@AfterThrowing and simply @After which is like finally i.e no matter how the method returns ,it will run.

In the example ,the goal is to try and have profiling enabled in the project.Its a very crude profiling which just logs how much time a method takes.

To run this example just create a new AspectJ project in Eclipse,and paste the code in corresponding java files.

TestClass.java is the one which have main method,also if eclipse you can see a nice Cross-Refrence view as which aspect is advising whom or other way round.

Note:Looking out for your comment’s!

TimerClass.java

TestClass.java

Profile.java

Profiler.java

ProfilerAspect.java

Advertisements

September 6, 2007

Difference Between JPA and Hibernate

Filed under: JEE — khangharoth @ 3:44 pm

My colleague just asked me this question and i thought its quite a common question in, lot of developers mind.

Well as you might aware of now we have a much leaner and easy to use Ejb 3 specifications.

The EJB 3.0 specififcation is divided into two parts the first which deals with session and MDBs and the second which deals with persistence and entity beans.

This later part is know as JPA.And as i say latter part so this is independent of session and other concepts.This means that this can be used outside of any Server or managed Container .You can use JPA in SDK just like you write “Hello World”,no need of any Server or container.

JPA works with normal java beans so your code can be just Java beans ,some XML/annotatons about how Object’s are mapped with tables and you are good to go,very easy to write test cases as well.

And hibernate 3.0 implements the JPA specification so  TopLink and other O/R mapping framework.So your code depends upon JPA Api and its implementation is supplied by any JPA impl vendor framework.

So in effect if you use JPA , you are using a standard Java Api and not some orthogonal Api and hence you have flexibility in your system.

Say initially you have used Hibernate but now want to shift to Other framework.That means JPA supports pluggable persistence providers.

JPA specification is heavily influenced by current O/R mapping frameworks like Hibernate and Top-Link .

So if i need to put in Concise words:

a) JPA is Persistence Api which your code should use.

b) JPA Api will pass on the call to actual peristence provider (ex:Hibernate/TopLink) to do the actual work.

c) If you are looking from Performance prespective ,it will be dependent on actual peristence provider (Hibernate/TopLink) and not on JPA as its just a wrapper layer.

d) If you are looking from code dependency prespective ,JPA makes more sense as your code is dependent on standard Java Api.

e) If you have used Hibernate then you will find that certain features are missing in JPA like criteria queries etc.This does not mean that you can’t write criteria query in your application, you need to get Session object from JPA Entity manager and now you are as good as in hibernate project.

But now your code is dependent on Specific impl (Hibernate),going forward you can see more things getting added in JPA (2.0)

f) Should you use JPA: My take is you should ,API is clean and although not everthing you need is their but is a good step forward.

Hopefully this give better picture as where all these pieces fits into an Java based Enterprise Application .

September 3, 2007

TestNG: Another Testing Framework

Filed under: JEE — khangharoth @ 4:39 pm

Well yet again , some new framework in the pool of already existing frameworks.They all do the same stuff and yet i repeatably come across something which gives more value than existing one’s.

So what is Test-NG , this is the definition from test-ng web site : http://testng.org/doc/

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:

  • JDK 5 Annotations (JDK 1.4 is also supported with JavaDoc annotations).
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Allows distribution of tests on slave machines.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for runtime and logging (no dependencies).
  • Dependent methods for application server testing.

TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc

I am using it now for my testing requirements and found quite useful when test-cases have dependency on each other or some other subtle things.Overall it does seems more useful and flexible than junit.

Anyway if you are familiar with Junit than it should be fairly easy to start with Test-NG.

Note: I am using Test-NG plug in for eclipse so guess things are fairly simple.

Create a free website or blog at WordPress.com.