Home arrow Web Services arrow Page 3 - Using EJBs with Axis

Using EJBs with Axis

Read this article if you would like to learn how to use SOAP, WSDL, and the Web services stack with J2EE. The second of three parts, this section focuses on EJBs and Axis. It is excerpted from chapter 7 of the book Building Web Services with Java: Making sense of XML, SOAP, WSDL, and UDDI, written by Steve Graham et al. (Sams; ISBN: 0672326418).

Author Info:
By: Sams Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
May 25, 2006
  1. · Using EJBs with Axis
  2. · Using EJBs from Axis
  3. · The Session Bean
  4. · The Deployment Unit
  5. · Exposing the EJBs via Axis

print this article

Using EJBs with Axis - The Session Bean
(Page 3 of 5 )

The session bean has similar interface and implementation classes (see Listings 7.2 and 7.3).

Listing 7.2  SkatesProducts.java Session Bean Interface

public interface SkatesProducts extends
javax.ejb.EJBObject { public void addProduct(Product prod) throws java.rmi.RemoteException; public Product viewProduct(String prodcode) throws java.rmi.RemoteException; }

Listing 7.3 SkatesProductsBean.java Session Bean Implementation

import javax.ejb.CreateException; 
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class SkatesProductsBean
implements javax.ejb.SessionBean { private javax.ejb.SessionContext mySessionCtx; public javax.ejb.SessionContext
getSessionContext() { return mySessionCtx; } public void setSessionContext
(javax.ejb.SessionContext ctx) { mySessionCtx = ctx; } public void ejbCreate() throws
javax.ejb.CreateException {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbRemove() {} public void addProduct(Product prod) { try { InitialContext ic = new InitialContext(); SkatesEntityLocalHome selh = (SkatesEntityLocalHome) ic.lookup("java:comp/env/skateEnt"); SkatesEntityLocal skent = selh.create(prod.getProductCode()); skent.setDescription(prod.getDescription()); skent.setPrice(prod.getPrice()); } catch (NamingException ne) { // handle naming } catch (CreateException ce) { // handle create } } // viewProduct goes here }

Because the entity is simply a view on the persistence layer, it utilizes the underlying semantics of a data element (create, read, update, delete). The session bean has the interface we wish to offer to the service requestor. We want to expose two "verbs" add and view to the service requestor. Because the service interface we're designing is stateless, we need to map one (or more) of the parameters of the service request into the unique primary key the entity bean needs.

The implementation code shows some standard code—that is, it's the same in most EJBs. The ejbXXXXXX() methods, such as ejbCreate(), let you override methods that are called by the container during the component's event lifecycle. A more intelligently designed bean would get a reference to the entity at component creation time, reuse it, and therefore be more efficient. However, for this simple example it's easier to get the reference as needed.

In the addProduct() method shown, the logic gets a reference to the entity bean using a local ejb-ref (local name for the entity bean) "java:comp/env/skateEnt". The "java:comp/env/" piece is predefined (it's the J2EE way of saying environment). "skateEnt" is a name we chose to identify the entity bean. This reference is then defined in the DD to point to the real internal name of the entity bean. This capability allows EJBs to be wired together, so a different entity bean (perhaps with a different persistence method) that implemented the same interface contract could be used without recoding our session bean.

The code then calls the create() method on the entity to create a new product instance, using the product code as the primary key. At this point, if there is already a product in the database that has the same key, a createException will be thrown, which will cause a fault in the Web service response.

There is similar code in the viewProduct() method shown here:

public Product viewProduct(String prodcode) {
Product prod = new ProductImpl();
try {
InitialContext ic = new InitialContext();
SkatesEntityHome home =
SkatesEntity skent = 
catch (NamingException ne) { /* deal with exc */}
catch (FinderException fe) { /* deal with exc */}
return prod;

This code looks up the existing product instance and then returns the information as a new Product object.

blog comments powered by Disqus

- Dealing with Loose Coupling in a Service-Ori...
- Loose Coupling in a Service-Oriented Archite...
- Safety, Idempotence, and the Resource-Orient...
- The Resource-Oriented Architecture in Action
- Features of the Resource-Oriented Architectu...
- The Resource-Oriented Architecture
- Getting Started with Flex
- Automated Billing and Faxing for the Web
- An Introduction to Web Services
- The Foundations of Web Services: From Novice...
- Web Services Reengineering: Finishing Touches
- Fault Handling with Web Services
- Flow and Web Services
- Process Lifecycles and Web Services
- Business Processes and Web Services

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials