精通soa8.8

来源:互联网 发布:网络同步的网络云盘 编辑:程序博客网 时间:2024/05/01 21:29

8.8   在Eclipse3.1.2上创建相应的调用SOA Web

Service的Struts客户端类

这里将在Eclipse3.1.2+WebLogic 9.1+Struts上来调用RAD Websphere 6.0所提供的SOA服务,实现跨不同服务器的基于SOA架构的整合应用。

8.8.1   复制入站服务的WSDL到相应的Eclipse目录下

如下为入站服务的3类WSDL。

l          后缀为ServiceService.wsdl

l          后缀为ServicePortTypes.wsdl

l          后缀为ServiceBindings.wsdl

具体说明分别如下。

Ø          BookStoreBus.BookMgrDestinationInboundServiceService.wsdl

Ø          BookStoreBus.BookMgrDestinationInboundServicePortTypes.wsdl

Ø          BookStoreBus.BookMgrDestinationInboundServiceBindings.wsdl

Ø          BookStoreBus.OrderMgrDestinationInboundServiceService.wsdl

Ø          BookStoreBus.OrderMgrDestinationInboundServicePortTypes.wsdl

Ø          BookStoreBus.OrderMgrDestinationInboundServiceBindings.wsdl

Ø          BookStoreBus.UserAccountMgrDestinationInboundServiceService.wsdl

Ø          BookStoreBus.UserAccountMgrDestinationInboundServicePortTypes.wsdl

Ø          BookStoreBus.UserAccountMgrDestinationInboundServiceBindings.wsdl

将上述内容复制到相应的Eclipse目录下,如图8-123所示。

图8-123   将入站服务的3类WSDL复制到Eclipse目录下

8.8.2   创建相应的Ant Build脚本生成相应的Web Service客户端程序

将上面的9个WSDL文件复制到相应的目录后,就可以创建build.xml,以及用Ant Build生成相应的Web Service客户端程序了。

1.创建相应的build.xml(可参见第5章)

在build.xml中定义如下相应的输出路径。

<property name="axis.home" location="C:/axis-1_1"/>

<property name="options.output" location="./Web Service Client Source"/>

在build.xml定义如下原始的WSDL文件和所在目录。

<!--下面定义对应于上面

      <target name="all">

          <antcall target="bindings1"/>

           <antcall target="portTypes1"/>

           <antcall target="service1"/>

          ……

      </target>

<!--下面是以BookStoreBus.UserAccountMgrDestinationI

       nboundServiceService.wsdl为例,定义其所在目录为./wsdl,即相应的输出

      目标target为bindings1,其他的8个WSDL文件都进行类似的定义

-->

   <target name="bindings1">

     <antcall target="-WSDL2Axis">

       <param name="options.WSDL-URI"

location="./wsdl/BookStoreBus.UserAccountMgrDestinationInbound

                ServiceService.wsdl"/>

     </antcall>

   </target>

2.在Eclipse中运行相应的Ant Build

用鼠标右键单击“build.xml”,在弹出的快捷菜单中选【Run As】→【Ant Build】命令,如图8-124所示。

图8-124   选择【Ant Build】

弹出“Ant Build”对话框,如图8-125所示。

图8-125   “Ant Build”对话框

3.Ant Build所生成的Web Service客户端程序

创建com.bkstore.ejb.sessionbean 包下面的客户端Java类,如图8-126所示。

图8-126   创建com.bkstore.ejb.sessionbean目录下面的客户端Java类

下面是所创建的exception、dto、Binding和BookStoreBus.Service包下的类,如图8-127所示。

图8-127   创建的exception、dto、Binding和BookStoreBus.Service包下的类

上面所创建的类的内容类似于5.7节所介绍的WSDL所创建的Web Service的客户端Java类。

8.8.3   客户端和SOA Web Service的集成

本节将通过BookStoreMgrImp类(model class)来调用相应的SOA Web Service,实现客户端和SOA Web Service的集成,如图8-128所示。

图8-128   实现客户端和SOA Web Service的集成

客户端对SOA的Web Service的调用在BookStoreMgrImp.java中实现,参见如下代码,包括本章所实现的所有业务逻辑的接口调用。

package com.bkstore.model.service.imp;

……

public class BookStoreMgrImp implements   BookStoreMgrIfc {

     private BookMgr getBookMgr() throws ServiceException

     {

          BookMgrDestinationInboundServiceLocator bookMgrLocator = new

                   BookMgrDestinationInboundServiceLocator();

          return bookMgrLocator.getSOAPHTTPChannel1InboundPort();

     }

     private OrderMgr getOrderMgr() throws ServiceException

     {

          OrderMgrDestinationInboundServiceLocator orderMgrLocator = new

                   OrderMgrDestinationInboundServiceLocator();

          return orderMgrLocator.getSOAPHTTPChannel1InboundPort();

     }

     private UserAccountMgr getUserAccountMgr() throws ServiceException

     {

          UserAccountMgrDestinationInboundServiceLocator

               userAccountMgrLocator=new

               UserAccountMgrDestinationInboundServiceLocator();

          return userAccountMgrLocator.getSOAPHTTPChannel1InboundPort();

     }

      public boolean checkUserLogin(String loginName, String password)

      throws ServiceException,

          RemoteException {

          return getUserAccountMgr().checkUserLogin(loginName,password);

      }

     

      public void saveUserInfo(UserAccountDTO userAccountDTO) throws

          ServiceException, RemoteException

      {    

          getUserAccountMgr().saveUserInfo(userAccountDTO);

      }

     public List getUserList() throws ServiceException, RemoteException {

            ArrayList list = new ArrayList();

          ArrayOf_tns2_UserAccountDTO userAccountArrays =

               getUserAccountMgr().getUserList();

          UserAccountDTO[] userAccountDTOs =

               userAccountArrays.getUserAccountDTO();

          for (int i=0; i<userAccountDTOs.length; i++) {

               list.add(userAccountDTOs[i]);

          }

          return list;

      }

      public void updateAccountBalance(UserAccountDTO userAccountDTO)

          throws RemoteException, ServiceException

      {

          getUserAccountMgr().updateAccountBalance(userAccountDTO);    

      }

      public UserAccountDTO getUserInfo(String loginName) throws

          ServiceException, RemoteException {

          return getUserAccountMgr().getUserInfo(loginName);

      }

    

      public void createBook(BookDTO bookDTO) throws RemoteException,

          ServiceException {

          getBookMgr().createBook(bookDTO);

      }

      public BookDTO getBook(int getBookBookID) throws RemoteException,

          ServiceException {

          return getBookMgr().getBook(getBookBookID);

      }

      public ArrayOf_tns2_BookDTO getBookList() throws RemoteException,

          ServiceException {

          return getBookMgr().getBookList();

      }

      public void createOrderItem(ArrayOf_tns2_OrderItemDTO

          orderItemDTOs) throws RemoteException, ServiceException

      {

          getOrderMgr().createOrderItem(orderItemDTOs);

      }

      public int createOrder(OrderDTO orderDTO) throws RemoteException,

          ApplicationException, ServiceException

      {

          return getOrderMgr().createOrder(orderDTO);

      }    

}

(1)getBookMgr:得到基于SOA 的关于图书管理BookMgr入站服务的Web Service组件。

l          得到基于SOA的关于图书管理BookMgr入站服务的Web Service的地址。

l          返回基于SOA的关于图书管理BookMgr入站服务的Web Service服务接口。

(2)getOrderMgr:得到基于SOA 的关于购物车OrderMgr入站服务的Web Service组件。

l          得到基于SOA的关于购物车OrderMgr入站服务的Web Service的地址。

l          返回基于SOA的关于购物车OrderMgr入站服务的Web Service服务接口。

(3)getUserAccountMgr:得到基于SOA 的关于用户账户管理UserAccountMgr入站服务的Web Service服务接口。

l          得到基于SOA的用户账户管理UserAccountMgr入站服务的Web Service的地址。

l          返回基于SOA的关于用户账户管理UserAccountMgr入站服务的Web Service服务接口。

(4)checkUserLogin:检查用户登录的用户名和密码是否正确。

调用SOA的Web Service接口checkUserLogin去检查用户名和密码是否正确。

(5)saveUserInfo:存储用户信息。

调用SOA的Web Service接口saveUserInfo存储用户信息。

(6)getUserList:得到所有用户信息。

l          创建一个ArrayList的实例。

l          调用SOA的Web Service接口getUserList得到所有用户的数组。

l          将所有用户的数组加入到ArrayList之中。

l          返回ArrayList。

(7)updateAccountBalance:更新用户账户资金。

调用SOA的Web Service接口updateAccountBalance更新用户账户资金。

(8)getUserInfo:通过用户名得到用户信息。

调用SOA的Web Service接口getUserInfo通过用户名得到用户信息。

(9)createBook:创建和加入新的图书到图书管理系统。

调用SOA的Web Service接口createBook创建和加入新的图书到图书管理系统。

(10)getBook:通过系统的图书号查询一本书。

调用SOA的Web Service接口getBook通过系统的图书号查询一本书。

(11)getBookList:得到包含系统所有图书的一个数组。

调用SOA的Web Service接口getBookList得到包含系统所有图书的一个数组。

(12)createOrderItem:创建图书订单明细。

调用SOA的Web Service接口createOrderItem创建图书订单明细。

(13)createOrder:创建图书订单。

调用SOA的Web Service接口createOrder创建图书订单。

8.8.4   创建相应的Struts Action Bean类

Struts Action Bean类将模型层和视图JSP关联起来,在Action Bean的execute方法中调用上面的BookStoreMgrImp的接口,如图8-129所示。

图8-129   创建相应的Struts Action Bean类

8.8.5   创建相应的JSP

用JSP来实现视图(页面显示),JSP是通过相应的Struts的tag lib和Form Bean来创建的,如图8-130所示。

图8-130   创建相应的JSP

 

8.9   运行本章的例子

本书所附光盘提供了本章实例源代码,读者需要先创建一个对应于RAD的chapter_8_rad_workspace,然后导入光盘目录chapter_8_rad_workspace下面的bkstoreEJBProjectEAR.ear即可(可参照第7章例子)。

读者还需要完成下面的工作。

(1)配置数据源(参见第7章)。

(2)根据8.2节创建DB2的数据库表和数据。

(3)在RAD上启动Server和Admin Console。

(4)完成8.7节所说明的建立服务总线的所有工作。

(5)发布入站服务,生成WSDL文件。

此外读者需要将光盘里的chap_8_eclipse.zip解压缩,然后将Eclipse3.1.2指向chap_8_eclipse以运行WebLogic(参照第7章的例子),以实现WebLogic对Websphere的服务总线的调用。因为每个人的机器的名字不同,读者需要用自己所创建的入站服务的WSDL文件覆盖光盘中的WSDL文件,重新生成和编译客户端代码。

8.10   小结

本章首先介绍了SOA出现的原因在于其能够提供一个整合和监控各种松散耦合(或完全解耦)的各种服务的整合平台。然后详细介绍了如何创建基于Websphere 6.0 SIBus服务总线的SOA架构的企业服务系统。以一个网上书店系统的实例,详细介绍了SOA的实施过程。完成了在Websphere 6.0上建立SOA的网上书店服务体系。在WebLogic 9.1中应用Struts调用SOA Web Service客户端,实现基于SOA架构的Struts、EJB和Web Service的J2EE跨服务器平台整合应用开发。一个SOA的实施主要包括以下几个方面。

l          对业务需求进行分析,将整个业务需求划分为松散耦合的子服务系统,实现本章第1节所述的SOA的第1个要点。

l          对服务接口方法进行粗粒度的接近业务实际服务操作的划分,实现SOA的第2个要点。

l          在进一步细化业务需求的基础上对各个子服务系统进行数据库设计、建模、创建类图、创建时序图等设计工作。

l          在设计的基础上通过IDE工具实现各个子服务系统的编程开发。

l          通过服务总线将各个子服务系统进行集成,实现SOA的第3个要点,即服务的地址和传输协议的透明化(此外Websphere 6.0 SIBus作为服务总线进一步提供了修改消息格式,修改消息内容,以及改变传输协议等强大功能,有兴趣的读者可以进行进一步的探讨)。

l          将用户界面系统和服务总线进行集成。

本书第6章介绍了基于BEA Aqulogic服务总线的SOA的实现,本章进一步介绍了基于IBM SIBus服务总线的SOA的实现,这样对基于目前市面上两大主流J2EE厂商的服务总线ESB产品进行SOA的实施本书都介绍到了,希望能对读者将来的SOA项目实施起到帮助和借鉴作用。

 
原创粉丝点击