Chapter 3. Writing Your First Stateful Web Service in 5 Simple Steps

MathService

In this chapter we are going to write and deploy a simple stateful web service that uses WSRF to keep state information. Our first web service is an extremely simple Math Web Service, which we'll refer to as MathService. It will allow users to perform the following operations:

Furthermore, MathService will have the following resource properties (RPs for short):

We will also add a "Get Value" operation to access the Value RP. In Chapter 6, Resource Properties we will see a better way of accessing resource properties, without having to add get/set operations.

MathService's internal logic is very simple. Once a new resource is created, the "value" RP is initialized to zero, and the "last operation" RP is initialized to "NONE". The addition and subtraction operations expect only one integer parameter. This parameter is added/subtracted to the "value" RP, and the "last operation" RP is changed to "ADDITION" or "SUBTRACTION" accordingly. Also, the addition and subtraction operations don't return anything.

Finally, this first example will be limited to having one single resource. In the following chapters we will see how we can write a service that has several resources associated to it, as seen in Section 1.3.2, “The resource approach to statefulness”.

High-tech stuff, huh? Don't worry if this seems a bit lackluster. Since this is going to be our first stateful web service, it's better to start with a small didactic service which we'll gradually improve by adding more complex resource properties, notifications, etc. You should always bear in mind that MathService is, after all, just a means to get acquainted with GT4. Typical WSRF Web Services are generally much more complex and do more than expose trivial operations (such as addition and subtraction).

The Five Steps

Writing and deploying a WSRF Web Service is easier than you might think. You just have to follow five simple steps.

  1. Define the service's interface. This is done with WSDL

  2. Implement the service. This is done with Java.

  3. Define the deployment parameters. This is done with WSDD and JNDI

  4. Compile everything and generate a GAR file. This is done with Ant

  5. Deploy service. This is also done with a GT4 tool

Don't worry if you don't understand these five steps or are baffled by terms such as WSDL, WSDD, and Ant. In this first example we're going to go through each step in great detail, explaining what each step accomplishes, and giving detailed instructions on how to perform each step. The rest of the examples in the tutorial will also follow these five steps, but won't repeat the whole explanation of what that step is. So, if you ever find that you don't understand a particular step, you can always come back to this chapter ("Writing Your First Stateful Web Service in 5 Simple Steps") to review the details of that step.

Before we start...

Ready to start? Ok! Just hold your horses for a second. Don't forget to download the tutorial files before you start. You can find a link to the tutorial files in the tutorial website. The examples bundle includes all the tutorial source files, plus a couple of extra files we'll need to successfully build and deploy our service. Just create an empty directory on your file system and untar-gunzip the file there. From now on, we'll refer to that directory as $EXAMPLES_DIR.

Once you have the files, take into account that there are two ways of following the first chapters of the tutorial:

Ok, now we're ready to start :-)