- Статьи и примеры
- | Веб сервисы
- | Сравнение скорости работы четырех клиентов веб сервисов
- | Сравнение скорости работы четырех клиентов веб сервисов страница 3
Сравнение скорости работы четырех клиентов веб сервисов Страница 3
Клиент Castor
В этом случае все немножко по-другому. Castor в данном случае будет использоваться для связывания xml с классами Java, а вот интерфейсы будет генерировать все тот же AXIS. Поэтому, во-первых, внесем изменения в WSDL. Вынесем в отдельный XSD файл объекты. В WSDL файле появиться запись:
<import namespace="http://localhost" location="Joker.xsd" />
Для объектов создадим новый файл Joker.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://localhost" xmlns="http://localhost"> <xs:element name='methodRequest'> <xs:complexType> <xs:sequence/> <xs:attribute name='reqString' type='xs:string' use='required'/> </xs:complexType> </xs:element> <xs:element name='methodResponse'> <xs:complexType> <xs:sequence/> <xs:attribute name='resString' type='xs:string' use='required'/> </xs:complexType> </xs:element> </xs:schema>
Далее скачиваем все необходимые библиотеки. AXIS вот отсюда. Все его библиотеки положим в папку axis2. Castor можно взять здесь. Версия 1.2. Кроме того, понадобиться библиотека из проекта Apache Velocity. Использовалась версия 1.4. И, конечно же, Apache Xerces. Версия 2.9.2. В папку lib положим следующие библиотеки:
- lib
- castor-1.2.jar
- castor-1.2-codegen.jar
- castor-1.2-xml-schema.jar
- commons-logging-1.0.3.jar
- velocity-1.5.jar
- xercesImpl.jar
Теперь, когда все приготовления закончены, можно писать антовский скрипт build.xml и генерировать файлы.
<project name="castorJoker" basedir="." default="generate.joker"> <property name="target" value="src"/> <property name="wsdl" value="./Joker.wsdl"/> <path id="classpath"> <fileset dir="./axis2"> <include name="*.jar"/> </fileset> <fileset dir="./lib"> <include name="*.jar"/> </fileset> </path> <target name="generate.joker"> <delete dir="${target}"/> <mkdir dir="${target}"/> <sequential> <java classname="org.exolab.castor.builder.SourceGeneratorMain" fork="true"> <classpath refid="classpath"/> <arg line="-i Joker.xsd -package com.castor.client.data -dest ${target}/src" /> </java> </sequential> <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="classpath"> <arg line="-uri ${wsdl}"/> <arg line="-p com.castor.client"/> <arg line="-d none"/> <arg line="-o ${target}"/> </java> </target> </project>
После того, как файлы готовы, остается добавить тестовый метод. Он будет несколько другого вида, чем два предыдущих. Для чистоты эксперимента вынесем создание всех объектов из метода. Это занимает некоторое время.
SAXOMBuilder builder = new SAXOMBuilder(); Unmarshaller unmarshaller = new Unmarshaller(com.castor.client.data.MethodResponse.class); UnmarshalHandler unmarshalHandler = unmarshaller.createHandler(); com.castor.client.data.MethodResponse methodResponse = new com.castor.client.data.MethodResponse(); com.castor.client.data.MethodRequest methodRequest = new com.castor.client.data.MethodRequest(); ContentHandler contentHandler = Unmarshaller.getContentHandler(unmarshalHandler); private String сastorTest() throws MarshalException, ValidationException, IOException{ methodRequest.setReqString(testString); Marshaller.marshal(methodRequest, builder); OMElement response = stub.shift(builder.getRootElement()); StAXSource staxSource = new StAXSource(response.getXMLStreamReader()); try { TransformerFactory.newInstance().newTransformer().transform(staxSource, new SAXResult(contentHandler)); methodResponse = (com.castor.client.data.MethodResponse) unmarshalHandler.getObject(); } catch (TransformerException e) { e.printStackTrace(); } return java.net.URLDecoder.decode(methodResponse.getResString(),"UTF-8"); }
И последний..