EJB3.0 Development Guide: State Session Bean

xiaoxiao2021-03-06  45

Like stateless session beans, a stateful session bean must have a business interface, which is implemented by session beans, or can be generated by session beans. In this way, you can only write a file, you can generate business logic implementation classes, remote interfaces, local interfaces, etc.

In the current JBoss implementation, there must be a separate business interface.

This interface does not have to implement EJBOBJECT or EJBLOCALOBJECT.

A stateful session bean must use the Statelfull annotation to indicate that it is a stateful session bean. The EJB container will determine its type according to this annotation. Or implement a javax.ejb.sessionBean interface.

A stateful session bean can implement the setSessionContext method or not.

The ejbobject.remove method is called in EJB2.0 to reach the function of deleting a status session bean from the container. In eJB3.0, simply add REMOVE comments in some ways. Once these marked methods are called, this EJB will be removed from the container.

A stateful session bean can rely into the resource and environmental properties of the container.

In Eclipse, the examples provided herein are STATEFUL.

This example is similar to the example of the stateless session bean, and only different code is listed below.

This example has main five files:

Counter.java: Business Interface.

CounterBean.java: Business implementation class. The EJB we develop in the future is also named (adding beans on the interface name).

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Here is a description of the content of each file.

Counter.java

......

Import javax.ejb.remove;

......

@Remote

Public Interface Counter

{

......

@Remove

Public void clean ();

}

This interface is very simple, and the stateless session bean is basically the same, but the new Clean method is added and the Remove comment is added to this method. Once this method is executed, this bean will be removed from the container.

JBoss defaults to use the full name of the interface as its JNDI name. In the example above, its full name is:

com.kuaff.ejb3.Stateful.counter, you can also get it through counter.class.

Counterbean.java

......

Import javax.ejb.stateful;

......

@Stateful

Public Class CounterBean Implements Counter

{

// Increase transaction support

@TX (TXTYPE.REQUIRESNEW)

Public int getNumber ()

{

Return Number;

}

@Remove

Public void clean ()

{

System.out.println ("I, deleted!");

}

}

This is the implementation class of the counter. Note that this class uses Stateful to make a comment, which is necessary. At the same time, this example also demonstrates how to use transactions. Detailed usage of the transaction will be introduced in the following chapters.

Client.java

Package com.kuaff.ejb3.stateful;

Import javax.ejb.ejbexception;

Import java.rmi.nosuchobjectException;

Import javax.naming.initialcontext; import javax.naming.namingexception;

Public Class Client

{

Public static void main (string [] args)

{

InitialContext CTX;

Try

{

CTX = New InitialContext ();

Counter counter = (counter) ctx.lookup (counter.class.getname ());

Counter.add (10);

System.out.println ("Current Number:" Counter.getNumber ());

Counter.add (10);

System.out.println ("Current Number:" Counter.getNumber ());

Counter Counter2 = (counter) ctx.lookup (counter.class.getname ());

Counter2.add (10);

System.out.println ("Current Number:" Counter2.GetNumber ());

//delete

Counter2.clean ();

// If you use Counter2, you will be wrong.

Try

{

System.out.println ("Current Number:" Counter2.GetNumber ());

}

Catch (EJBEXCEPTION EX)

{

IF (ex. getcausedbyexception () instanceof nosuchobjectException)

{

System.out.println ("I have been deleted, I also look for me!");

}

Else

{

Throw EX;

}

}

}

Catch (Namingexception E)

{

E.PrintStackTrace ();

}

}

}

This class is used to test the counter EJB we publish. First pass

CTX = new initialContext (); get the context, then use the Lookup lookup counter, then add 10 to the counter, display the current counter information. Finally, the Clean method is called. Once this method is executed, this EJB will be removed from the container, and an exception will appear in using this EJB.

Please run the run.bat: Run? C all under the directory of {$ jboss_home} / bin, start JBoss.

Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.

Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.

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

New Post(0)