Camel 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 Spring
You can define a proxy in the spring XML file as shown below
Now the client can grab this bean using regular spring bean coding and invoke it as if its just another bean.
Proxy from Java
You can also create a proxy from regular Java using a
In Camel 2.3 you can use
Proxy with Annotation
Another way to configure the proxy from java is by using the @Produce annotation. Also see POJO Producing.
This basically does the same as ProxyHelper.createProxy.
What is send on the Message
When using a proxy Camel will send the message payload as a
Turning the BeanInvocation into a first class payload
Available as of Camel 2.1
If you proxied method signature only have one parameter such as:
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
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
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
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.
And this one below shows using different types that Camel adapts to.
Isn't this cool?
Asynchronous using Future
Available as of Camel 2.8
By default the Camel Proxy invocation is synchronous when invoked from the client. If you want this to be asynchronous you define the return type to be of
For example given this client interface
The client can use this with a Camel Proxy as shown from the following snippet from an unit test:
This allows you to fully define your client API without any Camel dependency at all, and decide whether the invocation should be synchronous or asynchronous.
If the Client is asynchronous (return type is Future) then Camel will continue processing the invocation using a thread pool which is being looked up using the key