--- url: "http://www.enterpriseintegrationpatterns.com/EventDrivenConsumer.html" titulek: "Event-Driven Consumer - Enterprise Integration Patterns" h1: "" popis: "How can an application automatically consume messages as they become available?" jazyk: "en" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-15 15:34:39" znaku: 5516 slov: 487 kodovani: "utf-8" sha256: "bdd22eb6db0ea6606a9ff53c93d17750b2977ec7f0a25120467a49c532c446d0" --- | | Event-Driven Consumer[Messaging Patterns](index.html) » [Messaging Endpoints](MessagingEndpointsIntro.html) | [Contents](toc.html)[⇐ Prev](PollingConsumer.html)[Next ⇒](CompetingConsumers.html) | | --- | --- | --- | An application needs to consume [*Message*](Message.html)s as soon as they’re delivered. How can an application automatically consume messages as they become available? The application should use an *Event-Driven Consumer*, one that is automatically handed messages as they’re delivered on the channel. This is also known as an asynchronous receiver, because the receiver does not have a running thread until a callback thread delivers a message. We call it an *Event-Driven Consumer* because the receiver acts like the message delivery is an event that triggers the receiver into action. An *Event-Driven Consumer* is an object that is invoked by the messaging system when a message arrives on the consumer’s channel. The consumer passes the message to the application through a callback in the application’s API. (How the messaging system gets the message is implementation-specific and may or may not be event-driven. All the consumer knows is that it can sit dormant with no active threads until it gets invoked by the messaging system passing it a message.) ... Read the entire pattern in the book [Enterprise Integration Patterns](/books1.html) ## Example: RabbitMQ Consumer[NEW](/ramblings/eip1_examples_updated.html) [RabbitMQ](http://www.rabbitmq.com) consumers are by default event-driven. When consuming messages off a Channel, you have to provide an implementation of the Consumer interface. The following code example sets up a very simple consumer that simply prints incoming messages as strings: ``` Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery( String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("Received '" + message + "'"); } }; boolean autoAck = true; channel.basicConsume(queueName, autoAck, consumer); ``` This implementation is not a [*Transactional Client*](TransactionalClient.html) because the client acknowledges messages automatically on receipt but before processing. The complete source code for this example can be found on [Github](https://github.com/spac3lord/eip/tree/master/Rabbit/src). **Related patterns:** [*Competing Consumers*](CompetingConsumers.html), [*Durable Subscriber*](DurableSubscription.html), [*Message*](Message.html), [*Message Channel*](MessageChannel.html), [*Message Dispatcher*](MessageDispatcher.html), [*Selective Consumer*](MessageSelector.html), [*Polling Consumer*](PollingConsumer.html), [*Transactional Client*](TransactionalClient.html) --- ## Integration Pattern Language [Message Construct.](MessageConstructionIntro.html) [Message](Message.html) [Command Message](CommandMessage.html) [Document Message](DocumentMessage.html) [Event Message](EventMessage.html) [Request-Reply](RequestReply.html) [Return Address](ReturnAddress.html) [Correlation Identifier](CorrelationIdentifier.html) [Message Sequence](MessageSequence.html) [Message Expiration](MessageExpiration.html) [Format Indicator](FormatIndicator.html) [Message Routing](MessageRoutingIntro.html) [Pipes-and-Filters](PipesAndFilters.html) [Message Router](MessageRouter.html) [Content-based Router](ContentBasedRouter.html) [Message Filter](Filter.html) [Dynamic Router](DynamicRouter.html) [Recipient List](RecipientList.html) [Splitter](Sequencer.html) [Aggregator](Aggregator.html) [Resequencer](Resequencer.html) [Composed Msg. Processor](DistributionAggregate.html) [Scatter-Gather](BroadcastAggregate.html) [Routing Slip](RoutingTable.html) [Process Manager](ProcessManager.html) [Message Broker](MessageBroker.html) [Message Transformation](MessageTransformationIntro.html) [Message Translator](MessageTranslator.html) [Envelope Wrapper](EnvelopeWrapper.html) [Content Enricher](DataEnricher.html) [Content Filter](ContentFilter.html) [Claim Check](StoreInLibrary.html) [Normalizer](Normalizer.html) [Canonical Data Model](CanonicalDataModel.html) [Messaging Endpoints](MessagingEndpointsIntro.html) [Message Endpoint](MessageEndpoint.html) [Messaging Gateway](MessagingGateway.html) [Messaging Mapper](MessagingMapper.html) [Transactional Client](TransactionalClient.html) [Polling Consumer](PollingConsumer.html) [Event-driven Consumer](EventDrivenConsumer.html) [Competing Consumers](CompetingConsumers.html) [Message Dispatcher](MessageDispatcher.html) [Selective Consumer](MessageSelector.html) [Durable Subscriber](DurableSubscription.html) [Idempotent Receiver](IdempotentReceiver.html) [Service Activator](MessagingAdapter.html) [Messaging Channels](MessagingChannelsIntro.html) [Message Channel](MessageChannel.html) [Point-to-Point Channel](PointToPointChannel.html) [Publish-Subscr. Channel](PublishSubscribeChannel.html) [Datatype Channel](DatatypeChannel.html) [Invalid Message Channel](InvalidMessageChannel.html) [Dead Letter Channel](DeadLetterChannel.html) [Guaranteed Delivery](GuaranteedMessaging.html) [Channel Adapter](ChannelAdapter.html) [Messaging Bridge](MessagingBridge.html) [Message Bus](MessageBus.html) [Systems Mgmt.](SystemManagementIntro.html) [Control Bus](ControlBus.html) [Detour](Detour.html) [Wire Tap](WireTap.html) [Message History](MessageHistory.html) [Message Store](MessageStore.html) [Smart Proxy](SmartProxy.html) [Test Message](TestMessage.html) [Channel Purger](ChannelPurger.html)