Session Bean for Speed-based progressive learning (III.)

Source: Internet
Author: User
Tags sleep

To create a stateless session Bean

From a programmatic point of view, creating stateless session beans is as simple as creating stateful one. In addition to modifying a setting in the Configuration tool, the only difference is that in the initial design phase of the bean, stateless session beans do not remember anything between the method calls, and any messages that the bean needs must be obtained by the client. Although stateless session beans do not remember the session-oriented data, they can store data in a stateless session bean, but cannot hold data related to the client.

In the helloworldsession example, the bean still remembers a string of greetings between the method calls. For example, you call setgreeting to modify the welcome word, and when you call GetGreeting, the session remembers the welcome word that was saved.

List 6.5 "Hello World" Session Bean Remote Interface (no status version)

  Listing 6.5 Source Code for StatelessHello.java
package usingj2ee.hello;
  import java.rmi.*;
import javax.ejb.*;
  /** Defines the methods you can call on a StatelessHello object */
  public interface StatelessHello extends EJBObject
{
  /** Returns a greeting for the named object */
public String greet(String thingToGreet) throws RemoteException;
  }

In this example, the remote interface provides only a greet method that receives a parameter and returns a welcome word. For example, if the "world" argument is passed to the Greet,greet method will return "Hello world! ”。

Listing 6.6 shows the home interface for the Statelesshello bean.

  Listing 6.6 Source Code for StatelessHelloHome.java
package usingj2ee.hello;
  import java.rmi.*;
import javax.ejb.*;
  /** Defines the methods for creating a StatelessHelloWorld */
  public interface StatelessHelloHome extends EJBHome
{
  /** Creates a StatelessHello session bean. A stateless session bean
can't have a create method that takes parameters. */
public StatelessHello create() throws RemoteException, CreateException;
  }

A stateless session bean has only one create method, and the method cannot accept any parameters. This may seem strange, but you'll understand if you consider the meaning of a stateless session bean. The Bean cannot remember any information from a customer, and in fact, for performance reasons, the container may occasionally have different sessions to process a client's method call. Because session does not need to memorize a customer's information, using another bean to handle the load does not pose any problems.

If the bean's Create method accepts any arguments, the behavior between the session bean instances will vary, because you provide a different value for the Create method.

Implementing a stateless session bean is as simple as a stateful session bean. Listing 7 is the Statelesshelloimpl class, which implements the remote and home interfaces.

Listing 6.7 Source Code for Statelesshelloimpl.java
Package Usingj2ee.hello;
Import java.rmi.*;
Import java.util.*;
Import javax.ejb.*;
/** the implementation class for the Statelesshello Bean * *
public class Statelesshelloimpl implements Sessionbean
{
/** The session context provided by the EJB container. A Session Bean must
Hold on to the "context" it is given. */
Private Sessioncontext context;
/** an EJB must have a public, parameterless constructor * *
Public Statelesshelloimpl ()
{
}
/** called by the EJB container to set this session \ s context/
public void Setsessioncontext (Sessioncontext acontext)
{
context = Acontext;
}
/** called by the EJB container when a client calls the Create ()
The Home interface * *
public void Ejbcreate ()
Throws CreateException
{
}
/** called by the EJB container to wake this session bean over it
has been put to sleep with the Ejbpassivate method. */
public void Ejbactivate ()
{
}
/** called by the EJB container to tell this session bean, it is being
Suspended from use (it's being put to sleep). */
public void Ejbpassivate ()
{
}
/** called by the EJB container to the "session Bean" it has been
removed, either because the client invoked the Remove () method or the
Container has timed. */
public void Ejbremove ()
{
}
/** Returns A greeting for the named Object * *
public string greet (string thingtogreet)
{
Return "Hello" +thingtogreet+ "!"
}
}

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.