Java
  Home arrow Java arrow Page 13 - Using RPC-Style Web Services with J2EE
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Using RPC-Style Web Services with J2EE
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2004-12-08


    Table of Contents:
  • Using RPC-Style Web Services with J2EE
  • Web Service Overview
  • Web Service Technologies and Protocols
  • Web Services for J2EE
  • RPC-Oriented Web Services
  • Creating a Simple Service
  • The WSDL File
  • Creating the Web Service WAR
  • The Web Services Deployment Descriptor
  • Building More Robust Web Services
  • Exposing EJBs Through Web Service Protocols
  • Web Service Compiler Configuration File
  • Configuring the EJB Component
  • Other Considerations for Web Services
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.

    bond

    Figure 20.18
    WSDL location configuration.

    bond

    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.



     
     
    >>> More Java Articles          >>> More By Sams Publishing
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT