Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Ce contenu n'est pas disponible dans la langue sélectionnée.
50.2. Implementing the Producer Interface
Alternative ways of implementing a producer
Copier lienLien copié sur presse-papiers!
					You can implement a producer in one of the following ways:
				
How to implement a synchronous producer
Copier lienLien copié sur presse-papiers!
					Example 50.4, “DefaultProducer Implementation” outlines how to implement a synchronous producer. In this case, call to 
Producer.process() blocks until a reply is received.
				Example 50.4. DefaultProducer Implementation
- 1
- Implement a custom synchronous producer class, CustomProducer, by extending theorg.apache.camel.impl.DefaultProducerclass.
- 2
- Implement a constructor that takes a reference to the parent endpoint.
- 3
- Theprocess()method implementation represents the core of the producer code. The implementation of theprocess()method is entirely dependent on the type of component that you are implementing. In outline, theprocess()method is normally implemented as follows:- If the exchange contains an In message, and if this is consistent with the specified exchange pattern, then send the In message to the designated endpoint.
- If the exchange pattern anticipates the receipt of an Out message, then wait until the Out message has been received. This typically causes theprocess()method to block for a significant length of time.
- When a reply is received, callexchange.setOut()to attach the reply to the exchange object. If the reply contains a fault message, set the fault flag on the Out message usingMessage.setFault(true).
 
How to implement an asynchronous producer
Copier lienLien copié sur presse-papiers!
					Example 50.5, “CollectionProducer Implementation” outlines how to implement an asynchronous producer. In this case, you must implement both a synchronous 
process() method and an asynchronous process() method (which takes an additional AsyncCallback argument).
				Example 50.5. CollectionProducer Implementation
- 1
- Implement a custom asynchronous producer class, CustomProducer, by extending theorg.apache.camel.impl.DefaultProducerclass, and implementing theAsyncProcessorinterface.
- 2
- Implement a constructor that takes a reference to the parent endpoint.
- 3
- Implement the synchronousprocess()method.
- 4
- Implement the asynchronousprocess()method. You can implement the asynchronous method in several ways. The approach shown here is to create ajava.lang.Runnableinstance,task, that represents the code that runs in a sub-thread. You then use the Java threading API to run the task in a sub-thread (for example, by creating a new thread or by allocating the task to an existing thread pool).
- 5
- Normally, you returnfalsefrom the asynchronousprocess()method, to indicate that the exchange was processed asynchronously.
- 6
- The CustomProducerTaskclass encapsulates the processing code that runs in a sub-thread. This class must store a copy of theExchangeobject,exchange, and theAsyncCallbackobject,callback, as private member variables.
- 7
- Therun()method contains the code that sends the In message to the producer endpoint and waits to receive the reply, if any. After receiving the reply (Out message or Fault message) and inserting it into the exchange object, you must callcallback.done()to notify the caller that processing is complete.