Stateless session bean (1)---definition

Source: Internet
Author: User

A stateless session Bean is used to complete the operation over the life cycle of a single method. Stateless beans can do a lot of business, but every method cannot assume that any other method will be called before it. The second half of the sentence means that you may not be who you are right now. Tomorrow you can not be today's you, this time you are called "stateless you."

The above description sounds like a limitation of a stateless bean , as if every time goodbye is like a farewell.

But this is by far the most common form of business services, like to go to a restaurant to eat, a la carte waiter and to serve you the waiter is not the same this is not our concern.

A stateless reply bean differs from a stateful session Beanthat is suitable for accumulating state in a conversation (such as a shopping cart for a retail application). The stateless reply Bean is designed to run independent operations efficiently. The stateless session Bean is designed to take into account the resource usage of the server when it cannot tolerate a large number of clients.

The session Bean definition is divided into two parts, such as the following:

l 0 or more business interfaces. Defines a method that a client can invoke on a bean .

When an interface is not defined, the public method collection of theBean 's implementation class forms a logical client interface.

l classes that implement these interfaces, called Bean classes, are tagged with @Stateless annotations.

Most session Beans have a business interface, but there is no limit to the number of interfaces that a session Bean can expose to its client. When the server encounters @Stateless annotations, it knows to treat the Bean as a session Bean.

It configures the Beanin the EJB container so that other components of the application can use it.

A business interface is included in the Hello World Example of this EJB version number below. A single method is included in this demo sample. There is no annotation or parent interface to indicate that this is a business interface. The interface with the generic class implementation looks identical. When the session Bean implements it, it will take its own initiative to treat it as a local business interface, meaning that only the client on the same application server can access it.

To emphasize that an interface is a local business interface, you can choose to add @Local annotations to the interface.

Public interface HelloService {public    string SayHello (string name);}

Now consider the implementation of the interface. The following code shows a general Java class that implements the above interface. The only special thing about this class is @Stateless annotation, which marks it as a stateless session Bean.

There is no special restriction or requirement on the implementation of a business method, it happens to be a regular class of EJBs .

@Statelesspublic class Helloservicebean implements HelloService {public    string SayHello (String name) {        return ' Hello, "  + name;    }}

About No interface view

Introducing a no-interface view (No-interface-view) in EJB3.1 , which enables local session beans to not implement interfaces. This makes it easier to define a local session Bean and to access the local session Bean . As the following code sees the use of a no-interface view to define the same Helloservicebeanin the above code, the developer simply needs to create the implementation class, without having to implement whatever business interface:

@Statelesspublic class Helloservicebean {public    string SayHello (string name) {        return "Hello,"  + name;< c11/>}}

The logical interface of a session Bean includes its public methods, in this case the SayHello() method. The client uses the Helloservicebean class as an interface, regardless of the nonpublic method or specific information that is implemented.

It is conceivable that the client is provided with a proxy interface within the server that overrides the business method to provide a standard container service, and the client interacts with the agent.

One of the strengths of the no-interface view is simplicity.

It does not need to implement a redundant business interface and further makes the EJB look like a regular JavaBean class.

However, because the No interface view applies only to the local reply bean. In this example, a separate interface with a traditional style is used to ensure that stateless and stateful session beans are consistent.

The definition of a stateless session Bean class requires only two points to be noted. The first is that it requires a non-parameter constructor, but when there are no other constructors. The compiler typically builds this constructor itself. The second is that static fields should not be used, mainly due to another deployment of the Bean .

Many EJB containers Create an instance pool of a stateless session Bean . Then select an arbitrary instance to serve each client request. Since this does not guarantee that the same state will be used between calls, it is not possible to feel that the waiter who ordered the order is the same person as the waiter serving you.

Stateless session bean (1)---definition

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.