Jboss Ejb3.0 Interceptor

Source: Internet
Author: User
Tags object interface reference string window zip jboss log4j







Interceptor is an interceptor that can listen to the entire program or method of a program. It is often easier to understand problems from multiple programs, and I have added interceptor on the basis of the HelloWorld program I write myself.



There are several programs: Hello.java, Hellolocal.java, Helloremote.java, Hellobean.java, Tracinginterceptor.java



Client.java



Focus on Hellobean.java, Tracinginterceptor.java these two programs.















Hello.java



/**



* @author Roson



*



* 2005-4-10



*/



Package Org.jboss.tutorial.stateless.bean;







Public interface Hello {



public void Hello ();



Public String Hello2 ();



}















Hellolocal.java



/**



* @author Roson



*



* 2005-4-10



*/



Package Org.jboss.tutorial.stateless.bean;







Import javax.ejb.Local;







@Local



Public interface Hellolocal extends hello{







}















Helloremote.java



/**



* @author Roson



*



* 2005-4-10



*/



Package Org.jboss.tutorial.stateless.bean;







Import Javax.ejb.Remote;







@Remote



Public interface Helloremote extends hello{







}















Hellobean.java



/**



* @author Roson



*



* 2005-4-10



*/



Package Org.jboss.tutorial.stateless.bean;







Import Javax.ejb.InvocationContext;



Import javax.ejb.Stateless;



Import Javax.ejb.AroundInvoke;



Import Javax.ejb.Interceptor;







@Stateless



@Interceptor ("Org.jboss.tutorial.stateless.bean.TracingInterceptor")



public class Hellobean implements Helloremote,hellolocal {



public void Hello ()



{



System.out.println ("Hello Roson");



}







Public String Hello2 ()



{



Return to "Are you OK";



}







@AroundInvoke



Public Object Mybeaninterceptor (Invocationcontext ctx) throws Exception



{



if (Ctx.getmethod (). GetName (). Equals ("Hello"))



{



SYSTEM.OUT.PRINTLN ("* * * Hello:");



}



if (Ctx.getmethod (). GetName (). Equals ("Hello2"))



{



SYSTEM.OUT.PRINTLN ("* * * Hello2:");



}



return Ctx.proceed ();



}



}







Description



The @Interceptor ("Org.jboss.tutorial.stateless.bean.TracingInterceptor") defines a interceptor that is defined by itself.



You can also have multiple interceptor formats as follows



@Interceptors ({"Org.jboss.TracingInterceptor", "Org.jboss.") Otherinterceptor "})



The above two monitoring of the entire class situation, more to do is to monitor individual interception of each method separately, need to write a different method:



@AroundInvoke



The public Object method name can be arbitrary (Invocationcontext ctx) throws Exception



{



}















Tracinginterceptor.java



Package Org.jboss.tutorial.stateless.bean;







Import Javax.ejb.AroundInvoke;



Import Javax.ejb.InvocationContext;







public class Tracinginterceptor {







@AroundInvoke



Public Object log (Invocationcontext ctx) throws Exception



{



SYSTEM.OUT.PRINTLN ("* * * tracinginterceptor intercepting");



Long start = System.currenttimemillis ();



Try



{



return Ctx.proceed ();



}



catch (Exception e)



{



Throw e;



}



Finally



{



Long time = System.currenttimemillis ()-Start;



String method = Ctx.getbean (). GetClass (). GetName () + "." + Ctx.getmethod (). GetName () + "()";



SYSTEM.OUT.PRINTLN ("* * * tracinginterceptor Invocation of" + Method + "took" + Time + "MS");



}



}



}







This is a interceptor in the Jboss-ejb-3.0_preview_5.zip, which can be written in reference to this interceptor.



















Client.java



Package org.jboss.tutorial.stateless.client;







Import Org.jboss.tutorial.stateless.bean.Calculator;



Import Org.jboss.tutorial.stateless.bean.Hello;



Import Org.jboss.tutorial.stateless.bean.HelloRemote;



Import Javax.naming.InitialContext;







public class Client



{



public static void Main (string[] args) throws Exception



{



InitialContext CTX = new InitialContext ();



Hello hello= (Hello) ctx.lookup (HelloRemote.class.getName ());



Hello.hello ();



System.out.println (Hello.hello2 ());



}



}







In the client program incredibly no interceptor traces, hehe. The point to note here is hello, where the output of the Hello2 method is different, one in the build Ant window and the other in the JBoss run window.















Here attached log4j.properties in Jboss-ejb-3.0_preview_5.zip without this always shows a lack of appender. With this, a record.log log file will be generated in this directory.







Log4j.properties



Log4j.appender.r=org.apache.log4j.rollingfileappender



Log4j.appender.r.file=record.log



Log4j.appender.r.layout=org.apache.log4j.patternlayout



Log4j.appender.r.layout.conversionpattern=%p%d{hh:mm:ss}%t%c{1}-%m%n



Log4j.appender.r.maxbackupindex=1



log4j.appender.r.maxfilesize=100kb



Log4j.appender.stdout.layout=org.apache.log4j.patternlayout



log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n



Log4j.appender.stdout=org.apache.log4j.consoleappender



Log4j.rootlogger=stdout,r















Running: Reference installing.html



To run this program, you can combine it with the jboss-ejb-3.0_preview_5.zip, and change target run on the Build.xml file.



Under Windows



Open command Prompt cmd, to Jboss_home/bin



Run.bat–c All



With Ant



Just build and run.











Discuss:



In many places I can see interceptor, the first thing I saw in AOP. The example of the interceptor in Jboss-ejb-3.0_preview_5.zip may be to integrate some of the knowledge in the context of what has been learned before. It is not difficult to understand, here I just break it down, easier to see and master.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.