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
- Create the Web service client:
- 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
- Right-click the WSDL file, for example, RewardPointsService.wsdl,
and select .
- Ensure the client project is WebSphereCommerceServerExtensionsData and
complete the wizard.
- Create the SessionBean façade:
- In the Enterprise Explorer view,
expand .
- Right-click Session Beans and
select .
- Ensure Session Bean is selected
and use RewardPointsServiceSessionFacade as
the Bean name. Click Next.
- Ensure Remote Client View is
selected and click Finish.
- Open the RewardPointsServiceSessionFacadeBean bean
class.
- 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;
}
- In the Outline view, right-click
the getBalance() method and select .
- In the Enterprise Explorer view,
expand .
- Right-click the Deployment Descriptor and
select Prepare for Deployment.
- Create a helper class to access the session bean:
- 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;
}
}
- Create a task command to use the session bean:
- 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);
}
}
}