Jboss and Aspect-oriented programming

Započeo maxogm, 26.12.2010, 23:57

prethodna tema - sledeća tema

maxogm

26.12.2010, 23:57 Poslednja Izmena: 27.12.2010, 00:00 od maxogm
1.   Preuzmite jboss-aop - http://jboss.org/jbossaop/downloads.
2.   U Jboss server konfiguraciji, obrisite deploy/jboss-aop.deployer direktorijum.
3.   Kopirajte
jboss-aop_1.5.6.GA/jboss-40-install/jboss-aop-jdk50.deployer u Jboss server deploy direktorijum
4.   Kopirajte 
jboss-aop_1.5.6.GA/lib-50/pluggable-instrumentor.jar u Jboss server bin direktorijum
5.   Izmenite
Jboss server/deploy/jboss-aop-jdk50.deployer/META-INF/jboss-service.xml,

postavite EnableLoadTimeWeaving na vrednost true:

<attribute name="EnableLoadtimeWeaving">true</attribute>

6.   izmenite Jboss server/bin/run.bat(Windows) i run.conf za Linux OS.

run.bat

line:
set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx512m
change with:
set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx512m -javaagent:pluggable-instrumentor.jar


run.conf

line:
JAVA_OPTS="-server -Xms128m -Xmx128m"
change with:
JAVA_OPTS="-server -Xms128m -Xmx128m -javaagent:pluggable-instrumentor.jar"


Sada imate podesen Jboss za rad sa AOP-om.

Kreirajte vas Aspect:

Napravite Java projekat i za dependency dodajte:
jboss-aop-jdk50-single.jar

Definisite kada ce se pokretati vas Aspect, U sledecem primeru se pokrece pozivom bilo koje metode iz klase: rs.in.jmax.interface.JmaxInterface

jboss-aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<aop>
<bind pointcut="execution(* rs.in.jmax.interface.JmaxInterface->*(..))">
<interceptor class="jboss.aop.interceptor.MethodInterceptor" />
</bind>
</aop>


Napravite Interceptor koji ce se izvrsavati pri svakom pozivu metoda iz: rs.in.jmax.interface.JmaxInterface klase

MethodInterceptor.java

package jboss.aop.interceptor;

import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.aop.joinpoint.MethodInvocation;

public class MethodInterceptor implements Interceptor{   

public String getName() {
return "MethodInterceptor";
}   

public Object invoke(Invocation invocation) throws Throwable   {

System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
long start = System.currentTimeMillis();
MethodInvocation mi = (MethodInvocation)invocation;
System.out.println("Method: " + mi.getMethod().toString() + " startTime: " + start +"ms.");
try {
Object result = invocation.invokeNext();
return result;
}
finally {
long end = System.currentTimeMillis();
long timeMs = end - start;
System.out.println("Method: " + mi.getMethod().toString() + " end: " + end +"ms.");
System.out.println("Method: " + mi.getMethod().toString() + " took: " + timeMs +"ms.");
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
}
}


Ja sam koristio Jboss-4.0.1sp1 i  JBoss AOP 1.5.6.GA.
Kada napravite jar od vase aplikacije, izmenite .jar u .aop jer je deployer napravljen da radi na .aop