Apache Camel – Producer and Consumer Template

Hello everyone, In this post, you will learn How to use Producer and Consumer Template in Apache Camel. The example has been tested and shared in the post.

Maven Dependencies


Example Program

package com.dineshkrish.example31;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

 * @author Dinesh Krishnan

public class ProducerAndConsumer {

	public static void main(String[] args) throws Exception {

		// creating a camel context
		CamelContext context = new DefaultCamelContext();

		// adding routes to camel context
		context.addRoutes(new RouteBuilder() {
			public void configure() throws Exception {

		// start the context

		// producer template
		ProducerTemplate producerTemplate = context.createProducerTemplate();
		producerTemplate.sendBody("direct:start", "This message sent by producer");

		// consumer template
		ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
		String message = consumerTemplate.receiveBody("seda:end", String.class);


		// stop the context


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
This message sent by producer