`
pharaohsprince
  • 浏览: 288861 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

webservice学习笔记——提高篇

阅读更多

 /*title: web service入门学习笔记(五)
**date: 2007/01/18
**author:laomai
**url: http://blog.csdn.net/laomai/
*/


五、精简后的ant脚本
1、卸载webservice
 执行原build.xml里的clean和 undeploy任务,把安装好的webservice删除掉,具体办法为:
 (1)在myeclipse里打开build.xml文件
 (2)在build.xml文件里单击右键菜单中的""run as"->"2 ant build..."",
 (3)在弹出的对话框中只选择clean和undelpoy任务。 然后单击"run"按钮。
 此时再访问http://localhost:8080/Hello/HelloService?WSDL,出现http 404错误,说明卸载成功

2、简化后的脚本内容,在hello-jaxws目录下新建一个buildtest.xml文件,内容为
<?xml version="1.0" encoding="UTF-8"?>
<project name="hello-jaxws" default="all" basedir=".">
 <!-- javaee安装目录的设置,代替了原来脚本中的导入语句 -->
 <property name="javaee.home" value = "d:/Sun/SDK"/>
 <property name="javaee.domaindir" value="${javaee.home}/domains/domain1"/>
 <property name="javaee.server.name" value="localhost"/>
 <property name="javaee.server.port" value="8080"/>

  <!-- 设置发布目录和类的输出目录 -->
 <property name="autodeploydir" value="${javaee.domaindir}/autodeploy"/>
 <property name="classesdir" value="./build"/>   

 <!-- 设置java类库路径 -->
 <path id="classpath">
  <pathelement location="${javaee.home}/lib/j2ee.jar"/>
  <pathelement location="${classesdir}"/>
 </path>

 <target name="all" depends="run-client">
  <!--antcall target="restore"/-->
 </target>

 <target name="run-client" depends="compile-client,run-client-windows" />

 <!-- 运行测试类,为项目的最后一个步骤--> 
 <target name="run-client-windows">
  <exec executable="${javaee.home}/bin/appclient.bat" dir="${classesdir}">
   <arg value="client.Client" />
  </exec>
 </target>

 <!-- 编译测试webservice的客户类,为项目的第三个步骤,本步骤的输出文件为
  ${classesdir}/client/Client.class
 -->
   
 <target name="compile-client" depends="get-artifacts">
  <javac srcdir="./src/client" destdir="${classesdir}">
   <classpath refid="classpath" />
  </javac>
 </target>

 <target name="get-artifacts" depends="compile-deploy-service,get-artifacts-windows"/>
   
 <!-- 本步骤的目的是生成客户端的stub文件,是项目的第二个步骤,本步骤的输出文件为
 在${classesdir}下面自动生成了如下的文件
  GetHello.java
  GetHelloResponse.java
  Hello.java
  HelloService.java
  ObjectFactory.java
  package-info.java
  package-info.class
  GetHello.class
  GetHelloResponse.class
  Hello.class
  HelloService.class
  ObjectFactory.class
 -->   
 <target name="get-artifacts-windows">
  <exec executable="${javaee.home}/bin/wsimport.bat">
   <arg line="-keep -d ${classesdir} http://${javaee.server.name}:${javaee.server.port}/Hello/HelloService?WSDL" />
  </exec>
 </target>

 <!-- 本步骤的目的是编译服务器类,并自动产生wsdl.是项目的第一个步骤,本步骤的主要输出文件为
  ${autodeploydir}\endpoint\Hello.class
  ${autodeploydir}\domain1\autodeploy\endpoint\Hello.class_deployed
  
  在d:\Sun\SDK\domains\domain1\applications\j2ee-modules
  下建立一个endpoint_Hello目录,
  
  
  在d:\Sun\SDK\domains\domain1\generated\ejb\j2ee-modules建立一个
  endpoint_Hello目录,这个目录下又建立了一个endpoint\jaxws目录,里面有如下内容
   GetHello.java
   GetHelloResponse.java
   GetHello.class 
   GetHelloResponse.class
   
  在D:\Sun\SDK\domains\domain1\generated\xml\j2ee-modules\下建立一个
  endpoint_Hello目录,这个目录下又有一个WEB-INF子目录,内容为
   wsdl子目录
   
   sun-web.xml  文件
   web.xml      文件
   webservices.xml  文件
   wsdl子目录下又有两个文件
    HelloService.wsdl
    HelloService_schema1.xsd
  当我们在浏览器输入http://localhost:8080/Hello/HelloService?WSDL时,显示的正是
  这个domains\domain1\generated\xml\j2ee-modules\endpoint_Hello\WEB-INF\wsdl
  文件的内容 
 -->   
   
 <target name="compile-deploy-service">
  <mkdir dir="${classesdir}" />
  <echo message="${javaee.home}" />
  <javac
            srcdir="./src"
            includes="endpoint/**"          
            destdir="${autodeploydir}"    
            classpath="${javaee.home}/lib/j2ee.jar"
        />
  <waitfor maxwait="100" maxwaitunit="second">
   <or>
    <available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
    <available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
   </or>
  </waitfor>
  <condition property="deploy_succeeded">
   <available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
  </condition>
  <condition property="deploy_failed">
   <available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
  </condition>
 </target>

    
 <!-- 以下的任务用于清理和卸载-->    
 <target name="clean">
  <delete dir="${classesdir}"/>
 </target>

 <target name="restore">
  <delete>
   <fileset dir="${autodeploydir}/endpoint" includes="Hello*.*"/>
  </delete>
 </target>

 <target name="undeploy">
  <antcall target="restore"/>
 </target>
</project>

3、运行结果
在myeclipse里执行这个刚才编写的buildtest.xml脚本,console里的输出为
Buildfile: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\buildtest.xml
compile-deploy-service:
    [mkdir] Created dir: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
     [echo] d:/Sun/SDK
    [javac] Compiling 1 source file to D:\Sun\SDK\domains\domain1\autodeploy
get-artifacts-windows:
get-artifacts:
compile-client:
    [javac] Compiling 1 source file to D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
run-client-windows:
     [exec] Hello result = Hello Administrator!
run-client:
all:
BUILD SUCCESSFUL
Total time: 50 seconds

也执行成功


/*title: web service入门学习笔记(六)
**date: 2007/01/19
**author:laomai
**url: http://blog.csdn.net/laomai/
*/

六、一个通用的ant脚本
1、脚本内容
  上面的buildtest.xml脚本虽然对原始的build.xml做了精简,但是还不够通用,
比如服务器的类名和方法名等等都写死了,显然还不适合我们的需要,于是我又写了一个相对通用的脚本
mybuild.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?>

<project name="hello-jaxws" default="all" basedir=".">
 <!-- javaee安装目录的设置,代替了原来脚本中的导入语句 -->
 <property name="javaee.home" value = "d:/Sun/SDK"/>
 <property name="javaee.domaindir" value="${javaee.home}/domains/domain1"/>
 <property name="javaee.server.name" value="localhost"/>
 <property name="javaee.server.port" value="8080"/>

 <!-- 设置发布目录和类的输出目录 -->
 <property name="autodeploydir" value="${javaee.domaindir}/autodeploy"/>
 <property name="classesdir" value="./build"/>

 <!-- 设置java类库路径 -->
 <path id="classpath">
  <pathelement location="${javaee.home}/lib/j2ee.jar"/>
  <pathelement location="${classesdir}"/>
 </path>

 
 <!-- 提供服务的类名 -->
 <property name="serviceclass" value="Hello" />
 <property name="serverclasspath" value="endpoint" /> <!-- 服务器所在的package名称
               也就是Hello.java文件中第一句
               package endpoint;语句中的endpoint
               -->
 <!-- 调用webservice的测试类名-->
 <property name = "clientclass" value="Client"/>
 <property name = "clientclasspath" value="client"/>  <!-- 测试类所在的package名称,
                   也就是Client.java文件中第一句
                  package client;语句中的client;
                  -->
 <!-- 生成的wsdl文件的uri的格式,注意value的值分成两行写时有问题,
  这里为了阅读方便而分行,拷贝时必须放到一行
  我还没找到ant脚本里的续行符是什么:-(
 -->
 <property name="wsdluri"
     value="http://${javaee.server.name}:${javaee.server.port}
     /${serviceclass}/${serviceclass}Service?WSDL" />

 <target name="all" depends="run-client">
  <!-- antcall target="restore"/ -->
 </target>

 <target name="run-client" depends="compile-client,run-client-windows" />

 <!-- 运行测试类,为项目的最后一个步骤-->
 <target name="run-client-windows">
  <exec executable="${javaee.home}/bin/appclient.bat" dir="${classesdir}">
   <arg value="${clientclasspath}.${clientclass}" />
  </exec>
 </target>

 <!-- 编译测试webservice的客户类,为项目的第三个步骤,本步骤的输出文件为
  ${classesdir}/client/Client.class
 -->

 <target name="compile-client" depends="get-artifacts">
  <javac srcdir="./src/${clientclasspath}" destdir="${classesdir}">
   <classpath refid="classpath" />
  </javac>
 </target>

 <target name="get-artifacts" depends="compile-deploy- ②asadmin deploy --user admin --password laomailaomai --host localhost --port 4848 --contextroot hello --upload=true --target server
service,get-artifacts-windows"/>

 <!-- 生成客户端的stub类 -->
 <target name="get-artifacts-windows">
  <exec executable="${javaee.home}/bin/wsimport.bat">
   <arg line="-keep -d ${classesdir} ${wsdluri}" />
  </exec>
 </target>

 <!-- 本步骤的目的是编译服务器类,并自动产生wsdl文件-->
 <target name="compile-deploy-service">
  <echo message="${javaee.home}" />
  <javac
            srcdir="./src"
            includes="${serverclasspath}/**"          
            destdir="${autodeploydir}"    
            classpath="${javaee.home}/lib/j2ee.jar"
        />
  <waitfor maxwait="100" maxwaitunit="second">
   <or>
    <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployed"/>
    <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployFailed"/>
   </or>
  </waitfor>
  <condition property="deploy_succeeded">
   <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployed"/>
  </condition>
  <condition property="deploy_failed">
   <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployFailed"/>
  </condition>
 </target>

 <!-- 以下的任务用于清理和卸载-->
 <target name="clean">
  <delete dir="${autodeploydir}/${serverclasspath}"/>
  <delete dir="${classesdir}/${serverclasspath}"/>
  <delete dir="${classesdir}/${clientclasspath}"/>
 </target>
</project>

2、脚本的使用方式
以后再写自己service时,只要按实际情况改变以下4条语句中的value值就可以了
<property name="serviceclass" value="Hello" /> 
<property name="serverclasspath" value="endpoint" />
<property name = "clientclass" value="Client"/>
<property name = "clientclasspath" value="client"/> 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics