esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I get started with an EJB stateless session bean in JBoss?
A stateless session bean, the simplest of all EJB's, is a lightweigt enterprise bean that does not maintain client state over method invocations. After the method invocation is finished, the state is no longer retained. EJB containers pool stateless session beans and can assign any instance of such a bean to a client. Contrary to Stateful session beans (that retains state during the client-bean session), a stateless session bean is never written to secondary storage, and therefor offers better performance.
You should use it for business processes that span only one method call and state is not to be retained for a specific client.
For example:
- validate credit card data
- facade for multiple entity beans, eg. simplified access to complex entity beans
- ...

The following example shows the necessary steps to write an HelloWorld stateless session bean and deploy it under JBoss. All of the packaging and deploying will be done using command-line tools. At the end, an Ant build script is provided that will make these labour tasks easier.

  1. Download and install JBoss

    This example is written and tested with JBoss 3.0.4. Download and run it!

  2. Write the remote interface

    The remote interface is the view the client has on the bean. It contains the business methods that a client may invoke. It must extend EJBObject and its methods should follow the RMI rules (throw java.rmi.RemoteException and have all parameters as well as the return values be Serializable objects). The remote interface is implemented by the container.

    When the client invokes a method on the remote interface, the call gets proxied to the actual bean class. You will notice later on that the bean class does not implement this interface. However, the methods of your bean class must match the signature of the methods defined in your remote interface.

    com/esus/ejb/helloworld/HelloWorld.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


  3. Write the home interface

    The home interface is used by the EJB container to create your session bean and contains a single create method and no arguments (because it cannot contain state). This interface will be implemented by the EJB container. The create method should return the remote interface of the bean.

    The Home interface must inherit from EJBHome, follow the RMI rules as well and provide a single create() method with no arguments. Its create() method returns the remote interface.

    com/esus/ejb/helloworld/HelloWorldHome.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


    When the client calls the create method on the Home interface, it will get back the remote interface and has everything it needs to invoke business methods.

  4. Write the stateless sessionbean

    The business logic sits in the actual Bean. This class must implement all methods listed in the remote interface (but not implement the remote interface itself!). It must implement the interface javax.ejb.SessionBean that contains a number of callback methods:

    • ejbRemove(): called by the container when it decides to delete a session bean instance.
    • ejbActivate(): Used for swapping beans, not applicable for stateless session beans
    • ejbPassivate(): Used for swapping beans, not applicable for stateless session beans
    • setSessionContext(SessionContext ctx): This method is called once by the container, when the bean is initialized, right before ejbCreate(). It provides access to the container-provided runtime context (eg. the environment properties)

    In addition, a stateless session bean is required to contain a no-argument ejbCreate() method that returns void. The ejbCreate() method acts as the contructor for the session bean and is called by the Container when the create() method on the Home interface is intercepted.

    com/esus/ejb/helloworld/HelloWorldBean.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


  5. Create the deployment descriptor ejb-jar.xml

    With a deployment descriptor, you can declaratively specify attributes on the bean. The following attributes are specified in the ejb-jar.xml for our HelloWorld bean:

    • ejb-name: a symbolic name for the bean, used as a reference in other parts of the deployment descriptor
    • home: the fully qualified name of the home interface class
    • remote: the fully qualified name of the remote interface class
    • ejb-class: the fully qualified name of the bean class
    • session-type: the session bean type, stateful or stateless
    • transaction-type: Bean or Container (bean or container managed transactions)


    META-INF/ejb-jar.xml:

    This code sample is only viewable to esus.com members
    Login or become a member!


  6. Compile

    Our directory structure now looks like this:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


    Make sure you put jboss-j2ee.jar in your classpath:

     
    This code sample is only viewable to esus.com members
    Login or become a member!



  7. Package your bean into a JAR file:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  8. Deploy your EJB

    To deploy HelloWorld.jar in JBoss, simply copy it to the JBoss deployment directory:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


    Your JBoss console will display:

     
    This code sample is only viewable to esus.com members
    Login or become a member!



  9. Write the client

    The client uses JNDI to locate the home object. Using the home object, it can create an instance that implements the HelloWorld remote interface. Using this interface, the bean's business methods can be invoked.
    About PortableRemoteObject.narrow: The JNDI lookup method returns an object that needs to be cast to the appropriate home interface type. This cast needs to be done using javax.rmi.PortableRemoteObject.narrow which is passed the object you desire to cast and the class to which you want to cast it. PortableRemoteObject performs a safe cast, that is, it ensures interoperability with EJB compliant implementations.

    client\HelloWorldClient.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


  10. Generate a client jar:

    Before being able to compile and run your client, it must have access to the EJB's home and remote interface classes via the classpath. You can have this done automatically by an IDE, or by an ant task. For now, let's do it manually:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  11. Compile the client

    Set your classpath and compile:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  12. Run the client

     
    This code sample is only viewable to esus.com members
    Login or become a member!



    Ant can be used to make the compile/deployment tasks easier. Place the following build.xml file in your current directory.
    build.xml:

    This code sample is only viewable to esus.com members
    Login or become a member!


    Your directory structure now looks like this:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


    To use ant, install it and add it to your path. Then, implicitly using the build.xml build file, you can execute the following commands:

     
    This code sample is only viewable to esus.com members
    Login or become a member!




    Further Information
    Author of answer: Joris Van den Bogaert

    Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.