Sample Message Web Service XML

In this section, you will learn how to implement a simple Web service that sends a message. This message is sent to the service endpoint and then back to the client. The service definition code for the Message service is as follows:

In the preceding code, you first need to import the rmi package because the JAX-RPC messaging model is based on Java's RMI method invocation. The methods declared in the MessageIF interface throw a RemoteException exception to ensure compliance with the JAX-RPC specifications.

Note?/td>
The specifications state that all mappings of wsdl:operation to the Java type should throw the RemoteException exception and any other application-specific exceptions.

The code uses the MessageIF interface that extends the Remote interface. The MessageImpl class, provided by the MessageIF interface, implements the service definitions. The code for the MessageImpl class is as follows:

You should compile these files using the Ant Build tool, which is packaged with the JWSDP. You should place the files in a single directory.

build.xml File

To run the Ant tool on the files, you first need to create a build script. The build script controls the tasks that the Ant tool performs. The build script is shown in the following code:

Save the preceding code as the build.xml file. Place the Java files and the build.xml files in the same directory.

build.properties File

In addition to the build.xml and Java files, you need to create a build.properties file in the same directory. The build.properties file contains the username and password. This username and password is set up at installation time. The content of the build.properties file is shown in the following code:

Tip?/td>
Ensure that the environment variable, JWSDP_HOME, is set properly. The JWSDP_HOME variable should point to the home directory of the JWSDP. In addition, the PATH variable should be set to the <JWSDP_HOME>in directory.

You need to compile the Java files by using the Ant tool. Type the following command at the command prompt: ant compile-server

Tip?/td>
While you're executing the preceding command, ensure that you are in the same directory where you have stored the Java file, the build.properties file, and the build.xml file.

The output that is displayed when you execute the command is shown in Figure.

The Output of the ant Compile-Server Command

The Output of the ant Compile-Server Command

After successful execution of the ant compile-server command, two class files— MessageIF.class and MessageImpl.class—are created under the uildmessage directory.

Configuration File

Next, you need to create a configuration file that allows you to create a WSDL definition of your class file. The structure of the configuration file is as follows:

The code for the configuration file, config.xml, is as follows:

We will now provide an explanation of the code of the config.xml file:

The preceding statement is used to name the rmi. The name attribute determines the name of the service, and the targetNamespace attribute determines the target namespace for the WSDL. The typeNamespace attribute specifies the target namespace for the schema.

In the preceding statement, the service name attribute specifies the name of the service. The packageName attribute specifies the package name.

Using the xrpcc Tool

After you have saved the config.xml file, you need to generate the stubs and ties of the server-side classes that you created. To do this, use the xrpcc tool that is bundled with the JWSD pack.

The xrpcc tool creates the stubs and ties required to perform a remote procedure call. You can relate these stubs with the concepts of RMI stubs.

The following code executes the xrpcc tool and creates the appropriate classes and properties files:

xrpcc.bat -classpath build -both -d build config.xml

Various options that you can use with the xrpcc command are discussed in Table.

Options Used with the xrpcc Command

Options Used with the xrpcc Command

The xrpcc tool creates the following files in the message directory when it is executed:

  • Message_RequestStruct.class
  • Message _RequestStruct_SOAPSerializer.class
  • Message _ResponseStruct.class
  • Message_ResponseStruct_SOAPSerializer.class
  • Message IF.class
  • Message IF_Stub.class
  • Message IF_Tie.class
  • Message Impl.class
  • MyMessage.class
  • MyMessage _SerializerRegistry.class
  • MyMessage Impl.class

The files that are created under the build directory are as follows:

  • Message_Config.properties
  • MessageService.wsdl

The Message_Config.properties file contains information, such as the stub and the tie classes, the implementation class, the target namespace, and the port name. The content of the Message_ Config.properties file is displayed here:

The MessageService.wsdl file is an XML document containing information, such as the location of the Web service, the callable functions, the means to access those functions, and so on. The WSDL file enables the clients to locate and use the Web service. The following is the content of the MessageService.wsdl file:

After you finish generating the appropriate stubs, ties, and configuration files, the next step is to prepare a deployment descriptor for the Apache Tomcat server that is bundled with the JWSDP. Creating the deployment descriptor for the Apache Tomcat server is explained in the following section.

Creating the Deployment Descriptor for the Apache Tomcat Server

The web.xml file contains the deployment descriptor. The deployment descriptor contains various deployment details. The deployment descriptor is shown in the following code:

In the preceding deployment descriptor, the servlet mappings are specified to the servlet, com. sun. xml. rpc. server .http. JAXRPC Servlet. This servlet obtains the configuration details from the Message_ Config.properties file. You should copy this file under the /WEB-INF directory of the webapps oot directory of the Tomcat container.

Similar to the Message_Config.properties file, the web.xml file is also saved under the WEBINF directory. If you do not want to manually move these files to the appropriate directories, you can package the contents into a WAR file and deploy it on the server using the Ant tool.

Packaging Service in a WAR File

Next, you need to pack the service in the WAR file. To package the service definition into a WAR file, execute the following command at the command prompt: ant packagewar

When you execute the preceding command successfully, you should be able to locate a jaxrpc-message.war file under the current directory.

The following files are packaged in the jaxrpc-message.war file:

  • Service definition interfaces
  • Service definition classes
  • xrpcc-generated files
  • Classes for pluggable serializers and deserializers
  • Other files required by the service implementation classes
  • A file containing the deployment descriptor
  • A WSDL file that describes the service (optional)

Deploying the Packaged File

The next step after creating the WAR package is to deploy the package in the Tomcat container. To deploy the package, execute the following command at the command prompt: ant deploy

The output of the preceding command is displayed in Figure.

The Output of the ant deploy Command.

The Output of the ant deploy Command.

Note?/td>
When you execute the ant deploy command, a WAR file is created and placed in the <JWSDP_HOME> webapps directory. Therefore, in this example, the jaxrpc-message.war file is created in the <JWSDP_HOME> webapps directory.

Testing the Deployment

You have almost deployed the service to the Web server. To verify the deployment, proceed with the following steps:

  1. Start he Tomcat server by executing the following command:
  2. startup
  3. Open a browser window and then type the following URL in the address bar of the browser:
  4. http://localhost:8080/jaxrpc-message/jaxrpc
  5. After you execute the preceding step, you should be able to view the contents in the Web browser. The output is displayed in Figure.

The Deployment of the Message Service.

The Deployment of the Message Service.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics