JBoss EJB3.0 Interceptor

xiaoxiao2021-03-05  25

Interceptor is an interceptor that can listen to the entire program or method of the program. From multiple programs, the problem is often easier, I added Interceptor on the basis of the HelloWorld program written.

There are several procedures: hello.java, hellolocal.java, helloremote.java, hellobean.java, TracingInterceptor.java

Client.java

Focus on the two programs of Hellobean.java, TracingInterceptor.java.

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.tracing Interceptor")

Public class hellobean imports helloremote, hellolocal {

Public void Hello ()

{

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

}

Public string hello2 ()

{

Return "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:

@Interceptor ("Org.jboss.Tutorial.Stateless.Bean.tracingInterceptor" defines a defined interceptor. You can also have multiple interceptor formats as follows

@Interceptors ({"org.jboss.tracinginterceptor", "org.jboss..otherinterceptor"})

The above two cases of monitoring the entire class, the more point to do is to monitor the interception of each method separately, need to write another way:

@AroundInvoke

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 traacinginterceptor {

@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 an interceptor in jboss-ejb-3.0_preview_5.zip, you can refer 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, there is no trace of interceptor in the client program, huh, huh. Here, it is necessary to indicate that Hello, Hello2 method is different, one is in the window of the build Ant, and the other is running the window in the JBoss. There is no log4j.properties in jboss-ejb-3.0_preview_5.zip, there is no such thing as the lack of appender. With this will generate a replard.log log file 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

Run: Refer to Installing.html

To run this program, you can combine it with jboss-ejb-3.0_preview_5.zip, change the Target Run in the build.xml file.

Under Windows

Open the command prompt cmd to jboss_home / bin

Run.bat -c all

Use ANT

After Build, run.

discuss:

In many places, I can see Interceptor's figure, I first saw it in AOP. The example of interceptor inside jboss-ejb-3.0_preview_5.zip may be in order to combine the previously learned, and some knowledge is integrated. It is not very difficult to understand. Here I only decompose it, it is easier to look and master.

转载请注明原文地址:https://www.9cbs.com/read-35650.html

New Post(0)