Apache Camel - 14 - CXF组件

Apache Camel CXF(WebService)组件

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo

如果觉得还行,麻烦点个Star

 

目前国内,关于Apache Camel 集成CXF(WebService)的样例,似乎没有一个完整的Demo。

关于Apache Camel 集成 CXF(WebService)的项目工程,后面等我整理好了以后,会上传,供网友参考...

最近出差,比较忙,没空整理

我这里假设 你之前是用过CXF 或者WebService的...

如果你没有看过,请移步《WebService(4)_标准JDK发布WebService》系列文章,先尝试使用下CXF/WebService...

Option

Default Value

Description

wsdlURL

Retrieved from the endpoint address

Specifies the location of the WSDL contract file.

serviceClass

null

Specifies the name of the service endpoint interface (SEI). Typically this interface will have JAX-WS annotations. The SEI is required if the CXF data format mode is POJO. If you already have an instance of a concrete class, you can reference it using the #beanName style.

serviceName

Obtained from the WSDL

Specifies the service to use. The format is a qualified name(QName) that has a namespace and name like {http://order.camelinaction}OrderEndpointService.

Note that if there is only one service in a WSDL, Camel will choose this as the default service. If there is more than one service defined, you need to set the serviceName property.

portName

Obtained from the WSDL

Specifies the port to use. The format is a qualified name(QName) that has a namespace and name like {http://order.camelinaction}OrderService.

dataFormat

POJO

Note that if there is only one port in a WSDL, Camel will choose this as the default port. If there is more than one port defined per service, you need to set the portName property.

 

Sets the data format type that CXF uses for its messages. The possible values are POJO, PAYLOAD, and MESSAGE. We’ll only be covering POJO mode in this chapter; you can find more information on the other two modes on the CXF component page in the Camel online documentation: http://camel.apache.org/cxf.html.

Apache Camel CXF组件发布WebService一共有两种方式。

我这里演示的是 利用wsdl文档,发布webservice。

《Camel in Action》中还有一种是代码先行,一直没有测试成功,暂时不看。

官方Demo 样例:

apache-camel-2.18.3\examples\camel-example-cxf

官方样例的思路比较复杂,涉及到CXF WebService 和 MQ,我只是想简单测试一下而已。

我把多余的代码全部去掉之后,就剩下三个类和一个wsdl文件。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
	license agreements. See the NOTICE file distributed with this work for additional 
	information regarding copyright ownership. The ASF licenses this file to 
	You under the Apache License, Version 2.0 (the "License"); you may not use 
	this file except in compliance with the License. You may obtain a copy of 
	the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
	by applicable law or agreed to in writing, software distributed under the 
	License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
	OF ANY KIND, either express or implied. See the License for the specific 
	language governing permissions and limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<groupId>org.apache.camel.example</groupId>
	<artifactId>examples</artifactId>
	<version>2.20.1</version>
	<packaging>jar</packaging>

	<name>ApacheCamelTest</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>

		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- camel -->
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jetty</artifactId>
			<version>2.20.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-test</artifactId>
			<version>2.20.0</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-cxf</artifactId>
			<version>2.20.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.20.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-cxf-transport</artifactId>
			<version>2.20.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-http</artifactId>
			<version>2.20.0</version>
		</dependency>


		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-http-common</artifactId>
			<version>2.20.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-bindings-soap</artifactId>
			<version>3.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.10</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.10</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.1.10</version>
		</dependency>

		<!-- <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jsonpath</artifactId> 
			<version>2.14.0</version> </dependency> -->

		<!-- Logger -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.4</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
			<version>1.6.1</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.5</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.6</version>
		</dependency>

		<!-- JSON -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20171018</version>
		</dependency>

		<dependency>
			<groupId>com.jayway.jsonpath</groupId>
			<artifactId>json-path</artifactId>
			<version>2.2.0</version>
		</dependency>

		<!-- httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.2</version>
		</dependency>


	</dependencies>



	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>

这个接口,在官方Demo中是没有的,我根据接口实现类写的...

 

package org.apache.hello_world_soap_http;

public interface Greeter {

	String greetMe(String param);

	void greetMeOneWay(String param);

	String sayHi();

	void pingMe(String param);
}

修改之后wsdl文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
	license agreements. See the NOTICE file distributed with this work for additional 
	information regarding copyright ownership. The ASF licenses this file to 
	you under the Apache License, Version 2.0 (the "License"); you may not use 
	this file except in compliance with the License. You may obtain a copy of 
	the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
	by applicable law or agreed to in writing, software distributed under the 
	License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
	OF ANY KIND, either express or implied. See the License for the specific 
	language governing permissions and limitations under the License. -->

<wsdl:definitions name="HelloWorld"
	targetNamespace="http://apache.org/hello_world_soap_http" xmlns="http://schemas.xmlsoap.org/wsdl/"
	xmlns:jms="http://cxf.apache.org/transports/jms" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:tns="http://apache.org/hello_world_soap_http"
	xmlns:x1="http://apache.org/hello_world_soap_http/types" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<wsdl:types>
		<schema targetNamespace="http://apache.org/hello_world_soap_http/types"
			xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://apache.org/hello_world_soap_http/types"
			elementFormDefault="qualified">
			<simpleType name="MyStringType">
				<restriction base="string">
					<maxLength value="30" />
				</restriction>
			</simpleType>

			<element name="sayHi">
				<complexType />
			</element>
			<element name="sayHiResponse">
				<complexType>
					<sequence>
						<element name="responseType" type="string" />
					</sequence>
				</complexType>
			</element>
			<element name="greetMe">
				<complexType>
					<sequence>
						<element name="requestType" type="tns:MyStringType" />
					</sequence>
				</complexType>
			</element>
			<element name="greetMeResponse">
				<complexType>
					<sequence>
						<element name="responseType" type="string" />
					</sequence>
				</complexType>
			</element>
			<element name="greetMeOneWay">
				<complexType>
					<sequence>
						<element name="requestType" type="string" />
					</sequence>
				</complexType>
			</element>
			<element name="pingMe">
				<complexType>
					<sequence>
						<element name="faultType" type="string" />
					</sequence>
				</complexType>
			</element>
			<element name="pingMeResponse">
				<complexType />
			</element>
			<element name="faultDetail">
				<complexType>
					<sequence>
						<element name="minor" type="short" />
						<element name="major" type="short" />
					</sequence>
				</complexType>
			</element>
		</schema>
	</wsdl:types>
	<wsdl:message name="sayHiRequest">
		<wsdl:part element="x1:sayHi" name="in" />
	</wsdl:message>
	<wsdl:message name="sayHiResponse">
		<wsdl:part element="x1:sayHiResponse" name="out" />
	</wsdl:message>
	<wsdl:message name="greetMeRequest">
		<wsdl:part element="x1:greetMe" name="in" />
	</wsdl:message>
	<wsdl:message name="greetMeResponse">
		<wsdl:part element="x1:greetMeResponse" name="out" />
	</wsdl:message>
	<wsdl:message name="greetMeOneWayRequest">
		<wsdl:part element="x1:greetMeOneWay" name="in" />
	</wsdl:message>
	<wsdl:message name="pingMeRequest">
		<wsdl:part name="in" element="x1:pingMe" />
	</wsdl:message>
	<wsdl:message name="pingMeResponse">
		<wsdl:part name="out" element="x1:pingMeResponse" />
	</wsdl:message>
	<wsdl:message name="pingMeFault">
		<wsdl:part name="faultDetail" element="x1:faultDetail" />
	</wsdl:message>

	<wsdl:portType name="Greeter">
		<wsdl:operation name="sayHi">
			<wsdl:input message="tns:sayHiRequest" name="sayHiRequest" />
			<wsdl:output message="tns:sayHiResponse" name="sayHiResponse" />
		</wsdl:operation>

		<wsdl:operation name="greetMe">
			<wsdl:input message="tns:greetMeRequest" name="greetMeRequest" />
			<wsdl:output message="tns:greetMeResponse" name="greetMeResponse" />
		</wsdl:operation>

		<wsdl:operation name="greetMeOneWay">
			<wsdl:input message="tns:greetMeOneWayRequest" name="greetMeOneWayRequest" />
		</wsdl:operation>

		<wsdl:operation name="pingMe">
			<wsdl:input name="pingMeRequest" message="tns:pingMeRequest" />
			<wsdl:output name="pingMeResponse" message="tns:pingMeResponse" />
			<wsdl:fault name="pingMeFault" message="tns:pingMeFault" />
		</wsdl:operation>

	</wsdl:portType>
	<wsdl:binding name="Greeter_SOAPBinding" type="tns:Greeter">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />

		<wsdl:operation name="sayHi">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="sayHiRequest">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="sayHiResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="greetMe">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="greetMeRequest">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="greetMeResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="greetMeOneWay">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="greetMeOneWayRequest">
				<soap:body use="literal" />
			</wsdl:input>
		</wsdl:operation>

		<wsdl:operation name="pingMe">
			<soap:operation style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
			<wsdl:fault name="pingMeFault">
				<soap:fault name="pingMeFault" use="literal" />
			</wsdl:fault>
		</wsdl:operation>

	</wsdl:binding>

	<wsdl:service name="SOAPService">

		<wsdl:port binding="tns:Greeter_SOAPBinding" name="SoapOverHttpRouter">
			<soap:address location="http://localhost:9001/SoapContext/SoapPort" />
		</wsdl:port>

	</wsdl:service>
</wsdl:definitions>

主类

 

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.example.cxf.httptojms;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.PropertyConfigurator;

/**
* An example for demonstrating how Camel works as a Router. This example shows
* how Camel can route a SOAP client's HTTP request to a SOAP over JMS Service.
*/
public final class CamelCxfExample {
	private static final String ROUTER_ADDRESS = "http://localhost:{{routerPort}}/SoapContext/SoapPort";
	private static final String SERVICE_CLASS = "serviceClass=org.apache.hello_world_soap_http.Greeter";
	private static final String WSDL_LOCATION = "wsdlURL=wsdl/hello_world.wsdl";
	private static final String SOAP_OVER_HTTP_ROUTER = "portName={http://apache.org/hello_world_soap_http}SoapOverHttpRouter";

	private static final String ROUTER_ENDPOINT_URI = 
	"cxf://" + ROUTER_ADDRESS + "?" + SERVICE_CLASS + "&" + WSDL_LOCATION + "&" + SOAP_OVER_HTTP_ROUTER + "&dataFormat=POJO";

	private CamelCxfExample() {
	}

	public static void main(String args[]) throws Exception {

	PropertyConfigurator.configure("./conf/log4j.properties");
	PropertyConfigurator.configureAndWatch("./conf/log4j.properties", 1000);

	System.setProperty("routerPort", "9001");
	System.setProperty("servicePort", "9003");

	CamelContext context = new DefaultCamelContext();
	PropertiesComponent pc = new PropertiesComponent();
	context.addComponent("properties", pc);
	try {

	context.addRoutes(new RouteBuilder() {
			public void configure() {

	from(ROUTER_ENDPOINT_URI).to("log:CamelCxfExample?showExchangeId=true");
	}
	});

	String address = ROUTER_ADDRESS.replace("{{routerPort}}", System.getProperty("routerPort"));
	System.out.println("address : " + address);

	context.start();

	synchronized (CamelCxfExample.class) {
		CamelCxfExample.class.wait();
	}
	} catch (Exception ex) {
		ex.printStackTrace();
	} finally {
		// server.stop();
		System.exit(0);
	}

	}
}

启动程序:

 

根据这个url路径,生成wsdl文档,然后生成客户端测试代码,

我们直接看客户端测试代码:

package com.client;

import org.apache.hello_world_soap_http.Greeter;
import org.apache.hello_world_soap_http.SOAPService;

public class ClientCode {

	public static void main(String[] args) throws Exception {

	SOAPService soapService = new SOAPService();

	Greeter greeter = soapService.getSoapOverHttpRouter();

	String result1 = greeter.greetMe("ccc111");
	greeter.greetMeOneWay("ccc222");
	greeter.pingMe("ccc333");
	String result2 = greeter.sayHi();

	System.out.println("result1 : " + result1);
	System.out.println("result2 : " + result2);

	}

}

运行之后,客户端打印内容:

 

服务端打印内容:

 

没问题...

如果你在from(ROUTER_ENDPOINT_URI)之后,再加上一个process方法,添加上自定义的方法,就可以将客户端发送的数据取出来。

 

测试到这里,如果你仔细看一下主类中的成员变量以及WSDL,你会觉得很有意思。

完全是依赖WSDL进行服务发布的...

 

好,问题来了,WSDL文档还是比较复杂的,在我们还不能很熟练的编写WSDL文档的情况下,该怎么搞呢??

 

我的想法是这样的:

先使用传统的CXF 进行编写接口、实现类以及Endpoint主发布类。

启动主类,发布服务,通过URL,在浏览器上获得wsdl文档,然后我们将该文档,保存到本地。

拿到WSDL文档之后,我们使用CXF的工具,使用命令"wsdl2java -impl"来获取接口代码、接口实现代码。

 

(下面的一个栗子,就是根据我上面的想法来实现的...是切实可行的。)

 

测试到这里,我一直有一个疑问?!

传统的CXF WebService开发的时候,都会有一个接口实现类,客户端调用方法,服务端调用对应的接口实现类,进行业务处理。

为什么在Camel-CXF里,不需要接口实现类??

 

还是我的Demo 样例写的有问题?

于是我重新仔细推敲了一下camel-example-cxf官方Demo。

 

在原Demo中,有这样一段:

 

以及,下面这段,implementor对象是接口实现类,但address确实jms的。

 

于是我大脑发散了一下!!

会不会Camel发布的 from(ROUTER_ENDPOINT_URI)url 仅仅是一个内部的代理...

在Camel 的内 部,依旧是需要启动一个WebService服务的??

 

来,我们试试看...

我们先看下接口:

 

package com.camel.cxf.server;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.1.7
 * 2017-12-31T12:35:16.206+08:00
 * Generated source version: 3.1.7
 * 
 */
@WebService(targetNamespace = "http://server.cxf.camel.com/", name = "CamelCXFServiceInter")
public interface CamelCXFServiceInter {

    @WebMethod
    @RequestWrapper(
		localName = "queryInfomation", 
		targetNamespace = "http://server.cxf.camel.com/", 
		className = "com.camel.cxf.server.QueryInfomation")
    @ResponseWrapper(
		localName = "queryInfomationResponse", 
		targetNamespace = "http://server.cxf.camel.com/", 
		className = "com.camel.cxf.server.QueryInfomationResponse")
    @WebResult(name = "return", targetNamespace = "")
    public java.lang.String queryInfomation(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );

        @WebMethod
        @RequestWrapper(
			localName = "sayHello", 
			targetNamespace = "http://server.cxf.camel.com/", 
			className = "com.camel.cxf.server.SayHello")
        @ResponseWrapper(
			localName = "sayHelloResponse", 
			targetNamespace = "http://server.cxf.camel.com/", 
			className = "com.camel.cxf.server.SayHelloResponse")
        @WebResult(name = "return", targetNamespace = "")
    public java.lang.String sayHello(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );
}

 

 

一共两个方法。

 

接口实现类:

 

package com.camel.cxf.server;

public class CamelCXFServiceImpl implements CamelCXFServiceInter {

	@Override
	public String queryInfomation(String arg0) {
		System.out.println("queryInfomation : " + arg0);
		return "hhhhhh";
	}

	@Override
	public String sayHello(String arg0) {
		System.out.println("sayHello : " + arg0);
		return "xxxxxxxx";
	}

}

 

 

接口实现类中,两个方法,接收到参数之后,会打印接收的参数,然后返回固定的参数给客户端。

wsdl文档

 

<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://server.cxf.camel.com/"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
	name="CamelCXFServiceImplService" targetNamespace="http://server.cxf.camel.com/">
	<wsdl:types>
		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
			xmlns:tns="http://server.cxf.camel.com/" elementFormDefault="unqualified"
			targetNamespace="http://server.cxf.camel.com/" version="1.0">

			<xs:element name="queryInfomation" type="tns:queryInfomation" />

			<xs:element name="queryInfomationResponse" type="tns:queryInfomationResponse" />

			<xs:element name="sayHello" type="tns:sayHello" />

			<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />

			<xs:complexType name="queryInfomation">
				<xs:sequence>
					<xs:element minOccurs="0" name="arg0" type="xs:string" />
				</xs:sequence>
			</xs:complexType>

			<xs:complexType name="queryInfomationResponse">
				<xs:sequence>
					<xs:element minOccurs="0" name="return" type="xs:string" />
				</xs:sequence>
			</xs:complexType>

			<xs:complexType name="sayHello">
				<xs:sequence>
					<xs:element minOccurs="0" name="arg0" type="xs:string" />
				</xs:sequence>
			</xs:complexType>

			<xs:complexType name="sayHelloResponse">
				<xs:sequence>
					<xs:element minOccurs="0" name="return" type="xs:string" />
				</xs:sequence>
			</xs:complexType>

		</xs:schema>
	</wsdl:types>
	<wsdl:message name="sayHelloResponse">
		<wsdl:part element="tns:sayHelloResponse" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="queryInfomation">
		<wsdl:part element="tns:queryInfomation" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="queryInfomationResponse">
		<wsdl:part element="tns:queryInfomationResponse" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="sayHello">
		<wsdl:part element="tns:sayHello" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:portType name="CamelCXFServiceInter">
		<wsdl:operation name="queryInfomation">
			<wsdl:input message="tns:queryInfomation" name="queryInfomation">
			</wsdl:input>
			<wsdl:output message="tns:queryInfomationResponse" name="queryInfomationResponse">
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="sayHello">
			<wsdl:input message="tns:sayHello" name="sayHello">
			</wsdl:input>
			<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
			</wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="CamelCXFServiceImplServiceSoapBinding"
		type="tns:CamelCXFServiceInter">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="queryInfomation">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="queryInfomation">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="queryInfomationResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="sayHello">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="sayHello">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="sayHelloResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="CamelCXFServiceImplService">
		<wsdl:port binding="tns:CamelCXFServiceImplServiceSoapBinding"
			name="CamelCXFServiceImplPort">
			<soap:address location="http://localhost:8088/CamelCXFService/queryService" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

 

 

Camel内部启动webservice接口

 

package com.camel.cxf.server;

import javax.xml.ws.Endpoint;

public class Server {

	public void start() {

	String address = "http://localhost:9022/camel-cxf/greeter-service";
	Endpoint.publish(address, new CamelCXFServiceImpl());
	System.out.println("WebService 发布成功 , address : " + address);

	}

}


下面是主类:

 

 

package com.camel.cxf.server.main;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.CxfComponent;
import org.apache.camel.component.cxf.CxfEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.PropertyConfigurator;

import com.camel.cxf.server.CamelCXFServiceInter;
import com.camel.cxf.server.Server;

public class CamelReleaseServiceMain {

	private static final String ROUTER_ADDRESS = "http://localhost:8088/CamelCXFService/queryService";

	private static final String SERVICE_CLASS = "serviceClass=com.camel.cxf.server.CamelCXFServiceInter";

	private static final String WSDL_LOCATION = "wsdlURL=wsdl/queryService.wsdl";

	private static final String ROUTER_ENDPOINT_URI = 
	"cxf://" + ROUTER_ADDRESS + "?" + SERVICE_CLASS + "&" + WSDL_LOCATION + "&dataFormat=POJO";

	private static final String SERVICE_ADDRESS = "http://localhost:9022/camel-cxf/greeter-service?wsdl";

	public static void main(String[] args) {

	PropertyConfigurator.configure("./conf/log4j.properties");
	PropertyConfigurator.configureAndWatch("./conf/log4j.properties", 1000);

	System.out.println("ROUTER_ENDPOINT_URI : " + ROUTER_ENDPOINT_URI);

	CamelContext context = new DefaultCamelContext();

	Server server = new Server();

	try {

	server.start();

	context.addRoutes(new RouteBuilder() {
			public void configure() {

	CxfComponent cxfComponent = new CxfComponent(getContext());

	CxfEndpoint serviceEndpoint = new CxfEndpoint(SERVICE_ADDRESS, cxfComponent);

	serviceEndpoint.setServiceClass(CamelCXFServiceInter.class);

	from(ROUTER_ENDPOINT_URI).to("log:CamelCxfExample?showExchangeId=true").to(serviceEndpoint);

	// from(ROUTER_ENDPOINT_URI).process(new Processor() {
	//
	// @Override
	// public void process(Exchange exchange) throws Exception {
	// System.out.println("exchange : " + exchange);
	// System.out.println("exchange.getIn() : " +
	// exchange.getIn());
	// System.out.println("exchange.getIn(String.class) : " +
	// exchange.getIn(String.class));
	// System.out.println("exchange.getIn().getBody() : " +
	// exchange.getIn().getBody());
	// }
	// }).to("log:cxfdemocamelexample?showExchangeId=true");
	}
	});

	String address = ROUTER_ADDRESS;

	System.out.println("address : " + address + "?wsdl");
	context.start();

	// 通用没有具体业务意义的代码,只是为了保证主线程不退出
	synchronized (CamelReleaseServiceMain.class) {
		CamelReleaseServiceMain.class.wait();
	}

	} catch (Exception ex) {
		ex.printStackTrace();
	} finally {
		System.exit(0);
	}

	}

}

 

 

你看上面的代码,我先调用server.start()方法,启动内部的webservice。

然后使用同样的接口发布Camel-CXF服务。

接着将内部的webservice,添加到路由中。

 

我们启动程序:

 

然后根据url,得到客户端代码,看下客户端测试代码:

 

package com.camel.cxf.server.test;

import com.camel.cxf.server.CamelCXFServiceImplService;
import com.camel.cxf.server.CamelCXFServiceInter;

public class TestCamelCXFServiceClient {

	public static void main(String[] args) {

	CamelCXFServiceImplService camelCXFServiceImplService = new CamelCXFServiceImplService();

	CamelCXFServiceInter camelCXFServiceInter = camelCXFServiceImplService.getCamelCXFServiceImplPort();

	String result = camelCXFServiceInter.queryInfomation("cyx00000");
	String result2 = camelCXFServiceInter.sayHello("cccccccc");

	System.out.println("result : " + result);
	System.out.println("result2 : " + result2);

	}

}

运行之后,客户端控制台打印:

Camel-CXF控制台打印:

没问题吧...Camel-CXF调用到了接口实现类,进行了业务处理,然后将处理之后的结果返回给客户端了...

我的目的达到了...

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Apache Camel和JBang是两个不同的工具,但可以配合使用。Apache Camel是一个源的集成框架,用于构建面向企业的集成应用程序,而JBang是一个基于Java的脚本引擎。 如果你想在使用Apache Camel时结合JBang进行脚本编写和执行,可以按照以下步骤操作: 1. 首先,确保你已经安装了Java和Apache Camel。你可以从官方网站下载和安装最新版本的Apache Camel。 2. 接下来,你需要安装JBang。JBang可以通过其官方网站或GitHub仓库进行下载和安装。 3. 安装完成后,你可以创建一个新的JBang脚本文件,用于编写和执行Apache Camel的集成路由。你可以使用任何文本编辑器打开该文件,并在其中编写你的Camel路由逻辑。 4. 在脚本文件的开头,添加`#!/usr/bin/env jbang`这一行,以告诉系统使用JBang来执行该脚本。 5. 在脚本文件中,导入所需的Apache Camel类和其他必要的依赖项。你可以使用JBang的内置依赖管理功能来自动下载和管理这些依赖项。 6. 编写你的Apache Camel路由逻辑。你可以使用Camel的Java DSL或XML DSL来定义和配置路由。在脚本中,你可以使用Java代码来编写这些路由逻辑。 7. 最后,保存脚本文件,并在命令行中执行它。你可以使用`jbang <脚本文件名>`命令来运行该脚本。 通过这种方式,你可以使用JBang来编写和执行Apache Camel的集成路由,从而实现各种企业集成场景的需求。注意,这只是一种使用JBang和Apache Camel的方法,你还可以根据自己的需求进行灵活调整和扩展。希望对你有所帮助!如果你有更多问题,可以继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值