Local Stateless Session Hello
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Bean Annotation
Table
Tutorials

Stateless Session
Tutorials
Tutorials
Security

Find this tutorial in: /usr/local/resin/webapps/resin-doc/ejb3/tutorial/stateless
Try the Tutorial

Stateless sessions make database queries and updates robust by setting transaction boundaries at each business method. This stateless session bean example annotates a single business method with a SUPPORTS transaction attribute, marking the method as a read-only transaction boundary.

See also:

  1. Files in this tutorial
  2. Local Interface
  3. Bean Implementation
    1. @Stateless
    2. @Inject
    3. Alternate Dependency Injection
    4. @TransactionAttribute
  4. Configuring the Resin EJB server
  5. Client
    1. @EJB

A Hello, World example for EJB 3.0 is much simpler than for earlier versions of EJB. To implement the EJB you need to implement:

  • A local interface
  • The bean implementation

To configure Resin to be a server for the EJB you need to:

  • Configure the ejb-server
  • Configure the client

In this tutorial, a simple "Hello" EJB is created and deployed within Resin.

Files in this tutorial

WEB-INF/web.xml web.xml configuration
WEB-INF/classes/example/Hello.java The local interface for the stateless session bean
WEB-INF/classes/example/HelloBean.java The implementation for the stateless session bean
WEB-INF/classes/example/HelloServlet.java The client for the stateless session bean

Local Interface

The remote interface defines the client view of the bean. It declares all the business methods. Our only business method is the hello method.

Hello.java
See it in: WEB-INF/classes/example/Hello.java
package example;

public interface Hello {
  public String hello();
}

Bean Implementation

The second class for EJBs is the bean implementation class. It implements the functionality provided by the remote interface.

HelloBean.java
See it in: WEB-INF/classes/example/HelloBean.java
package example;

import static javax.ejb.TransactionAttributeType.SUPPORTS;

@javax.ejb.Stateless
public class HelloBean implements Hello {
  private String _greeting = "Default Hello";

  @javax.ejb.Inject 
  public void setGreeting(String greeting)
  {
    _greeting = greeting;
  }

  @javax.ejb.TransactionAttribute(SUPPORTS)
  public String hello()
  {
    return _greeting;
  }
}

@Stateless

The @Stateless annotation marks the bean as a stateless session bean. Resin will create a stub implementing Hello and store it in JNDI at "java:comp/env/ejb/HelloBean".

The @Stateless annotation can have an optional name value which overrides the default name of "HelloBean".

@Inject

The @javax.ejb.Inject annotation tells Resin to lookup the greeting from JNDI when the session bean is created. The JNDI name will be java:comp/env/greeting.

In this example, the greeting is configured with an <env-entry> in the web.xml.

Alternate Dependency Injection

The EJB 3.0 draft spec's dependency injection is somewhat inflexible since the greeting is required to be in JNDI. Resin offers a more flexible dependency injection configuration based on the configuration file. By setting the value in the configuration file, Resin's alternate dependency injection adds more flexibility and some clarity.

<ejb-server jndi-name="java:comp/env/ejb">
  <bean type="qa.TestBean">
    <init greeting="Hello, World from web.xml"/>
  </bean>
</ejb-server>

@TransactionAttribute

Managing transactions is the primary purpose of stateless session beans. Transactions are a more powerful version of a synchronized lock used to protect database integrity. @TransactionAttribute marks the transaction boundary for each business method.

@javax.ejb.TransactionAttribute(SUPPORTS)
public String hello()

The hello() business method uses SUPPORTS because it's a read-only method. It doesn't need to start a new transaction on its own, but will participate in any transaction that already exists.

The REQUIRED transaction value starts up a new transaction if none already exists. It's used when updating database values.

TransactionAttributemeaning
REQUIREDStart a new transaction if necessary
SUPPORTSDon't start a new transaction, but use one if it exists

Configuring the Resin EJB server

<ejb-server> configure the Resin EJB server. Typically it configures the EJB root using jndi-name and configures a number of EJB classes using <bean>. The <bean> entry will look at the bean's annotations to enhance the class.

ejb-server in web.xml
See it in: WEB-INF/web.xml
<web-app xmlns="http://caucho.com/ns/resin">

  ...
  <env-entry env-entry-name="greeting"
             env-entry-type="java.lang.String"
             env-entry-value="Hello, World."/>

  <ejb-server jndi-name="java:comp/env/ejb">
    <bean type="qa.TestBean"/>
  </ejb-server>
    
  ...

</web-app>

The <bean> can optionally configure the bean instances with an <init> tag as described in the alternate dependency injection section.

Client

See it in: WEB-INF/classes/example/HelloServlet.java
public class HelloServlet extends GenericServlet {
  private Hello _hello;

  @javax.ejb.EJB
  public void setHello(Hello hello)
  {
    _hello = hello;
  }

  public void service(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException
  {
    PrintWriter out = res.getWriter();
    
    out.println(_hello.hello());
  }
}

@EJB

The @EJB annotation tells Resin to lookup the session bean in JNDI with name "java:comp/env/ejb/HelloBean".

The servlet could also lookup the Hello bean with JNDI in the init() method or use an <init> configuration in the web.xml:

alternative configuration
<servlet servlet-name="hello" servlet-class="example.HelloServlet">
  <init hello="${jndi:lookup('java:comp/env/ejb/HelloBean')}"/>
</servlet>

Try the Tutorial


Tutorials
Tutorials
Security
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.