我目前正在使用Apache CXF与Apache Camel集成的项目。 Apache CXF是一种解决方案,我们用它来公开WebService,然后编组/解组SOAP请求并将其传递给Camel。这是非常标准的。默认情况下,使用ApacheCXF中的POJO dataFormat,但是需要从SOAP头中获取一些信息""并传递给骆驼。我的问题是如何做到这一点?当我在Apache CXF中使用Interceptor时,我可以获得我需要的信息,但我无法将其传递给Camel。下面的类是CXF拦截器
public class MyInterceptor extends AbstractSoapInterceptor {
//..... some variables
@Override
public void handleMessage(SoapMessage message) throws Fault {
//..some logic and then setting a variable
message.getExchange().put("Foo", "Bar");
}
}
...下面的类是最终调用的Camel Processor:
public class MyCamelProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
//how can I read information from CXF Intercptor here?
//how can I read "Foo" value?
}
}
据我所知,Apache CXF使用的Exchange类与Camel使用的Exchange不同,但是应该有一种在这两种集成技术之间传递信息的方法吗?
答案 0 :(得分:3)
最后,我解决了以下问题:
在我的上下文中,我有一个带有camel-cxf组件的消费者服务,它被路由到处理器。
Camel的CxfEndpoint类有一个方法调用setInInterceptors:
public void setInInterceptors(List<org.apache.cxf.interceptor.Interceptor<? extends org.apache.cxf.message.Message>> interceptors)
因此,如果我们在bean定义文件中定义下一个:
...
<cxf:cxfEndpoint id="consumerId"
address="/myservice"
serviceClass="com.example.service.MyServiceSEI">
<cxf:inInterceptors>
<ref bean="myInterceptor"/>
</cxf:inInterceptors>
</cxf:cxfEndpoint>
<bean id="myInterceptor" class="com.example.interceptors.MyInterceptor" />
然后,在我们的自定义拦截器中,我们可以在地图中设置任何变量
...
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
...
public class MyInterceptor extends AbstractSoapInterceptor {
public MyInterceptor() {
super(Phase.RECEIVE);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
//..some logic and then setting a variable
message.getExchange().put("Foo", "Bar");
}
}
最后,我们可以使用org.apache.cxf.message.Message类在我们的处理器中获取变量,这与使用Exchange.getIn()方法的org.apache.camel.Message不同
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.cxf.message.Message;
import org.apache.camel.component.cxf.common.message.CxfConstants;
public class MyCamelProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
//how can I read information from CXF Intercptor here?
//how can I read "Foo" value?
Message cxfMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, Message.class);
String foo = (String) cxfMessage.getExchange().get("Foo");
// read message from camel context
org.apache.camel.Message inMessage = exchange.getIn();
...
}
}