|
|
概述
本文介绍了realMethods框架如何实现axis的java客户端代码(用java axis作Web Service的提供者,服务请求者用java)。
编写java客户端代码访问Web Service
本文以一个图书管理系统为例,展示如何使用java编写客户端代码访问Web Service(在图书管理系统中主要需要维护的是一个叫Book的对象)。
首先启动realMethods4.0的AIB,接着装载由library领域模型转换的xml文件,在AIB中选择“Delegate To Session Façade via Web Service”,并且选择BMP场景,最后产生基于领域模型的代码,所有产生的文件输出在d:\library目录上,产生代码后的目录结构如下所示:
附件:1112321415174-1.jpg(34K)
realMethods框架为Book产生了增加、修改、删除、根据主键ID查询Book特定的一条记录和查询Book的所有记录这五种基本操作。web层使用struts框架,web层的Action子类通过Delegate访问业务逻辑,(EJB),而Delegate中的代码是通过axis访问EJB的, 在d:\library\axis目录中就是realMethods生成的使用部署描述符WSDD描述被发布的web服务(web服务在这里就是EJB实现的业务逻辑代码),下面就是realMethods生成的描述web服务Book的部署描述符:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="urn:BookService" provider="java:EJB">
<parameter name="beanJndiName" value="com_library_BookSessionHome" />
<parameter name="homeInterfaceName" value="com.library.ejb.BookSessionHome" />
<parameter name="remoteInterfaceName" value="com.library.ejb.BookSessionRemote" />
<parameter name="allowedMethods" value="*" />
<parameter name="className" value="com.library.ejb.BookSessionBean" />
<parameter name="jndiURL" value="t3://localhost:7003" />
<parameter name="jndiContextClass" value="weblogic.jndi.WLInitialContextFactory" />
<typeMapping
xmlns:urn="urn:library"
qname="urn:BookBO"
type="java:com.library.bo.BookBO"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
<typeMapping
xmlns:urn="urn:library"
qname="urn:BookPrimaryKey"
type="java:com.library.primarykey.BookPrimaryKey"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
上面描述了一个名称为urn:BookService 的web服务。TypeMapping(类型映射)元素定义如何在Java类(或基本数据类型)和XML之间进行来回转换。第一个TypeMapping元素定义了urn:BookBO和java:com.library.bo.BookBO的类型映射。
接下来我要描述客户端代码如何访问上面发布的web服务。
Delegate中的代码就是我在这里要描述的axis 客户端代码,下面是代理通过axis访问增加一条Book的web服务:
package com.library.delegate;
import java.util.*;
import javax.xml.namespace.QName;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.XMLType;
import com.framework.business.axis.*;
import com.framework.business.bo.*;
import com.framework.business.delegate.*;
import com.framework.business.pk.*;
import com.framework.common.exception.*;
import com.framework.integration.log.*;
import com.library.axis.*;
import com.library.ejb.*;
import com.library.locator.*;
import com.library.primarykey.*;
import com.library.bo.*;
import com.library.to.*;
/**
* Book business delegate.
* <p>
* This class implements the Business Delegate design pattern for the purpose of:
* <ol>
* <li>Reducing coupling between the business tier and a client of the business tier by hiding all business-tier implementation details</li>
* <li>Improving the available of Book related services in the case of a Book business related service failing.</li>
* <li>Exposes a simpler, uniform Book interface to the business tier, making it easy for clients to consume a simple Java object.</li>
* <li>Hides the communication protocol that may be required to fulfill Book business related services.</li>
* </ol>
* <p>
* @author amanda
*/
public class BookBusinessDelegate
extends BaseBusinessDelegate
implements IBookBusinessDelegate
{
//************************************************************************
// Public Methods
//************************************************************************
/**
* Default Constructor
*/
public BookBusinessDelegate()
{
}
/**
* Constructor with a IBookBO
* @param businessObject
* @exception IllegalArgumentException Thrown when the parameters are null.
*/
public BookBusinessDelegate( IBookBO businessObject )
throws IllegalArgumentException
{
// Call base class
super(businessObject);
}
/**
* Book Business Delegate Factory Method
*
* Returns a singleton instance of BookBusinessDelegate().
* All methods are expected to be self-sufficient.
*
* @return BookBusinessDelegate
*/
static public BookBusinessDelegate getInstance()
{
if ( singleton == null )
{
singleton = new BookBusinessDelegate();
}
return( singleton );
}
/**
* Creates the provided BO.
* @param businessObject IBookBO
* @return IBookBO
* @exception ProcessingException
* @exception IllegalArgumentException
*/
public IBookBO createBook( IBookBO businessObject )
throws ProcessingException, IllegalArgumentException
{
if ( businessObject == null )
{
String errMsg = "BookBusinessDelegate:createBook( IBookBO businessObject ) - null businessObject provided.";
FrameworkDefaultLogger.error( errMsg );
throw new IllegalArgumentException( errMsg );
}
try
{
// create the FrameworkAxisSOAPParameter parameters
FrameworkAxisSOAPParameter parameter = new FrameworkAxisSOAPParameter ( "businessObject", businessObject, new QName("urn:BookService", "BookBO") );
ArrayList parameters = new ArrayList();
parameters.add( parameter );
// make the SOAP call
businessObject = (IBookBO)ApacheAxisHelper.makeAxisCall( "create", parameters, "BookService", getTypeMappings( "BookMaps" ), voQName );
|
|