Using RPC-Style Web Services with J2EE - Configuring the EJB Component (Page 13 of 15 )
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
WSDL location configuration.

Figure 20.19
Adding entity references to the EJB-based Web Service.
During this process, deploytool has generated several new files for you:
The ejb-jar.xml file—This contains the EJB definition for your Web Service and looks like a standard stateless session bean deployment descriptor.
- The sun-ejb-jar.xml file—This contains the Sun-specific configuration performed during the bean configuration. This includes the setting of the endpoint URI and the location from which the WSDL description can be recovered:
<sun-ejb-jar>
<enterprise-beans>
...
<ejb>
<ejb-name>ServiceBean</ejb-name>
<jndi-name>ServiceBean</jndi-name>
<webservice-endpoint>
<port-component-name>Service</port-component-name>
<endpoint-address-uri>jaxrpc/ServiceBean</endpoint-
address-uri>
</webservice-endpoint>
</ejb>
<webservice-description>
<webservice-description-name>AgencyService</
webservice-description-name>
<wsdl-publish-location>jaxrpc/ServiceBean?WSDL</
wsdl-publish-location>
</webservice-description>
</enterprise-beans>
</sun-ejb-jar>
The webservices.xml file— This is described in the next section.
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
packageName= "client"/>
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:
Service interface—This is generated from the WSDL port description. It is almost identical to the remote Java Service interface from which the WSDL was generated.
AgencyService interface—This defines a single method getServicePort() that returns a client-side proxy for the Web Service implementing the Service interface.
AgencyService_Impl class—This is a factory class that represents the Web Service as a whole. You can obtain client-side proxies from instances of this class. For your simple service, this will just serve client-side proxies that implement the Service interface.
Service_Stub class—This is the client-side proxy itself that implements the client-side Java Service interface generated from the WSDL.
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
[ location ]");
System.exit(1);
}
try
{
AgencyService serviceProxy = new AgencyService_Impl();
Service interfaceProxy = serviceProxy.getServicePort();
System.out.println("Using Location: "+location);
System.out.println("Job list: ");
String[] jobs = interfaceProxy.findJobsAtLocation(location);
for(int i=0; i<jobs.length; ++i)
{
System.out.println(jobs[i]);
}
System.out.println("\nDone\n");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
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.
This chapteris fromTeach Yourself J2EE in 21 Days, second edition, byMartin Bond et. al.(Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.
|
Next: Other Considerations for Web Services >>
More Java Articles
More By Sams Publishing