Creating Web Services in WebLogic

A web service is a remote procedure available to clients through TCP/IP, typically using HTTP or SMTP as the transport and XML for encoding. The web service is described using standard XML notation called a service description. A web service fulfills a single task or a set of tasks.

All details of the web service are hidden from the user, and the service is both hardware and software independent. This encourages software developers to build applications consisting of small, individual services, which can then be used alone or in groups to perform even more complex tasks.

You can create a web service in two ways: manually, through the creation of Java source and configuration files, or using a GUI system called WebLogic Workshop. In this article, you create a web service manually.

Defining a web service

Before you create your web service, you should define what it will do. This allows you to properly construct the interface that other applications must adhere to if they want to communicate with your web service.

The most basic design decision is which of the two general types of web services you will create:

  • Synchronous service
  • Asynchronous service

A synchronous web service, which is the default, begins when the web service receives a message. This web service responds immediately.

Synchronous web services are most useful when the client program requires the data returned from the request immediately. An example of this type of data is the current stock quote for a particular company.

An asynchronous web service is asynchronous, so messages can be exchanged freely between the client and web service. A message from one side does not oblige the other to send a corresponding message. When a client sends a message to an asynchronous web service, the client doesn't wait for a message back from the web service. The web service may send a message back to the client at a later time, but nothing inherently ties this message to the original message that the client sent.

Asynchronous web services are a good choice when you're sending information that doesn't require a response. For example, you may want to a send a message to several of your servers, giving them a new greeting message to display to users.

Choosing and building a backend component

The purpose of your web service is to allow remote clients to access services that you provide. These services, called the backend component, take the form of Java code. You can build your backend component as one of the following:

  • A method of a stateless session EJB
  • A method of a Java class
  • A JMS method consumer

WebLogic can make any of these items available as a web service. This makes it convenient to package existing Java code as a web service. If you've already created stateless session EJBs, you can package them as a web service. Or if your code exists in regular Java classes, you can provide access to your class as a web service. You can also use JMS messaging as a backend service for your web service.

Building a synchronous web service

In this section, you build a synchronous web service using a regular Java class and then using a stateless session EJB. First, you must write the backend component. This will be either a Java class, a stateless EJB, or a JMS method consumer.

Working with a Java class backend component

You must follow a few rules when implementing a web service operation using a Java class:

  • Don't start any threads. This rule applies to all Java code that runs on WebLogic Server.
  • Define a default no-argument constructor.
  • Define as public the methods of the Java class that will be exposed as web service operations.

You must write thread-safe Java code because WebLogic Server maintains only a single instance of a Java class that implements a web service operation, and each invocation of the web service uses this same instance.

For an example of implementing a WebLogic web service operation with a Java class, go to the following directory:

WL_HOME\samples\server\src\examples\webservices\basic\javaclass

where WL_HOME refers to the main directory of your WebLogic Server installation. On a Windows system, this directory is usually C:\bea\weblogic81.

Listing 1 shows a Java class that you could use as a backend component. This Java class contains a single method, named sampleMethod, that accepts an int and returns a String.

Listing 1: Java Class Backend Component

package com.dummies.ejb;
public class SampleBackendComponent
{
public String sampleMethod(int num)
{
switch(num)
{
case 1:return "One";
case 2:return "Two";
case 3:return "Three";
case 4:return "Four";
case 5:return "Five";
case 6:return "Six";
case 7:return "Seven";
case 8:return "Eight";
case 9:return "Nine";
case 10:return "Ten";
default:return "Some Number";
}
}
}

Working with a stateless session EJB backend component

You may also choose to build your backend component as a stateless session EJB. Writing Java code for a stateless session EJB for a web service is no different than writing a standalone EJB.

In the web-services.xml deployment descriptor, you can specify that a web service operation is one way, which means that the client application that invokes the web service doesn't wait for a response. When you write the Java code for the EJB method that implements this type of operation, you must specify that it returns void.

When choosing between using an EJB or a Java class backend component, consider the other ways in which your backend component will be used. If your backend component will be commonly accessed as an EJB, build it as an EJB. This allows you to use the same code for both your EJB and web service.

Considerably more resources are required to call a web service than to call an EJB. Because of this, it's common to create all your backend components as EJBs and then allow external applications to access your backend components as web services. Your own local applications, which are running on the same network as your WebLogic server, can use the faster EJB calling method.

Listing 2 shows the bean file that implements the EJB backend component. The EJB backend component is nearly the same as the Java class backend component. The main difference is the additional code used to support the EJB.

Listing 2: EJB Backend Component

package com.dummies.ejb;
import javax.ejb.*;
import java.rmi.*;
import javax.swing.*;
public class SampleBean implements SessionBean
{
private SessionContext stx;
//Required methods, not used by this type of bean
public void ejbCreate(){}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
// setter for the SessionContext
public void setSessionContext(SessionContext ctx)
{
ctx = this.stx;
}
// the sample method
public String sampleMethod(int num)
throws RemoteException
{
switch(num)
{
case 1:return "One";
case 2:return "Two";
case 3:return "Three";
case 4:return "Four";
case 5:return "Five";
case 6:return "Six";
case 7:return "Seven";
case 8:return "Eight";
case 9:return "Nine";
case 10:return "Ten";
default:return "Some Number";
}
}

}

You must specify that the bean is a stateless EJB in the ejb-jar.xml file, which is shown in Listing 3. You can use many nodes and attributes.

Listing 3: ejb-jar.xml File for a Backend Component

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SampleObject</ejb-name>
<home>com.dummies.ejb.SampleHome</home>
<remote>com.dummies.ejb.Sample</remote>
<ejb-class>com.dummies.ejb.SampleBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.