2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java What Is a Message-Driven Bean? What Makes Message-Driven Beans Different from Session Beans? When to Use Message-Driven Beans The Contents of an Enterprise Bean Naming Conventions for Enterprise Beans The Life Cycles of Enterprise Beans The Life Cycle of a Stateful Session Bean The Life Cycle of a Stateless Session Bean The Life Cycle of a Message-Driven Bean Further Information about Enterprise Beans 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Defining Client Access with InterfacesThe material in this section applies only to session beans and notto message-driven beans. Because they have a different programming model, message-driven beans do nothave interfaces that define client access. A client can access a session bean only through the methods defined inthe bean’s business interface. The business interface defines the client’s view of abean. All other aspects of the bean (method implementations and deployment settings) arehidden from the client. Well-designed interfaces simplify the development and maintenance of Java EE applications. Not onlydo clean interfaces shield the clients from any complexities in the EJB tier,but they also allow the beans to change internally without affecting the clients.For example, if you change a session bean from a stateless to astateful session bean, you won’t have to alter the client code. But ifyou were to change the method definitions in the interfaces, then you mighthave to modify the client code as well. Therefore, it is important thatyou design the interfaces carefully to isolate your clients from possible changes inthe beans. Session beans can have more than one business interface. Session beans should, butare not required to, implement their business interface or interfaces. When you design a Java EE application, one of the first decisionsyou make is the type of client access allowed by the enterprise beans:remote, local, or web service. Remote ClientsA remote client of an enterprise bean has the following traits:
To create an enterprise bean that allows remote access, you must do oneof the following:
Theremote interface defines the business and life cycle methods that are specific tothe bean. For example, the remote interface of a bean namedBankAccountBeanmight have business methods nameddeposit andcredit.Figure 20-1 shows how the interfacecontrols the client’s view of an enterprise bean. Figure 20-1 Interfaces for an Enterprise Bean with Remote Access ![]() Local ClientsA local client has these characteristics:
Thelocal business interface defines the bean’s business and life cycle methods. If the bean’sbusiness interface is not decorated with@Local or@Remote, and the bean classdoes not specify the interface using@Local or@Remote, the business interface isby default a local interface. To build an enterprise bean that allowsonly local access, you may, but are not required to do one ofthe following:
Deciding on Remote or Local AccessWhether to allow local or remote access depends on the following factors.
If you aren’t sure which type of access an enterprise bean should have,choose remote access. This decision gives you more flexibility. In the future youcan distribute your components to accommodate the growing demands on your application. Although it is uncommon, it is possible for an enterprise bean to allowboth remote and local access. If this is the case, either thebusiness interface of the bean must be explicitly designated as a business interface bybeing decorated with the@Remote or@Local annotations, or the bean class mustexplicitly designate the business interfaces by using the@Remote and@Local annotations. The samebusiness interface cannot be both a local and remote business interface. Web Service ClientsA web service client can access a Java EE application in twoways. First, the client can access a web service created with JAX-WS. (Formore information on JAX-WS, seeChapter 16, Building Web Services with JAX-WS.) Second, a web service client can invokethe business methods of a stateless session bean. Message beans cannot be accessedby web service clients. Provided that it uses the correct protocols (SOAP, HTTP, WSDL), any web serviceclient can access a stateless session bean, whether or not the client iswritten in the Java programming language. The client doesn’t even “know” what technologyimplements the service: stateless session bean, JAX-WS, or some other technology. In addition,enterprise beans and web components can be clients of web services. This flexibilityenables you to integrate Java EE applications with web services. A web service client accesses a stateless session bean through the bean’s webservice endpoint implementation class. By default, all public methods in the bean classare accessible to web service clients. The@WebMethod annotation may be usedto customize the behavior of web service methods. If the@WebMethod annotation is usedto decorate the bean class’s methods, only those methods decorated with@WebMethod areexposed to web service clients. For a code sample, seeA Web Service Example:helloservice. Method Parameters and AccessThe type of access affects the parameters of the bean methods thatare called by clients. The following topics apply not only to method parameters butalso to method return values. IsolationThe parameters of remote calls are more isolated than those of local calls.With remote calls, the client and bean operate on different copies of aparameter object. If the client changes the value of the object, the valueof the copy in the bean does not change. This layer ofisolation can help protect the bean if the client accidentally modifies the data. In a local call, both the client and the bean can modifythe same parameter object. In general, you should not rely on this sideeffect of local calls. Perhaps someday you will want to distribute your components,replacing the local calls with remote ones. As with remote clients, web service clients operate on different copies of parametersthan does the bean that implements the web service. Granularity of Accessed DataBecause remote calls are likely to be slower than local calls, theparameters in remote methods should be relatively coarse-grained. A coarse-grained object contains more datathan a fine-grained one, so fewer access calls are required. For the samereason, the parameters of the methods called by web service clients should alsobe coarse-grained. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |