Demonstrates a basic stateful counter Web Service. The Java object that implements the service persists for the duration of the HTTP session.
Quick StartEnsure that you have installed the required software and set up your environment. You must have OC4J running. If it is not running on localhost, port 8888, you'll have to modify the ANT script (build.xml) to use the correct host and port to your server.
Once you have the server configured and running, just type ant from the basic/stateful directory. The service artifacts and implementation class will be compiled and placed into a WAR which is then placed into an EAR. The EAR will be deployed to OC4J. Next the stubs and client will be compiled. Finally the service will be invoked by the client and you should see the output both in the server's message output and on the stdout of the client.
Step By StepAnt tasks have been configured for easy running of this demo.
To create a deployable application, type.
% ant gen-service
This should result in a j2ee webservices compliant application file: dist/Counter.ear.
Note the following in service-config.xml to enable a stateful Web services:
<!-- Mark that this is service is stateful and 'session' scoped. Timeout is optional. Default is 60 sec. -->
<stateful>
<scope>session</scope>
<timeout>90</timeout>
</stateful>
To deploy and bind the application.
% ant deploy-service
Visit the http test page by going to http://localhost:8888/Counter/Counter to make sure that the application has been deployed properly.
In this step you will generate the stubs for the service. A client application
uses a stub to invoke operations on a remote service. Examine the file client-config.xml.
This is the configuration file that the WebServices Assembler (WSA)
tool uses to generate the stubs. To generate the stubs type:
% ant gen-stubs
The source for the stubs will be placed in build/src/client
directory .
To run the client, issue:
% ant run-demoYou should see a result like :
[java] Counter call 1 is: 1
[java] Counter call 2 is: 2
If you run it again, you will see the same result, i.e.:
[java] Counter call 1 is: 1
[java] Counter call 2 is: 2
As can observe, the Counter value is incremented separately in each (HTTP)
session. In the client code you need to set the following property to ensure the client participates in a session (this is a prerequisite for maintaining state in a session):
// Set SESSION_MAINTAIN_PROPERTY on the Stub
stub._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, new Boolean("true"));
If SESSION_MAINTAIN_PROPERTY is not enabled, the
resulting counter values would be: 1, 2, 3, 4...
In other words, by default the scope of Java class service instance is the application lifetime on the server. For stateful Web Services, we only maintain the Java service instance for the duration of the session. For each session, a new instance will be created.