GT4编程实例

1 开始前

该文档针对Windows平台已经安装完ws-core-4.0.x的用户。在继续进行之前保证已经设置环境变量GLOBUS_LOCATION,指向ws-core-4.0.x的安装目录(在我的系统中为D:\ws-core-4.0.0)。同时,为了方便,最好将D:\ws-core-4.0.0\bin目录添加到系统目录PATH中。这样可以方便我们执行Globus自带的一些命令。

此外,还需要安装Python脚本执行程序。目前最新的版本是2.4.1,可以到服务器Softwares目录下去下载。另外,还需要正确安装并配置Ant,它将负责生成最终的GAR文件。同时,文档中的EXAMPLES_DIR指代例子的安装目录。

2 最简单的例子

要编写、部署这样一个Web Service需要一下5个简单的步骤:

l 通过编写WSDL文件,定义服务的接口

l 通过编写Java代码,实现上述接口定义的服务

l 编写WSDD部署描述符和JNDI

l 使用ant编译并形成GAR(Globus ARchive)文件

l 通过GT4自带的工具部署服务

下面,我们分别介绍以上五个步骤:

2.1 定义服务接口

通过编写WSDL文件,来定义我们提供服务的接口。这样,客户端通过解析WSDL文件,从而了解服务的具体定义和调用情况。

2.1.1 文件代码

具体的文件(Math.wsdl)如下:

<?xml version="1.0" encoding="UTF-8"?>

<definitions name="MathService"

    targetNamespace="http://www.globus.org/namespaces/examples/core/MathService_instance"

    xmlns="http://schemas.xmlsoap.org/wsdl/"

    xmlns:tns="http://www.globus.org/namespaces/examples/core/MathService_instance"

    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

    xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd"

    xmlns:wsrpw="http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl"

    xmlns:wsdlpp="http://www.globus.org/namespaces/2004/10/WSDLPreprocessor"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema">



<wsdl:import

    namespace=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl"

    location="../../wsrf/properties/WS-ResourceProperties.wsdl" />





<!--============================================================



                      T Y P E S



 ============================================================-->

<types>

<xsd:schema targetNamespace="http://www.globus.org/namespaces/examples/core/MathService_instance"

    xmlns:tns="http://www.globus.org/namespaces/examples/core/MathService_instance"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema">





    <!-- REQUESTS AND RESPONSES -->



    <xsd:element name="add" type="xsd:int"/>

    <xsd:element name="addResponse">

       <xsd:complexType/>

    </xsd:element>



    <xsd:element name="subtract" type="xsd:int"/>

    <xsd:element name="subtractResponse">

       <xsd:complexType/>

    </xsd:element>



    <xsd:element name="getValueRP">

       <xsd:complexType/>

    </xsd:element>

    <xsd:element name="getValueRPResponse" type="xsd:int"/>





    <!-- RESOURCE PROPERTIES -->



    <xsd:element name="Value" type="xsd:int"/>

    <xsd:element name="LastOp" type="xsd:string"/>



    <xsd:element name="MathResourceProperties">

    <xsd:complexType>

       <xsd:sequence>

           <xsd:element ref="tns:Value" minOccurs="1" maxOccurs="1"/>

           <xsd:element ref="tns:LastOp" minOccurs="1" maxOccurs="1"/>

       </xsd:sequence>

    </xsd:complexType>

    </xsd:element>



</xsd:schema>

</types>





<!--============================================================



                       M E S S A G E S



 ============================================================-->

<message name="AddInputMessage">

    <part name="parameters" element="tns:add"/>

</message>

<message name="AddOutputMessage">

    <part name="parameters" element="tns:addResponse"/>

</message>



<message name="SubtractInputMessage">

    <part name="parameters" element="tns:subtract"/>

</message>

<message name="SubtractOutputMessage">

    <part name="parameters" element="tns:subtractResponse"/>

</message>



<message name="GetValueRPInputMessage">

    <part name="parameters" element="tns:getValueRP"/>

</message>

<message name="GetValueRPOutputMessage">

    <part name="parameters" element="tns:getValueRPResponse"/>

</message>







<!--============================================================



                       P O R T T Y P E



 ============================================================-->

<portType name="MathPortType"

    wsdlpp:extends="wsrpw:GetResourceProperty"

    wsrp:ResourceProperties="tns:MathResourceProperties">



    <operation name="add">

       <input message="tns:AddInputMessage"/>

       <output message="tns:AddOutputMessage"/>

    </operation>



    <operation name="subtract">

       <input message="tns:SubtractInputMessage"/>

       <output message="tns:SubtractOutputMessage"/>

    </operation>



    <operation name="getValueRP">

       <input message="tns:GetValueRPInputMessage"/>

       <output message="tns:GetValueRPOutputMessage"/>

    </operation>



</portType>



</definitions>

该文件被保存在EXAMPLES_DIR\schema\examples\MathService_instance目录下。

2.1.2 名字空间的映射

通过下面文件(EXAMPLES_DIR\namespace2package.mappings)来完成WSDL中名字空间的映射:

http\://www.globus.org/namespaces/examples/core
/MathService_instance=org.globus.examples.stubs.MathService_instance


http\://www.globus.org/namespaces/examples/core/MathService_instance
/bindings=org.globus.examples.stubs.MathService_instance.bindings


http\://www.globus.org/namespaces/examples/core/MathService_instance
/service=org.globus.examples.stubs.MathService_instance.service

将以上代码放入namespace2package.mappings文件,在放入指定文件夹

2.2 实现服务

下面就实现前面接口中定义的。

2.2.1 QName接口

我们会频繁的通过服务的Qualified Name来访问给服务,所以将这个Qualified Name
定义到一个接口(EXAMPLES_DIR \org\globus\examples\services\core\first\impl)中:

// MathQNames.java

package org.globus.examples.services.core.first.impl;



import javax.xml.namespace.QName;



public interface MathQNames {

    public static final String NS = "http://www.globus.org/namespaces/examples/core/MathService_instance";



    public static final QName RP_VALUE = new QName(NS, "Value");



    public static final QName RP_LASTOP = new QName(NS, "LastOp");



    public static final QName RESOURCE_PROPERTIES = new QName(NS,

            "MathResourceProperties");

}

2.2.2 服务的实现

在这个最简单的例子中,我们将所有的有关服务(service)和资源(resource)的代码都放入一个类中。但是,通常情况下,会有两个类文件。一个用来存放有关服务的代码,一个用来存放有关资源的代码。

类的代码如下:

package org.globus.examples.services.core.first.impl;



import java.rmi.RemoteException;



import org.globus.wsrf.Resource;

import org.globus.wsrf.ResourceProperties;

import org.globus.wsrf.ResourceProperty;

import org.globus.wsrf.ResourcePropertySet;

import org.globus.wsrf.impl.ReflectionResourceProperty;

import org.globus.wsrf.impl.SimpleResourcePropertySet;

import org.globus.examples.stubs.MathService_instance.AddResponse;

import org.globus.examples.stubs.MathService_instance.SubtractResponse;

import org.globus.examples.stubs.MathService_instance.GetValueRP;



public class MathService implements Resource, ResourceProperties {



    /* Resource Property set */

    private ResourcePropertySet propSet;



    /* Resource properties */

    private int value;

    private String lastOp;



    /* Constructor. Initializes RPs */

    public MathService() throws RemoteException {

        /* Create RP set */

        this.propSet = new SimpleResourcePropertySet(

                MathQNames.RESOURCE_PROPERTIES);



        /* Initialize the RP's */

        try {

            ResourceProperty valueRP = new ReflectionResourceProperty(

                    MathQNames.RP_VALUE, "Value", this);

            this.propSet.add(valueRP);

            setValue(0);



            ResourceProperty lastOpRP = new ReflectionResourceProperty(

                    MathQNames.RP_LASTOP, "LastOp", this);

            this.propSet.add(lastOpRP);

            setLastOp("NONE");

        } catch (Exception e) {

            throw new RuntimeException(e.getMessage());

        }

    }



    /* Get/Setters for the RPs */

    public int getValue() {

        return value;

    }



    public void setValue(int value) {

        this.value = value;

    }



    public String getLastOp() {

        return lastOp;

    }



    public void setLastOp(String lastOp) {

        this.lastOp = lastOp;

    }



    /* Remotely-accessible operations */



    public AddResponse add(int a) throws RemoteException {

        value += a;

        lastOp = "ADDITION";



        return new AddResponse();

    }



    public SubtractResponse subtract(int a) throws RemoteException {

        value -= a;

        lastOp = "SUBTRACTION";



        return new SubtractResponse();

    }



    public int getValueRP(GetValueRP params) throws RemoteException {

        return value;

    }



    /* Required by interface ResourceProperties */

    public ResourcePropertySet getResourcePropertySet() {

        return this.propSet;

    }

}

2.3 在Web服务部署描述符中配置服务

我们需要把些好的Web服务部署到Web容器中去,才能让客户端对服务进行访问。这一步我们是通过Web服务部署描述符(WSDD)来实现的。

2.3.1 WSDD(Web Service Deploy Descriptor)

<?xml version="1.0" encoding="UTF-8"?>

<deployment name="defaultServerConfig"

    xmlns="http://xml.apache.org/axis/wsdd/"

    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema">



    <service name="examples/core/first/MathService" provider="Handler" use="literal" style="document">

        <parameter name="className" value="org.globus.examples.services.core.first.impl.MathService"/>

        <wsdlFile>share/schema/examples/MathService_instance/Math_service.wsdl</wsdlFile>

        <parameter name="allowedMethods" value="*"/>

        <parameter name="handlerClass" value="org.globus.axis.providers.RPCProvider"/>

        <parameter name="scope" value="Application"/>

        <parameter name="providers" value="GetRPProvider"/>

        <parameter name="loadOnStartup" value="true"/>

    </service>



</deployment>

我们将这个文件(deploy-server.wsdd)放于EXAMPLES_DIR\ org\globus\examples\services\core\first目录下。

2.3.2 JNDI

虽然这里面我们使用最简单的一个例子,但是JNDI文件还是必须的,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<jndiConfig xmlns="http://wsrf.globus.org/jndi/config">



<service name="examples/core/first/MathService">

    <resource name="home" type="org.globus.wsrf.impl.ServiceResourceHome">

    <resourceParams>



       <parameter>

           <name>factory</name>

           <value>org.globus.wsrf.jndi.BeanFactory</value>

       </parameter>



    </resourceParams>



    </resource>

</service>



</jndiConfig>

2.4 创建GAR文件

在这里,我们需要Ant和Python的支持。首先,简单的将源文件中的build.xml、build.mappings以及globus-build-service.py三个文件拷贝到EXAMPLES_DIR目录下。在命令行上加入如下命令创建GAR文件:

globus-build-service.py first

如果没有出现错误,会在根目录下面创建一个名为org_globus_examples_services_core_first.gar的文件。

2.5 部署服务到容器中去

此时,部署服务的工具是由GT4提供的。因为我们已经将%GLOBUS_LOCATION\bin目录加入的系统路径中去,所以我们可以在服务台上直接输入下面命令,就可以方便地将上面编写的服务部署到服务器上去:

globus-deploy-gar org_globus_examples_services_core_first.gar

2.6 小结

通过以上五步,我们就开发了最简单的一个基于GT4的Web服务。下面,我们将通过编写简单的例子对该服务进行测试。

3 测试

3.1 编写客户端程序

我们将编写一个简单的客户端程序,并将其放在EXAMPLES_DIR \org\globus\examples\clients\MathService_instance\Client.java。它的全部代码如下

package org.globus.examples.clients.MathService_instance;



import org.apache.axis.message.addressing.Address;

import org.apache.axis.message.addressing.EndpointReferenceType;



import org.globus.examples.stubs.MathService_instance.MathPortType;

import org.globus.examples.stubs.MathService_instance.GetValueRP;

import org.globus.examples.stubs.MathService_instance.service.MathServiceAddressingLocator;



public class Client {



    public static void main(String[] args) {

       MathServiceAddressingLocator locator = new MathServiceAddressingLocator();



       try {

           String serviceURI = args[0];



           // Create endpoint reference to service

           EndpointReferenceType endpoint = new EndpointReferenceType();

           endpoint.setAddress(new Address(serviceURI));

           MathPortType math = locator.getMathPortTypePort(endpoint);



           // Get PortType

           //math = locator.getMathPortTypePort(endpoint);



           // Perform an addition

           math.add(10);



           // Perform another addition

           math.add(5);



           // Access value

           System.out.println("Current value: "

                  + math.getValueRP(new GetValueRP()));



           // Perform a subtraction

           math.subtract(5);



           // Access value

           System.out.println("Current value: "

                  + math.getValueRP(new GetValueRP()));

       } catch (Exception e) {

           e.printStackTrace();

       }

    }



}

3.2 编译客户端程序

3.2.1 设置环境变量

在编译客户端程序之前,我们需要将GT4自带的类库导入到系统变量CLASSPATH下。为此,GT4给我们提供了一个批处理文件,我们只需要简单的在GLOBUS_LACATION\etc目录下执行如下命令即可:

globus-devel-setcp.bat

3.2.2 编译

接下来,到EXAMPLES_DIR目录下,执行下面的命令来编译客户端程序:
set classpath=./build/stubs/classes/;%CLASSPATH%
javac org\globus\examples\clients\MathService_instance\Client.java

注:在PDF文档中给出的批处理命令是Unix/Linux系统下面的,需要对其进行更改才可以在Windows系统下正确执行!

3.2.3 启动容器

最后,我们需要启动容器。这时,最好另外开启一个命令行窗口,再输入如下命令:

globus-start-container –nosec
注:默认容器需要占用8080端口,如果系统上安装了Tomcat这样应用服务器,也有可能也占用了该端口。所以可以将Tomcat服务暂时关闭。

3.3 测试结果

如果发现容器中多了一个自己的web service那就说明添加成功了
此时,在原先的那个命令行下输入如下命令来执行我们写的客户端程序:

java  org.globus.examples.clients.MathService_instance.Client               http://127.0.0.1:8080/wsrf/services/examples/core/first/MathService

如果我们连续执行两次上面的命令,会输出如下结果:

D:\test>java -classpath ./build/stubs/classes/;%CLASSPATH% org.globus.examples.c

lients.MathService_instance.Client http://127.0.0.1:8080/wsrf/services/examples/

core/first/MathService

Current value: 15

Current value: 10

D:\test>java -classpath ./build/stubs/classes/;%CLASSPATH% org.globus.examples.c

lients.MathService_instance.Client http://127.0.0.1:8080/wsrf/services/examples/

core/first/MathService

Current value: 25

Current value: 20

3.4 小结

通过上面的测试,我们可以验证Web服务的正确性。同时要注意Windows和Unix/Linxu系统下命令书写的差别

此链接为源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习是一种人工智能(AI)的子领域,致力于研究如何利用数据和算法让计算机系统具备学习能力,从而能够自动地完成特定任务或者改进自身性能。机器学习的核心思想是让计算机系统通过学习数据中的模式和规律来实现目标,而不需要显式地编程。 机器学习应用非常广泛,包括但不限于以下领域: 图像识别和计算机视觉: 机器学习在图像识别、目标检测、人脸识别、图像分割等方面有着广泛的应用。例如,通过深度学习技术,可以训练神经网络来识别图像中的对象、人脸或者场景,用于智能监控、自动驾驶、医学影像分析等领域。 自然语言处理: 机器学习在自然语言处理领域有着重要的应用,包括文本分类、情感分析、机器翻译、语音识别等。例如,通过深度学习模型,可以训练神经网络来理解和生成自然语言,用于智能客服、智能助手、机器翻译等场景。 推荐系统: 推荐系统利用机器学习算法分析用户的行为和偏好,为用户推荐个性化的产品或服务。例如,电商网站可以利用机器学习算法分析用户的购买历史和浏览行为,向用户推荐感兴趣的商品。 预测和预测分析: 机器学习可以用于预测未来事件的发生概率或者趋势。例如,金融领域可以利用机器学习算法进行股票价格预测、信用评分、欺诈检测等。 医疗诊断和生物信息学: 机器学习在医疗诊断、药物研发、基因组学等领域有着重要的应用。例如,可以利用机器学习算法分析医学影像数据进行疾病诊断,或者利用机器学习算法分析基因数据进行疾病风险预测。 智能交通和物联网: 机器学习可以应用于智能交通系统、智能城市管理和物联网等领域。例如,可以利用机器学习算法分析交通数据优化交通流量,或者利用机器学习算法分析传感器数据监测设备状态。 以上仅是机器学习应用的一部分,随着机器学习技术的不断发展和应用场景的不断拓展,机器学习在各个领域都有着重要的应用价值,并且正在改变我们的生活和工作方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值