You can utilize Camel's endpoints directly from your Java code, making it easy to, for example, send a JMS message without any of the boilerplate code that is normally required. Camel makes it easy to send (one-way) or request (request-response) data from any Camel component or route. Camel calls this POJO Producing.
This recipe will show you how you can send data to a Camel endpoint, including a Camel route, from within your Java code.
The Java code for this recipe is located in the org.camelcookbook.extend.produce
package. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with produce
.
The included example uses an embedded (in-process) ActiveMQ instance to act as a JMS provider for its simple JMS message consuming method. In practice, you would normally connect Camel to an external messaging infrastructure.
Camel's POJO Producing makes use of the Camel's ProducerTemplate
class to send or request data from an endpoint. To get a handle to a ProducerTemplate
instance, define it as an variable of your class, and annotate it with @Produce
. Refer to the following code:
import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; public class ProducePojo { @Produce private ProducerTemplate template; public String sayHello(String name) { return template.requestBody("activemq:queue:sayhello", name, String.class); } }
In order to have the ProducerTemplate
injected into the ProducePojo
class, it must be defined as a bean in the same Spring application context as the Camel context.
<beans xmlns="http://www.springframework.org/schema/beans" ...> <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <bean id="producer" class="org.camelcookbook.extend.produce.ProducePojo"/> <camelContext xmlns="http://camel.apache.org/schema/spring"/> </beans>
Camel's POJO Producing uses the @Produce
annotation to inject a ProducerTemplate
at runtime. The ProducerTemplate
provides a large number of methods to send..()
(InOnly
/one-way) or request..()
(InOut
/request-response) data with or without additional message headers and properties. There are also asynchronous variants that allow you to get a callback when your send is complete or your requested data is available.
The ProducerTemplate
can interact with any Camel endpoint such as the ActiveMQ Component, or it can call a full Camel route listening from
an in-memory endpoint such as direct:
or seda:
. This allows you to easily call out to different transports and integration routes directly from your code with minimal disruption to your core logic.
Camel's @Produce
annotation can also be used to automatically create a proxy based on a Java interface provided by you. In this instance, Camel will, at runtime, wrap the call to ProducerTemplate
handling the data type conversion for you. This provides more separation in your code, hiding Camel behind your Java interface.
For example, we can do the same thing as the preceding example with the following code:
public interface ProxyPojo { String sayHello(String name); } public class ProxyProduce { @Produce(uri = "activemq:queue:sayhello") ProxyPojo myProxy; public String doSomething(String name) { return myProxy.sayHello(name); } }
In this code example, use of the @Produce
annotation results in the creation and injection of a proxy object by the Camel context so that your code interacts just with your interface, and the only visible Camel code is the @Produce
annotation. The name
parameter will contain the body of the message, and the return value will become the Camel exchange message seen by the next processor in the route. The fact that the method returns a value implies that this should be treated as an InOut
operation.
Camel uses its data type converters to automatically convert the message body to the type specified in the parameters of the method. A method parameter can also be of the type Exchange
, in which case the method would have full access to all of the data within the Camel exchange. See Camel's documentation on Bean Binding for more details.
3.16.76.237