Integrating JAX-WS Web service clients in HCL Commerce Developer

A JAX-WS Web service client can be created from the WSDL file. The only point to consider is with its service reference.

If you define the Web service client in the Stores Web Module, the service reference will not be visible if you are running outside the Stores context. For example, if you attempt to use the Web service client from a scheduler job, the call will fail.

To make the Web Service client available to all the modules, it is recommended to create the client in the WebSphereCommerceExtensionsData or a new EJB module. Then you can use a stateless session bean facade to access the service.

Before you begin

To successfully run this example, a server must be present. You can either create a mock service from the WSDL file using SOAP, or you can install the following sample files on an application server:

Procedure

  1. Create the Web service client:
    1. Copy the WSDL files to the following folder, or any other folder in the workspace. If you copy the files using Windows Explorer, you must refresh the workspace:
      • workspace_dir\WebSphereCommerceServerExtensionsData\ejbModule\META-INF\wsdl
    2. Right-click the WSDL file, for example, RewardPointsService.wsdl, and select Web Services > Generate Client.
    3. Ensure the client project is WebSphereCommerceServerExtensionsData and complete the wizard.
  2. Create the SessionBean façade:
    1. In the Enterprise Explorer view, expand EJB Projects > WebSphereCommerceExtensionsData > Session Beans.
    2. Right-click Session Beans and select New > Session Bean.
    3. Ensure Session Bean is selected and use RewardPointsServiceSessionFacade as the Bean name. Click Next.
    4. Ensure Remote Client View is selected and click Finish.
    5. Open the RewardPointsServiceSessionFacadeBean bean class.
    6. Add a method that will be used to get the balance for a customer.
      For example:
      
      public Integer getBalance(Integer customerId ) {
         Integer balance = null;
         try {
            RewardPointsPortProxy proxy = new RewardPointsPortProxy(); 
            balance = proxy.getBalance( customerId );
         } catch ( Exception e ) {
            e.printStackTrace();
         }
         return balance;		
      }
      
    7. In the Outline view, right-click the getBalance() method and select Enterprise Bean (1.x-2.x) > Promote to Remote Interface.
    8. In the Enterprise Explorer view, expand EJB Projects > WebSphereCommerceExtensionsData > Deployment Descriptor.
    9. Right-click the Deployment Descriptor and select Prepare for Deployment.
  3. Create a helper class to access the session bean:
    1. In the WebSphereCommerceServerExtensionsLogic project, create a helper class to obtain a reference to the session bean.
      For example:
      
      package com.mycompany.rewardpoints.proxy;
      
      import java.rmi.RemoteException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import com.mycompany.RewardPointsServiceSessionFacade;
      import com.mycompany.RewardPointsServiceSessionFacadeHome;
      
      public class RewardPointsServiceHelper {
      
        private static RewardPointsServiceSessionFacade service = null;
      
        public static RewardPointsServiceSessionFacade getRewardPointsBean() {
      
          if ( service == null ) {
            try {
              Context ctx = new InitialContext();
              RewardPointsServiceSessionFacadeHome rewardsHome = (RewardPointsServiceSessionFacadeHome) ctx.lookup("ejb/com/mycompany/RewardPointsServiceSessionFacadeHome");
              service = rewardsHome.create();
      
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
      
          return service;
        }
      }
      
  4. Create a task command to use the session bean:
    1. Creating a task command is generally optional, but good practice. For example:
      
      package com.mycompany.rewardpoints;
      
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import com.ibm.commerce.command.TaskCommandImpl;
      import com.mycompany.RewardPointsServiceSessionBeanProxy;
      import com.mycompany.rewardpoints.proxy.RewardPointsServiceHelper;
      
      public class RewardPointsTaskCmdImpl extends TaskCommandImpl implements
          RewardPointsTaskCmd {
      
        private static final String CLASS_NAME = RewardPointsTaskCmdImpl.class.getName();
        private static Logger logger = Logger.getLogger(CLASS_NAME);
      
        Integer customerId = null;
        Integer balance    = null;
      
        public void setCustomerId(Integer customerId) {
          this.customerId = customerId;
          this.balance    = null;
        }
      
        public Integer getBalance() {
          return balance;
        }
      
        public void performExecute() {
      
          final String methodName = "performExecute";
      
          if (logger.isLoggable(Level.FINER)) {
            logger.entering( CLASS_NAME, methodName);
          }
      
          try {
      
            RewardPointsServiceSessionBeanProxy rewards = RewardPointsServiceHelper.getRewardPointsBean();
            balance = rewards.getBalance(customerId);
      
            if (logger.isLoggable(Level.FINE)) {
               logger.logp( Level.FINE, CLASS_NAME, methodName, "Customer " + customerId + " Balance is " + balance );
            }
      
          } catch ( Exception e ) {
             logger.logp(Level.SEVERE, CLASS_NAME, methodName, e.getClass().getName(), e );
            e.printStackTrace();
          }
      
          if (logger.isLoggable(Level.FINER)) {
            logger.exiting( CLASS_NAME, methodName);
          }
        }
      
      }