37

I have difficulties wrapping my head around the concept.

I am trying to implement an endpoint that listens on a tcp port for incoming messages in a proprietary format, which would then transform the message and have camel take over the forwarding and routing.

Did I understand correctly that that the Producer is responsible for sending messages into the Endpoint and the Consumer receives them from that endpoint?

When studying the interfaces I couldn't figure out the message flow between those objects, especially on the consumer part. Consumer only defines start() and stop() methods...

When setting up a test on a skeleton implementation, Camel invoked createProducer() on the endpoint and process() on the producer object. After that it returned, swithout doing anything with the consumer or the processor associated with it.

Could someone point me in the right direction?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Arnelism
  • 1,484
  • 1
  • 15
  • 22

3 Answers3

64

It's important to remember that an Endpoint, created by a Component (i.e. Endpoint Factory), can sit at either end of a Camel Route. If you put the Component at the start of a route then there must be an implementation of the Consumer part of the Component. This does the work of converting the specific input/request (like an HTTP request) into something generic - a Camel Exchange - that can travel down a Route. Whereas if you put the Component at end of a route then you must have an implementation of a Producer. The Producer does the work of taking the Exchange from the end of a route and converting it into something specific (like a JMS message).

I find the Fuse ESB documentation to be better (in general) than the Apache Camel website. From the Fuse ESB Component page:

Consumer endpoints consume requests. They always appear at the start of a route and they encapsulate the code responsible for receiving incoming requests and dispatching outgoing replies.

Producer endpoints produce requests. They always appears at the end of a route and they encapsulate the code responsible for dispatching outgoing requests and receiving incoming replies.


enter image description here

chrisjleu
  • 4,329
  • 7
  • 42
  • 55
  • 6
    Nice clear explanation thanks. The terms are often counter-intuitive, as a typical user of camel is thinking in terms of routes and messageExchanges, rather than authoring a new camel component. From the perspective of a messageExchange, the terms are the wrong way around. A Consumer produces a messageExchange, while a Producer consumes a messageExchange. – dan carter Jan 22 '15 at 01:03
  • As a newbie to Camel, I'm really confused by this answer. If I look at the `org.apache.camel.Producer` interface, the only contracts it specifies are `createExchange` methods. And looking at something like the `EventDrivenPollingConsumer`, the only thing that does with an Exchange is `process` it. Doesn't an Exchange travel through a route - that's what Processors work with, so that was my impression. – Snekse May 15 '15 at 15:29
  • @Snekse The descriptive part of the answer says exactly this. But in any case I don't think this technicality has much relevance for a conceptual understanding of producers and consumers. – chrisjleu May 15 '15 at 16:51
22

I finally figured it out by looking at the Stream component.

Turns out that I made the mistake of thinking about the endpoint as something central thru which everything must go.

The simple answer is that the consumer receives data from an external system (listening on a server socket in my case) and the producer sends data to the external system.

Since my endpoint is read-only (it will not be used as the final destination of Camel routing process), I really do not need a producer (it should throw a RuntimeException if the system still tries to do it due to misconfiguration). A fitting example would be the camel-atom endpoint - you can read feeds but (as of 1.6.0) you cannot publish one.

Likewise, you only need a producer for a write-only endpoint which does not receive data from an external system (e.g logging).

Arnelism
  • 1,484
  • 1
  • 15
  • 22
8

A Producer extends Processor which means it has a process method as well.

Check out the free chapter 1 in the Camel in Action book which tells a bit about those Camel concepts. http://www.manning.com/ibsen/

And this tutorial is also excellent as it introduces those concepts in a steady pace http://camel.apache.org/tutorial-example-reportincident.html

AlexS
  • 5,295
  • 3
  • 38
  • 54
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65