Java & J2EE Page 13 - Using RPC-Style Web Services with J2EE |
To complete the Web Service configuration, highlight the agency-session-ejb in the left-hand pane and select its Web Services tab. Select the AgencyService in the list and in the Sun-specific Setting section, set the WSDL Publish Location to be jaxrpc/ServiceBean?WSDL as shown in Figure 20.18. To complete the EJB configuration, highlight the ServiceBean itself in the left-hand pane and select the EJB Refs tab. Click Add and add a reference to the JobBean setting the name to ejb/JobLocal, type to Entity, interfaces to Local, and the home and local interface names to data.JobLocalHome and data.JobLocal respectively. This is shown in Figure 20.19. Once this is added, select the reference and change the Sun-specific settings for ejb/JobLocal to data-entity-ejb.jar#JobBean.
Figure 20.18
Figure 20.19 During this process, deploytool has generated several new files for you:
The Web Services Deployment Descriptor The Web Services Deployment Descriptor for the EJB is similar to that for the servlet-based Web Service, containing the service description and pointers to the WSDL and mapping files. The principal difference is that the port-component points to an EJB rather than a servlet: <webservices ...> <webservice-description> ... <port-component> <service-impl-bean> <ejb-link>ServiceBean</ejb-link> </service-impl-bean> </port-component> </webservice-description> </webservices> Deploying the Service Now you can deploy the service. Select the Agency EAR in the left-hand pane of deploytool and then select Tools, Deploy from the menus. Provide your administrator user name and password if prompted and ensure that the message "Operation Complete Successfully" is displayed on the Distribute Module screen. As with the servlet-based Web Service, all of the server-side scaffolding required for the Web Service is generated during deployment. There is no client JAR file because you will contact the service over SOAP so the only thing needed to create the artifacts used by the client is the WSDL description. You can now access the simple Web Service at the URL http://localhost/jaxrpc/ServiceBean and its WSDL is available at http://localhost/jaxrpc/ServiceBean?WSDL. The WSDL document is identical to the one in the EJB-JAR file, but it now contains the location information for the deployed service: ... <service name="AgencyService"> <port name="ServicePort" binding="tns:ServiceBinding"> <soap:address xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/ location="http://localhost:8000/jaxrpc/ServiceBean"/> </port> </service> ... In the next section, you will use this WSDL file to generate stubs through which a client application can contact the Web Service. Consuming the Simple Greeting Service As you might expect, the client for a Web Service based on an EJB is very similar to one for a Web Service based on a servlet. The client will need to obtain a stub that represents the PortType of the deployed service. This can be generated by running wscompile against the WSDL file retrieved from the deployed Web Service. The wscompile configuration file (client-config.xml) is almost identical except that it points to a different URL: <wsdl location=http://localhost:8000/jaxrpc/ServiceBean?WSDL The wscompile command line is identical to the one used for the servlet-based Web Service you saw earlier. After you have generated the stubs and factories, you can use them in your client. The client will be similar to the servlet-based Web Service client shown in Listing 20.7, but obviously the types and methods implemented by them will be different. The following client-specific artifacts are generated that can be used in the client:
A client for the Web Service ServiceBean is shown in Listing 20.11. Again, this shares the namespace of the proxies (client) and catches RemoteExceptions. The client calls the findJobsAtLocation passing in the location string provided on the command line. It then prints out the job information returned from the Web Service. Listing 20.11 Web Service Client for the ServiceBean package client;
public class AgencyServiceClient
{
private static String location = "%";
public static void main(String[] args)
{
if (args.length == 1)
{
location = args[0];
}
else if (args.length > 1)
{
System.err.println("Usage: AgencyServiceClient
You compile and run your client as you would any other Java application, but ensure that the generated client-side classes are on your classpath. To generate the client-side scaffolding, compile the pre-provided client class and run it, using the following Ant directive: asant AgencyServiceClient This will communicate with the EJB-based web service you deployed earlier. If you enter the location "London," you should see the following message: [java] Using Location: London [java] Job list: [java] winston/Cigar trimmer So, now you have created, deployed, and invoked an EJB-based Web Service under J2EE using JAX-RPC.
blog comments powered by Disqus |
|
|
|
|
|
|
|