| ||||||||||||||||||||||||
Resin 3.1 Documentation Examples Changes Quercus Database Amber EJB SOA/ESB IoC JMS Servlet JMX Hessian Security Stateless |
Stateless sessions make database queries and updates robust by setting transaction boundaries at each business method. This bean example annotates a single business method with a SUPPORTS transaction attribute, marking the method as a read-only transaction boundary.See also:
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:
To configure Resin to be a server for the EJB you need to:
In this tutorial, a simple "Hello" EJB is created and deployed within Resin. Files in this tutorial
Local InterfaceThe remote interface defines the client view of the bean.
It declares all the business methods. Our
only business method is the package example; public interface Hello { public String hello(); } Bean ImplementationThe second class for EJBs is the bean implementation class. It implements the functionality provided by the remote interface. package example; import static javax.ejb.TransactionAttributeType.SUPPORTS; @javax.ejb.Stateless public class HelloBean implements Hello { private String _greeting = "Default Hello"; @javax.ejb.Resource public void setGreeting(String greeting) { _greeting = greeting; } @javax.ejb.TransactionAttribute(SUPPORTS) public String hello() { return _greeting; } } @StatelessThe @Stateless annotation marks the bean as a stateless session
bean. Resin will create a stub implementing The @Stateless annotation can have an optional @InjectThe @javax.ejb.Resource 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 InjectionThe 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> @TransactionAttributeManaging transactions is the primary purpose of stateless
session beans. Transactions are a more powerful version of
a @javax.ejb.TransactionAttribute(SUPPORTS) public String hello() The The REQUIRED transaction value starts up a new transaction if none already exists. It's used when updating database values.
Configuring the Resin EJB server
<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. Clientpublic 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()); } } @EJBThe @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
<servlet servlet-name="hello" servlet-class="example.HelloServlet"> <init hello="\${jndi('java:comp/env/ejb/HelloBean')}"/> </servlet>
|