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:
Addition
Subtraction
Furthermore, MathService will have the following resource properties (RPs for short):
Value (integer)
Last operation performed (string)
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).
Writing and deploying a WSRF Web Service is easier than you might think. You just have to follow five simple steps.
Define the service's interface. This is done with WSDL
Implement the service. This is done with Java.
Define the deployment parameters. This is done with WSDD and JNDI
Compile everything and generate a GAR file. This is done with Ant
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.
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:
With the example source
files: You'll have all the source code (Java, WSDL,
and WSDD) ready to use in $EXAMPLES_DIR
, so there's no need to
manually modify these files.
Without the examples source
files: Some people don't like getting all the source
code ready to use out-of-the-box, but prefer to write the
files themselves so they can have a better understanding of what
they're doing at each point. In fact, we think this is probably the
best way to follow this chapter (except for a few files which would take too long to write manually). Since this
chapter includes complete code listings (which
you can copy and paste to a file), you can easily write all the
files yourself. However, you do need a set of
auxiliary files included in the examples bundle that are needed
to build and deploy the services. So, if you want to follow the
examples without the source files, you still need to download the examples
files. Once you're in $EXAMPLES_DIR
, simply delete
directory "org
" to delete the source files, but don't
delete anything else.
Ok, now we're ready to start :-)