Saturday, July 25, 2009

EJB3 Interceptor class example on Weblogic server

Annotations Used to Configure Interceptors
http://edocs.bea.com/wls/docs103/ejb30/annotations.html#wp1428539


Programming the Annotated EJB 3.0 Class
http:
//edocs.bea.com/wls/docs103/ejb30/program.html#wp1207312


Oracle Weblogic example
@Stateful
@TransactionAttribute(REQUIRES_NEW)
@Remote({sessionbean.Account.class})
@Interceptors({sessionbean.AuditInterceptor.class})
public class AccountBean
implements Account
{

private int balance = 0;

public void deposit(int amount) {
balance += amount;
System.out.println("deposited: "+amount+" balance: "+balance);
}

public void withdraw(int amount) {
balance -= amount;
System.out.println("withdrew: "+amount+" balance: "+balance);
}

public int getBalance(){
int bogus;
return balance + 1;
}

@ExcludeClassInterceptors
public void sayHelloFromAccountBean() {

}


@PreDestroy
public void preDestroy() {
System.out.println("Invoking method: preDestroy()");
}

}


Interceptor class
package sessionbean;



import javax.interceptor.AroundInvoke;

import javax.interceptor.InvocationContext;



import javax.ejb.PostActivate;

import javax.ejb.PrePassivate;



/**

* Interceptor class. The interceptor method is annotated with the

* @AroundInvoke annotation.

*/



public class AuditInterceptor {



public AuditInterceptor() {}



@AroundInvoke

public Object audit(InvocationContext ic) throws Exception {

System.out.println("Invoking method: "+ic.getMethod());

return ic.proceed();

}



@PostActivate

public void postActivate(InvocationContext ic) {

System.out.println("Invoking method: "+ic.getMethod());

}



@PrePassivate

public void prePassivate(InvocationContext ic) {

System.out.println("Invoking method: "+ic.getMethod());

}



}




Inject EJB into servlet
@EJB
Account remoteAccount;




Invoking method: public java.lang.Object sessionbean.AccountBean_2lqacg_Impl.beaInvoke(java.lang.Object[],int)
deposited: 200 balance: 800

No comments:

Post a Comment