camel是一個路由選擇引擎,具體內(nèi)容可見
http://camel.apache.org還有一本camel in action,對camel核心進行了詳細講解;
這是一個簡單的例子,接收一個webservice消息,并且通過camel把消息路由給一個bean;
需要一些jar包:
1 新建bean:
3 需要建立spring配置文件,放在classpath下
apache-cxf-2.3.3\lib\cxf-2.3.3.jar
apache-cxf-2.3.3\lib\XmlSchema-1.4.7.jar
apache-cxf-2.3.3\lib\neethi-2.0.4.jar
apache-cxf-2.3.3\lib\wss4j-1.5.11.jar
apache-cxf-2.3.3\lib\wsdl4j-1.6.2.jar
apache-cxf-2.3.3\lib\geronimo-servlet_3.0_spec-1.0.jar
apache-cxf-2.3.3\lib\jsr311-api-1.1.1.jar
apache-cxf-2.3.3\lib\jetty-continuation-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-http-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-server-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-util-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-io-7.2.2.v20101205.jar
apache-camel-2.7.1\lib\spring\spring-beans-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-context-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-context-support-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-core-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-expression-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-tx-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\aopalliance-1.0.jar
apache-camel-2.7.1\lib\spring\commons-logging-1.1.1.jar
apache-camel-2.7.1\lib\spring\spring-aop-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-asm-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\slf4j-api-1.6.1.jar
apache-camel-2.7.1\lib\camel-core-2.7.1.jar
apache-camel-2.7.1\lib\commons-management-1.0.jar
apache-camel-2.7.1\lib\camel-cxf-2.7.1.jar
apache-camel-2.7.1\lib\camel-spring-2.7.1.jar
apache-cxf-2.3.3\lib\XmlSchema-1.4.7.jar
apache-cxf-2.3.3\lib\neethi-2.0.4.jar
apache-cxf-2.3.3\lib\wss4j-1.5.11.jar
apache-cxf-2.3.3\lib\wsdl4j-1.6.2.jar
apache-cxf-2.3.3\lib\geronimo-servlet_3.0_spec-1.0.jar
apache-cxf-2.3.3\lib\jsr311-api-1.1.1.jar
apache-cxf-2.3.3\lib\jetty-continuation-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-http-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-server-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-util-7.2.2.v20101205.jar
apache-cxf-2.3.3\lib\jetty-io-7.2.2.v20101205.jar
apache-camel-2.7.1\lib\spring\spring-beans-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-context-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-context-support-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-core-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-expression-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-tx-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\aopalliance-1.0.jar
apache-camel-2.7.1\lib\spring\commons-logging-1.1.1.jar
apache-camel-2.7.1\lib\spring\spring-aop-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\spring\spring-asm-3.0.5.RELEASE.jar
apache-camel-2.7.1\lib\slf4j-api-1.6.1.jar
apache-camel-2.7.1\lib\camel-core-2.7.1.jar
apache-camel-2.7.1\lib\commons-management-1.0.jar
apache-camel-2.7.1\lib\camel-cxf-2.7.1.jar
apache-camel-2.7.1\lib\camel-spring-2.7.1.jar
1 新建bean:
package bean;
public class Usr {
public String test(String str){
System.out.println("Hello Bean "+str);
return "Hello Bean "+str;
}
}
public class Usr {
public String test(String str){
System.out.println("Hello Bean "+str);
return "Hello Bean "+str;
}
}
2 再新建一個webservice接口文件:
package server;
import javax.jws.WebService;
@WebService
public interface IHello {
String sayHi(String name);
}
import javax.jws.WebService;
@WebService
public interface IHello {
String sayHi(String name);
}
3 需要建立spring配置文件,放在classpath下
配置文件有兩個用途:
用cxf發(fā)布一個webservice;
配置camel的route
5 寫個webservice客戶端測試一下:
得到的結(jié)果應(yīng)該是:
用cxf發(fā)布一個webservice;
配置camel的route
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />
<!--發(fā)布一個webservice-->
<cxf:cxfEndpoint id="orderEndpoint"
address="http://localhost:9000/helloWorld"
serviceClass="server.IHello"
/>
<bean id="usr" class="bean.Usr"></bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="cxf:bean:orderEndpoint"/>
<bean ref="usr" method="test"/>
</route>
</camelContext>
</beans>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />
<!--發(fā)布一個webservice-->
<cxf:cxfEndpoint id="orderEndpoint"
address="http://localhost:9000/helloWorld"
serviceClass="server.IHello"
/>
<bean id="usr" class="bean.Usr"></bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="cxf:bean:orderEndpoint"/>
<bean ref="usr" method="test"/>
</route>
</camelContext>
</beans>
4 啟動camel:
package camel;
import org.apache.camel.spring.Main;
public class Helloworld {
public static void main(String[] args) throws Throwable {
Main main = new Main();
main.start();
Thread.sleep(600000);
main.stop();
}
}
import org.apache.camel.spring.Main;
public class Helloworld {
public static void main(String[] args) throws Throwable {
Main main = new Main();
main.start();
Thread.sleep(600000);
main.stop();
}
}
5 寫個webservice客戶端測試一下:
package client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import server.IHello;
public class Test {
public static void main(String[] args) throws Throwable{
URL wsdlURL = new URL("http://localhost:9000/helloWorld?wsdl");//WSDL文件地址
QName SERVICE_NAME = new QName("http://server/", "IHelloService");//參數(shù)一是WSDL中的 targetNamespace,參數(shù)2是WSDL中的 name 節(jié)點
Service service = Service.create(wsdlURL, SERVICE_NAME);
IHello client = service.getPort(IHello.class);
String result = client.sayHi("bbb");
System.out.println(result);
}
}
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import server.IHello;
public class Test {
public static void main(String[] args) throws Throwable{
URL wsdlURL = new URL("http://localhost:9000/helloWorld?wsdl");//WSDL文件地址
QName SERVICE_NAME = new QName("http://server/", "IHelloService");//參數(shù)一是WSDL中的 targetNamespace,參數(shù)2是WSDL中的 name 節(jié)點
Service service = Service.create(wsdlURL, SERVICE_NAME);
IHello client = service.getPort(IHello.class);
String result = client.sayHi("bbb");
System.out.println(result);
}
}
得到的結(jié)果應(yīng)該是:
Hello Bean bbb