Appendix C. The producer and consumer templates
Throughout this book, you’ve seen the ProducerTemplate used as an easy way of sending messages to a Camel endpoint. While it was simple to understand in those cases, there are many more options to the ProducerTemplate. There is also an analog for consumers—the ConsumerTemplate, which makes consuming messages easy.
Both of these templates are inspired by the template utility classes in the Spring Framework that simplify access to an API. In Spring, you may have used a JmsTemplate or JdbcTemplate to simplify access to the JMS and JDBC APIs. In the case of Camel, the ProducerTemplate and ConsumerTemplate interfaces allow you to easily work with producers and consumers.
By “easily work,” we mean you can send a message to any kind of Camel component in only one line of code. For example, the following code sends a JMS text message to the ActiveMQ JMS queue named quotes:
Anyone who has worked with the JMS API will know that it takes quite a bit of work to replicate what Camel does in that single line of code.
In this appendix, we’ll show you how to use the ProducerTemplate and ConsumerTemplate in detail.
The ProducerTemplate has a lot of methods that at first may seem a bit overwhelming. But these methods make it powerful and flexible, which in turn makes it a time saver in many situations.