Using CamelProxyPage edited by Claus IbsenUsing CamelProxyCamel allows you to proxy a producer sending to an Endpoint by a regular interface. Then when clients using this interface can work with it as if its regular java code but in reality its proxied and does a Request Reply to a given endpoint. Proxy from SpringYou can define a proxy in the spring XML file as shown below <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">
<!-- create a proxy that will route to the direct:start endpoint when invoked -->
<proxy id="myProxySender"
serviceInterface="org.apache.camel.spring.config.MyProxySender"
serviceUrl="direct:start"/>
<!-- this is the route that our proxy will routed when invoked
and the output from this route is returned as reply on the proxy -->
<route>
<from uri="direct:start"/>
<transform>
<simple>Bye ${body}</simple>
</transform>
</route>
</camelContext>
Now the client can grab this bean using regular spring bean coding and invoke it as if its just another bean. ApplicationContext ac = new ClassPathXmlApplicationContext("org/apache/camel/spring/config/AnotherCamelProxyTest.xml"); MyProxySender sender = (MyProxySender) ac.getBean("myProxySender"); String reply = sender.hello("Camel"); assertEquals("Bye Camel", reply); In Camel 2.3 you can define that proxied methods which has void as return type, can be treated as Fire and Forget messaging style (aka InOnly). This is done by setting the attribute voidAsInOnly=true in the <proxy/> tag. If you let it be the default value, which is InOut then a void method will still wait for the message to be completely routed. This is not always what you want and hence you can use this option to turn it onto a fire and forget style. BTW: You can also add the @InOnly annotation to the method on the interface to tell Camel that this method is InOnly. Such an example is shown below: <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring"> <!-- create a proxy that will route to the seda:start endpoint when invoked and it should let the methods which returns void act as InOnly messages --> <proxy id="myOtherSender" serviceInterface="org.apache.camel.spring.config.MyOtherProxySender" serviceUrl="seda:start" voidAsInOnly="true"/> <!-- this is the route that our proxy will routed when invoked --> <route> <from uri="seda:start"/> <delay> <constant>2000</constant> </delay> <to uri="mock:result"/> </route> </camelContext> Proxy from JavaYou can also create a proxy from regular Java using a org.apache.camel.component.bean.ProxyHelper as shown below:
Endpoint endpoint = context.getEndpoint("direct:start");
MyProxySender sender = ProxyHelper.createProxy(endpoint, MyProxySender.class);
In Camel 2.3 you can use org.apache.camel.builder.ProxyBuilder which may be easier to use than ProxyHelper: public void testProxyBuilderProxyCallAnotherBean() throws Exception { // use ProxyBuilder to easily create the proxy OrderService service = new ProxyBuilder(context).endpoint("direct:bean").build(OrderService.class); String reply = service.submitOrderStringReturnString("World"); assertEquals("Hello World", reply); } Proxy with AnnotationAnother way to configure the proxy from java is by using the @Produce annotation. Also see POJO Producing.
@Produce(uri="direct:start")
MyProxySender sender;
This basically does the same as ProxyHelper.createProxy. What is send on the MessageWhen using a proxy Camel will send the message payload as a org.apache.camel.component.bean.BeanInvocation object which holds the details of which method was invoked and what the argument was. Turning the BeanInvocation into a first class payloadAvailable as of Camel 2.1 If you proxied method signature only have one parameter such as: String hello(String name); Then it gives another advantage in Camel as it allows Camel to regard the value passed in to this single parameter as the real payload. In other words if you pass in Camel to the hello method, then Camel can see that as a java.lang.String payload with the value of Camel. This gives you a great advantage as you can use the proxy as first class services with Camel. You can proxy Camel and let clients use the pure and clean interfaces as if Camel newer existed. Then Camel can proxy the invocation and receive the input passed into the single method parameter and regard that as if it was just the message payload. Okay lets try that with an example Example with proxy using single parameter methods.At first we have the interface we wish to proxy public interface OrderService { String submitOrderStringReturnString(String order); Document submitOrderStringReturnDocument(String order); String submitOrderDocumentReturnString(Document order); Document submitOrderDocumentReturnDocument(Document order); void doNothing(String s); Integer invalidReturnType(String s); String doAbsolutelyNothing(); } Notice that all methods have single parameters. The return type is optional, as you can see one of them is void. This allows us to easily use org.w3c.dom.Document and String types with no hazzle. Okay then we have the following route where we route using a Content Based Router that is XML based. See that we use XPath in the choices to route the message depending on its a book order or not. from("direct:start") .choice() .when(xpath("/order/@type = 'book'")).to("direct:book") .otherwise().to("direct:other") .end(); from("direct:book").transform(constant("<order id=\"123\">OK</order>")); from("direct:other").transform(constant("<order>FAIL</order>")); Now there is a couple of tests that shows using the Camel Proxy how we can easily invoke the proxy and do not know its actually Camel doing some routing underneath. Endpoint endpoint = context.getEndpoint("direct:start"); OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class); String reply = service.submitOrderStringReturnString("<order type=\"book\">Camel in action</order>"); assertEquals("<order id=\"123\">OK</order>", reply); And this one below shows using different types that Camel adapts to. Endpoint endpoint = context.getEndpoint("direct:start"); OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class); Document doc = context.getTypeConverter().convertTo(Document.class, "<order type=\"book\">Camel in action</order>"); Document reply = service.submitOrderDocumentReturnDocument(doc); assertNotNull(reply); String s = context.getTypeConverter().convertTo(String.class, reply); assertEquals("<order id=\"123\">OK</order>", s); Isn't this cool? See also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > Using CamelProxy confluence
- [CONF] Apache Camel > Using CamelProxy confluence
- [CONF] Apache Camel > Using CamelProxy confluence
- [CONF] Apache Camel > Using CamelProxy confluence
- [CONF] Apache Camel > Using CamelProxy confluence
