The definition of producer and consumer in Camel in Action
book is a bit confusing for me. I've already read two other answers for similar question however I still feel that it's not that.
A producer is the Camel abstraction that refers to an entity capable of creating and sending a message to an endpoint. Figure 1.10 illustrates where the producer fits in with other Camel concepts. When a message needs to be sent to an endpoint, the producer will create an exchange and populate it with data compatible with that particular endpoint. For example, a
FileProducer
will write the message body to a file. AJmsProducer
, on the other hand, will map the Camel message to ajavax.jms.Message
before sending it to a JMS destination. This is an important feature in Camel, because it hides the complexity of interacting with particular transports.A consumer is the service that receives messages produced by a producer, wraps them in an exchange, and sends them to be processed. Consumers are the source of the exchanges being routed in Camel. Looking back at figure 1.10, we can see where the consumer fits in with other Camel concepts. To create a new exchange, a consumer will use the endpoint that wraps the payload being consumed. A processor is then used to initiate the routing of the exchange in Camel using the routing engine.
Who's actually creating an exchange ? On which side of typical chanel of communication is producer and consumer ? From text above I can't really say who's responsible for that. It would be great if someone could provide a picture ( the one from book is unclear for me), where exactly is producer and consumer and explain how they work in an easy way. Maybe some example would be also useful.
Ok so maybe it would be better to give an example and someone could tell me how it works. Imagine that we want to fetch files from a folder and put them on a JMS queue and from there send them for further processing eventually saving on a disk.
Where exactly is producer, consumer according to my picture ? I realize what is a component and an endpoint.
You are more or less right about your suspicion. Given the simple example:
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("file:data/inbox?noop=true") // consumer
.to("file:data/outbox"); // producer
}
});
camelContext.start();
Thread.sleep(2000);
camelContext.stop();
In this example, we used a RouteBuilder
to create a Route
which, once the CamelContext
is started, executes somewhat as follows:
FileComponent
s to represent both locations.FileEndpoint
s by querying the former components.FileConsumer
for reading from data/inbox
.GenericFileProducer
to write to data/outbox
.FileConsumer
to start polling files from its directory which instructs its Endpoint
to create an Exchange
(as correctly shown in the picture). A GenericFileMessage
is bound to this Exchange
.Exchange
is handed over to the FileProducer
.From this view, no exchange is created by the Consumer
. I guess, at this stage of the book it does not yet make sense. And this is reflected in the text. However, looking at the implementation, both is equivalent when you look at the code:
The Consumer
uses some Processor
when sending the message is, in this case, represented by a Consumer
which is then queried by the Consumer
to generate an Exchange
which the Consumer
queries its Endpoint
for which is then creating the actual Exchange
.