![]() | ![]() | ![]() | ||||||||||||||||||||||||
![]() | ||||||||||||||||||||||||||
documentation examples changes amber (jpa) ejb database ioc jmx jsf messaging quercus remoting servlet security stateless | local stateless session hello
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.
The remote interface defines the client view of the bean.
It declares all the business methods. Our
only business method is the Hello.java package example; public interface Hello { public String hello(); } The second class for EJBs is the bean implementation class. It implements the functionality provided by the remote interface. HelloBean.java package example; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import static javax.ejb.TransactionAttributeType.SUPPORTS; import javax.webbeans.Named; @Stateless public class HelloBean implements Hello { @Named("greeting") private String _greeting; @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 @NamedThe @javax.webbeans.Named
annotation tells Resin to lookup the greeting In this example, the greeting is configured with an <env-entry> in the web.xml. Alternate Dependency InjectionIn some cases, it may be clearer to configure the session bean directly, rather than using WebBeans injection. Instead of creating a separate <env-entry>, you can configure the greeting value using XML straight from the resin-web.xml file. resin-web.xml <web-app xmlns="http://caucho.com/ns/resin"> <ejb-stateless-bean class="qa.TestBean"> <init> <greeting>Hello, World from web.xml</greeting> </init> </ejb-stateless-bean> </web-app> @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.
ejb-stateless-bean in 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-stateless-bean class="qa.TestBean"/> </web-app> The <ejb-stateless-bean> can optionally configure the bean directly with an <init> tag as described in the alternate dependency injection section. HelloServlet.java import javax.webbeans.In; public class HelloServlet extends GenericServlet { @In private Hello _hello; public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { PrintWriter out = res.getWriter(); out.println(_hello.hello()); } } @EJBThe @In annotation tells
Resin to look for a The servlet could also lookup the Hello bean with JNDI in the
alternative configuration <servlet servlet-name="hello" servlet-class="example.HelloServlet"> <init hello="${HelloBean}"/> </servlet>
|