Friday, July 8, 2011

Remember When Twitter Was A Joke? No One Is Laughing Anymore.

Read more here

Friday, March 4, 2011

AspectJ Example with Weblogic Server

Overview

This example provide steps to configure AspectJ Load Time Weaving with WebLogic Server. Also provide steps to run sample application


Configuration



modify $DOMAIN_HOME/bin/startWebLogic.cmd

To add following"

1) include -javaagent:%ASPECTJ_HOME%\lib\aspectjweaver.jar to JAVA_OPTIONS

search for following line in startWebLogic.cmd
set JAVA_OPTIONS=%SAVE_JAVA_OPTIONS%

Add following snippet below it:

set ASPECTJ_HOME=C:\softwares\aspectj1.6
set JAVA_OPTIONS=-javaagent:%ASPECTJ_HOME%\lib\aspectjweaver.jar



2) include aspectjrt.jar in classpath

search for echo WLS Start Mode=%WLS_DISPLAY_MODE% in startWebLogic.cmd

Add following line below it:
set CLASSPATH=C:\softwares\aspectj1.6\lib\aspectjrt.jar;%CLASSPATH%

Sample Application

The sample app contains a servlet which make jdbc calls. And the aspectj will monitor for any calls made from classes present in java.sql package. I'll reuse code from Ramnivas Laddad's book (AspectJ in Action)

Servlet

package demo;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JDBCServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

res.setContentType("text/html");
PrintWriter out = res.getWriter();

try {
// Load (and therefore register) the Oracle Driver
Class.forName("oracle.jdbc.driver.OracleDriver");

// Get a Connection to the database
con = DriverManager.getConnection(
"jdbc:oracle:thin:@host:port:SID", "username", "pwd");

// Create a Statement object
stmt = con.createStatement();

// Execute an SQL query, get a ResultSet
rs = stmt.executeQuery("SELECT name, address FROM Employees");

// Display the result set as a list
while(rs.next()) {
out.println(rs.getString("name") + " " + rs.getString("address"));
}

}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
finally {
// Always close the database connection.
try {
if (con != null) con.close();
}
catch (SQLException ignored) { }
}
}
}

Aspect Sample

/*
Copyright 2009 Ramnivas Laddad

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//Listing 7.10 Performance monitoring aspect using an around() advice
package ajia.monitoring;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

//import ...

@Aspect
public abstract class Monitoring {
@Pointcut
public abstract void monitored();

@Around("monitored()")
public Object measureTime(ProceedingJoinPoint pjp) throws Throwable {
long startTime = System.nanoTime();
Object ret = pjp.proceed();
long endTime = System.nanoTime();
System.out.println("Method " + pjp.getSignature().toShortString()
+ " took " + (endTime - startTime));
return ret;
}
}

AOP.xml [from chapter 8 section 4 .4 LoadTimeWeaver ]



Application Structure






jdbc-aspect.jar structure







Deployment Steps

Deploy servlet in nostage mode from weblogic console
Create a jar which contains the aspect and aop.xml
Copy this jar to WEB-INF/lib folder of the servlet application.
Start weblogic using $DOMAIN_HOME/startWebLogic.cmd
Invoke the servlet : http://localhost:7001/JDBCServlet/JDBCServlet
Now messages from Aspectj are printed to console