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.state
Ful.counter
You can also pass
Counter.class
get. 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, Will go wrong try {system.out.println ("Current Number:" Counter2.getNumber ());} Catch (EJBEXCEPTION EX) {IF (ex. getcausedbyexception () instanceof nosuchobjectException) {system.out.println ("I was deleted, I also found 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 context, Then via the lookup counter, then add 10 to the counter, display the current counter information. Finally call the Clean method, once this method is executed, this EJB will be removed from the container, which will have an exception in the use of this EJB.