📄 resin-ejb.xtp
字号:
<document> <header> <product>resin-ee</product> <title>Resin EJB 3.0</title> <type>contents</type> <description><p>Resin's EJB support is integrated with <a href="resin-ioc.xtp">Resin IoC</a>. This integration means plain Java beans can use EJB annotations and interception, EJBs can use Resin IoC annotations, and both kinds of beans can be configured directly from the <code>resin-web.xml</code> or discovered by classpath scanning.</p> </description> </header> <body><localtoc/><s1 title="See Also"><ul> <li>For persistence, see <a href="amber.xtp">Amber</a>.</li> <li><a href="resin-ioc.xtp">Resin IoC</a> for more information on dependency injection.</li> <li>See <a href="../examples/ejb-stateless/index.xtp">session tutorial</a></li></ul></s1><s1 title="Overview"><p>Resin's EJB support is integrated with <a href="resin-ioc.xtp">ResinIoC</a>. This integration means plain Java beans can use EJB annotationsand interception, EJBs can use Resin IoC annotations, and both kinds ofbeans can be configured directly from the <code>resin-web.xml</code>or discovered by classpath scanning.</p><p>So it's best to think of EJBs as a set of orthogonal capabilities thatare available to any registered bean. The basic capability types are:</p><ul><li><b>Lifecycle model:</b> Java, <code>@Stateless</code>, <code>@Stateful</code>,or <code>@MessageDriven</code>.Resin-managed objects like Servlets and Filters are POJO model beans.</li><li><b>Dependency injection:</b> injection annotations <code>@In</code>, <code>@Named</code>, <code>@EJB</code>, <code>@PersistenceUnit</code>, etc areavailable to all beans.</li><li><b>Registration:</b> all beans are registered in a unified typed-namespaceregistry (i.e. the registration half of dependency injection.)</li><li><b>Lifecycle events:</b> the <code>@PostConstruct</code> and <code>@PreDestroy</code></li><li><b>Predefined aspects:</b> the <code>@TransactionAttribute</code>, <code>@RunAs</code>, <code>@RolesAllowed</code>, etc. annotations areavailable to all beans.</li><li><b>Custom interceptors:</b> EJB-style <code>@AroundInvoke</code>, and <code>@Interceptors</code>, as wellas WebBeans-style <code>@Interceptor</code> and <code>@InterceptorBindingType</code> are available to all beans.</li><li><b>Event handling:</b> the WebBeans <code>@Observes</code> capability is available to all beans.</li></ul></s1><s1 title="Hello, World"><p>The Hello, World example is the smallest example deploying and usinga <code>@Stateless</code> session bean. The bean is configured inthe <code>resin-web.xml</code> and used in <code>MyServlet</code> withthe WebBeans <code>@In</code> providing dependency injection.</p><example title="Example: Bean.java">package demo;public interface Bean { public String hello();}</example><example title="Example: @Stateless MyBean.java">package demo;import javax.ejb.*;@Statelesspublic class MyBean implements Bean { public String hello() { return "hello, world"; }}</example><example title="Example: MyServlet.java">package demo;import java.io.*;import javax.servlet.*;import javax.webbeans.*;public MyServlet extends GenericServlet { @In private Bean _bean; public void service(ServletRequest, ServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println(_bean.hello()); }}</example><example title="Example: WEB-INF/resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="demo.MyBean"/> <servlet-mapping url-pattern="/test" servlet-class="demo.MyServlet"/></web-app></example></s1><s1 title="EJB Models"><p>Resin's EJB integration provides four lifecycle choices: plain Java,<code>@Stateless</code>, <code>@Stateful</code>, and <code>@MessageDriven</code>.</p><ul><li>Java: multithreaded, single instance, with no special method calling code by default, e.g. Servlets, Filters or <code>@Component</code> instances.</li><li><code>@Stateless</code>: pooled instances, each is single-threaded,REQUIRED transactions and classloader environment enabled by default.</li><li><code>@Stateful</code>: enforced single-threaded instances.REQUIRED transactions and classloader environment enabled by default.Only <code>@Stateful</code> beans can implement the<code>SessionSynchronization</code> interface.</li><li><code>@MessageDriven</code>: pool instances, each single-threaded,specifically tailored for JMS/Messaging tasks(See <a href="resin-messaging">Resin messaging</a>.) REQUIRED transactionsand classloader environment enabled by default.</li></ul><s2 title="Java"><p>A Java bean is a multithreaded instance, e.g. a servlet or filter.Because it's multithreaded, application developers must take extra careto handle their own synchronization. In particular, threads mustnot store thread-specific data in Java bean fields and should generallyavoid using static fields.</p><p>Java beans can use all the EJB and<a href="resin-ioc.xtp">Resin-IoC</a> aspects, including<code>@TransactionAttribute</code>, can use EJB <code>@Interceptor</code>classes, and Resin-Ioc <code>@InterceptorBinding</code>. They can handleevents with the <code>@Observes</code> annotation, and add lifecyclecallbacks with <code>@PostConstruct</code> and <code>@PreDestroy</code>.</p></s2><s2 title="@Local"><p>The <code>@Local</code> annotation marks an interface of a bean as theexposed proxy API. Since <code>@Stateless</code> and <code>@Stateful</code>beans are proxy-based, they need an interface to expose to users of thebean. If the bean implements exactly in interface, it's chosen by default,otherwise the developer needs to mark the interface to export.</p><deftable title="Local attributes"><tr> <th>Field</th> <th>Meaning</th> <th>Default</th></tr><tr> <td>value</td> <td>Lists the interfaces to export for the bean.</td> <td></td></tr></deftable><def title="javax.ejb.Local">@Target(TYPE)@Retention(RUNTIME)public @interface Local { Class []value() default {};}</def></s2><s2 title="@MessageDriven"><p>MessageDriven beans receive messages from a Queue, using a pooled,single-threaded model like @Stateless beans.</p><p>See <a href="resin-messaging.xtp">Resin messaging</a> for more information.</p><p>The MessageDriven bean connects with the messaging provider throughan <a href="http://wiki.caucho.com/ActivationSpec">ActivationSpec</a>.For JMS, the activation spec specifies the JMS Queue.</p><deftable title="MessageDriven attributes"><tr> <th>Field</th> <th>Meaning</th> <th>Default</th></tr><tr> <td>activationConfig</td> <td>Default properties for the ActivationSpec, e.g. theJMS Queue name or destination type.</td> <td></td></tr><tr> <td>description</td> <td>An optional description of the bean, typically for the benefit of administration</td> <td></td></tr><tr> <td>mappedName</td> <td>An optional provider-specific name. Resin doesn't use this</td> <td></td></tr><tr> <td>name</td> <td>The EJB's name</td> <td>The classname of the bean</td></tr><tr> <td>messageListenerInterface</td> <td>Defines the interface to be used to receive messages. The interfacewill match the messaging-provider's API, e.g. JMSuses <code>javax.jms.MessageListener</code>.</td> <td></td></tr></deftable><def title="javax.ejb.MessageDriven">@Target(TYPE)@Retention(RUNTIME)public @interface Stateful { String name() default ""; Class messageListenerInterface() default Object.class; ActivationConfigProperty []activationConfig() default {}; String mappedName() default ""; String description() default "";}</def></s2><s2 title="@Remote"><p>The <code>@Local</code> annotation marks an interface of a bean as theexposed proxy API for remoting. See <a href="resin-remoting.xtp">Resinremoting</a> for more information on exposing beans as remote objectsfor protocols such as Hessian, Burlap, and SOAP.</p><deftable title="Remote attributes"><tr> <th>Field</th> <th>Meaning</th> <th>Default</th></tr><tr> <td>value</td> <td>Lists the interfaces to export for the bean.</td> <td></td></tr></deftable><def title="javax.ejb.Remote">@Target(TYPE)@Retention(RUNTIME)public @interface Remote { Class []value() default {};}</def></s2><s2 title="SessionSynchronization"><p>The <code>SessionSynchronization</code> interfaces tells Resin toenlist a <code>@Stateful</code> session bean in the current transaction.Before the transaction starts its commit processing, the bean will receivea callback message, allowing it to coordinate any data persistence. Whenthe transaction either commits or rollsback, the bean will receive anothercallback indicating success or failure, allowing the bean to completeany transaction processing.</p><p>Only <code>@Stateful</code> session beans canimplement <code>SessionSynchronization</code> because the bean itself willbe registered with the transaction. The unique capability makesstateful session beans valuable for transaction processing.</p><deftable title="SessionSynchronization methods"><tr> <th>Method</th> <th>Description</th></tr><tr> <td>afterBegin</td> <td>Called when the transaction starts</td></tr><tr> <td>beforeCompletion</td> <td>The transaction will call <code>beforeCompletion</code> before itstarts the two-phase commit, allowing the bean to flush any cached dataif necessary.</td></tr><tr> <td>afterCompletion</td> <td>The transaction will call <code>afterCompletion</code>when the transaction completes, reporting whether the transactioncompleted with a successful commit or a rollback.</td></tr></deftable><def title="javax.ejb.SessionSynchronization">public interface SessionSynchronization { public void afterBegin() throws EJBException, RemoteException; public void beforeCompletion() throws EJBException, RemoteException; public void afterCompletion(boolean committed) throws EJBException, RemoteException;}</def></s2><s2 title="@Stateful"><p>A stateful session bean is a single-threaded instance. Applicationswill need to create a new stateful instance for each thread or request.</p><deftable title="Stateful attributes"><tr> <th>Field</th> <th>Meaning</th> <th>Default</th></tr><tr> <td>description</td> <td>An optional description of the bean, typically for the benefit of administration</td> <td></td></tr><tr> <td>mappedName</td> <td>An optional provider-specific name. Resin doesn't use this</td> <td></td></tr><tr> <td>name</td> <td>The EJB's name used for jndi and Resin IoC registration</td> <td>The classname of the bean</td></tr></deftable><def title="javax.ejb.Stateful">@Target(TYPE)@Retention(RUNTIME)public @interface Stateful { String name() default ""; String mappedName() default ""; String description() default "";}</def></s2><s2 title="@Stateless"><p>A stateless session bean is a pooled proxy, where each request getsits own bean instance.</p><deftable title="Stateless attributes"><tr> <th>Field</th> <th>Meaning</th> <th>Default</th></tr><tr> <td>description</td> <td>An optional description of the bean, typically for the benefit of administration</td> <td></td></tr><tr> <td>mappedName</td> <td>An optional provider-specific name. Resin doesn't use this</td> <td></td></tr><tr> <td>name</td> <td>The EJB's name used for jndi and Resin IoC registration</td> <td>The classname of the bean</td></tr></deftable><def title="javax.ejb.Stateless">@Target(TYPE)@Retention(RUNTIME)public @interface Stateless { String name() default ""; String mappedName() default ""; String description() default "";}</def></s2></s1><s1 title="Aspect Annotations"><s2 title="@AroundInvoke"><p><code>@AroundInvoke</code> marks an interception method on thebean or an interceptor class. The interceptor is invoked whileprocessing a business method.</p><def title="javax.interceptor.AroundInvoke">@Target(METHOD)@Retention(RUNTIME)public @interface AroundInvoke {}</def><example title="Example: @AroundInvoke method">import javax.interceptor.*;public class MyBean { @AroundInvoke protected Object log(InvocationContext cxt) throws Exception { System.out.println("Before: " + cxt.getMethod()); Object value = cxt.proceed(); System.out.println("After: " + cxt.getMethod()); return value; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -