---
Title: Consume logic
URL Source: https://company-skill.com/p/rocketmq/rocketmq-consume-logic
Language: en
Description: You want to receive messages from a RocketMQ topic and apply custom processing logic, including handling success/failure outcomes, managing consumer startup/shutdown, or implementing…
---

# Consume logic

Part of **Apache RocketMQ**. Route queries via `POST https://company-skill.com/api/route`.

## What You Want to Do

You want to receive messages from a RocketMQ topic and apply custom processing logic, including handling success/failure outcomes, managing consumer startup/shutdown, or implementing concurrency/ordered delivery models.

**Typical User Questions**:
- How to consume messages from RocketMQ topics?
- How to handle message consumption failures?
- Does RocketMQ support concurrent or ordered consumption?

## Decision Tree

- **If** you are implementing the core message processing loop using `MessageListener` with support for `CLUSTERING` or `BROADCASTING` modes → Use **Consume Message via Java SDK** (go to *rocketmq/rocketmq-queue*)
- **If** you need to call `start()` before consumption begins or `shutdown()` for graceful termination → Use **Manage Consumer Lifecycle via Java SDK** (go to *rocketmq/rocketmq-queue*)
- **If** your message listener must return either `Action.CommitMessage` or `Action.ReconsumeLater` based on processing results → Use **Handle Consumption Results via Java SDK** (go to *rocketmq/rocketmq-consumption*)
- **Otherwise (default)** → Start with **Consume Message via Java SDK**, as it provides the foundational message processing interface most applications require.

## Path Comparison

| Path | Best For | Complexity | Code Required | Automation | Key Fact | Detail Skill |
|------|----------|------------|---------------|------------|----------|-------------|
| Consume Message via Java SDK | When building applications that need to receive and process messages with support for concurrent or ordered models. | medium | Yes | Yes | Supports `shardingKey` for ordered consumption and `ConsumeMessageBatchMaxSize` for batch processing | `rocketmq/api/rocketmq-queue` |
| Manage Consumer Lifecycle via Java SDK | When initializing and gracefully shutting down consumer instances to manage resources and ensure proper acknowledgment. | low | Yes | Yes | Requires explicit `start()` and `shutdown()` calls; uses `Properties` with `AccessKey`, `SecretKey`, and `ONSAddr` | `rocketmq/api/rocketmq-queue` |
| Handle Consumption Results via Java SDK | When implementing custom logic to decide whether a message should be acknowledged or retried. | medium | Yes | Yes | Redelivered messages count toward billing; uses broker-managed exponential backoff for `ReconsumeLater` | `rocketmq/api/rocketmq-consumption` |

## Path Details

### Path 1: Consume Message via Java SDK

**Best For**: When building applications that need to receive and process messages with support for concurrent or ordered models.

**Brief Description**: This approach uses the RocketMQ Java SDK to create consumers via `ONSFactory`, register a `MessageListener`, and call `subscribe()` to bind to topics. It supports both `MessageModel.CLUSTERING` and `MessageModel.BROADCASTING` modes, and enables ordered consumption using a `shardingKey`.

**Key technical facts**:
- Billing: per_request
- Max concurrency: 100
- Regions available: cn-hangzhou, international

**When to Use**:
- Need to process messages with concurrent consumption model
- Building applications requiring ordered message processing with shardingKey
- Need batch consumption capabilities with ConsumeMessageBatchMaxSize configuration

**When NOT to Use**:
- Only need basic message acknowledgment without custom retry logic
- Looking for simple consumer lifecycle management without message processing implementation

**Known Limitations**:
- Requires rocketmq-client-java>=4.5.0 SDK dependency
- Max 100 QPS per consumer group
- Single consumer group maximum 100 concurrent connections
- Only supports CLUSTERING or BROADCASTING consumption modes

### Path 2: Manage Consumer Lifecycle via Java SDK

**Best For**: When initializing and gracefully shutting down consumer instances to manage resources and ensure proper acknowledgment.

**Brief Description**: This path focuses on consumer initialization and termination using `ONSFactory` to create a consumer from `Properties` containing `ConsumerId`, `AccessKey`, `SecretKey`, and `ONSAddr`. The consumer must call `start()` to begin receiving messages and `shutdown()` to release resources cleanly.

**Key technical facts**:
- Billing: per_request
- Max concurrency: 100
- Regions available: cn-hangzhou, international

**When to Use**:
- Need to properly initialize consumer instances before message processing
- Application requires graceful shutdown to ensure pending messages are handled properly
- Building resource-constrained applications that need explicit lifecycle control

**When NOT to Use**:
- Need to implement custom message processing logic with success/failure handling
- Looking for advanced consumption patterns like ordered or batch processing

**Known Limitations**:
- Requires explicit shutdown() call to release resources gracefully
- Must call start() before consumer can receive messages
- Limited to 100 QPS per consumer group

### Path 3: Handle Consumption Results via Java SDK

**Best For**: When implementing custom logic to decide whether a message should be acknowledged (CommitMessage) or retried (ReconsumeLater) based on processing results.

**Brief Description**: This approach implements fine-grained control over message outcomes by returning either `Action.CommitMessage` or `Action.ReconsumeLater` from the `consume` method of a `MessageListener`. It integrates with `ConsumeContext` to influence redelivery behavior, though custom delays are not supported.

**Key technical facts**:
- Billing: Billed per message consumption request (including redeliveries)
- Max concurrency: 100
- Regions available: cn-hangzhou, cn-shanghai, cn-beijing

**When to Use**:
- Need to implement custom retry logic for transient failures
- Application requires different handling for permanent vs temporary message processing errors
- Want to avoid infinite retry loops for malformed messages by committing permanently failed messages

**When NOT to Use**:
- Only need basic consumer lifecycle management without result handling
- Looking for simple message consumption without custom success/failure logic

**Known Limitations**:
- Must explicitly return either CommitMessage or ReconsumeLater from message listener
- Redelivered messages count toward total usage and billing
- Custom redelivery delays are not supported - uses broker-managed exponential backoff
- Callback must complete before next message in same queue partition is delivered

## FAQ

Q: Which path should I start with?
A: Begin with **Consume Message via Java SDK** if you're building a standard message processor. It includes the core `MessageListener` pattern and supports both concurrent and ordered models via `MessageModel` and `shardingKey`.

Q: What if I need to handle transient database errors but used only the lifecycle management path?
A: If you use **Manage Consumer Lifecycle via Java SDK** without implementing result handling, all messages will be auto-acknowledged regardless of processing success, causing failed messages to be lost permanently.

Q: What if I return neither `CommitMessage` nor `ReconsumeLater` in my listener?
A: The SDK requires an explicit return value. Failing to return either action may cause undefined behavior or block further message delivery in the same queue partition, as noted in the limitations of **Handle Consumption Results via Java SDK**.

Q: Can I use ordered consumption with custom retry logic?
A: Yes — combine **Consume Message via Java SDK** (for `shardingKey`-based ordering) with **Handle Consumption Results via Java SDK** (for `ReconsumeLater`). But note: redelivery uses broker-managed backoff, not custom delays.

Q: Why am I getting duplicate messages after a consumer restart?
A: If you didn’t call `shutdown()` properly (from **Manage Consumer Lifecycle**), unacknowledged messages may be redelivered. Also, if you’re using **Handle Consumption Results** and return `ReconsumeLater` unintentionally, those messages will reappear.

Q: Does the 100 QPS limit apply across all paths?
A: Yes — all three paths share the same underlying consumer group limits: max 100 QPS and 100 concurrent connections per group, as stated in each fact card.

Q: Are `AccessKey` and `SecretKey` required for every message?
A: No — credentials are configured once in `Properties` during client initialization (via `ONSFactory`), not per-message, as confirmed in the auth_method of all paths.

## Related queries

consume messages, message consumption, process rocketmq messages, handle incoming messages, consume from topic, message listener, ack messages, message acknowledgment, ordered consumption, concurrent consumption, start consumer, shutdown consumer, manage consumer lifecycle, custom retry logic, redel

---
Part of [Apache RocketMQ](https://company-skill.com/p/rocketmq.md) · https://company-skill.com/llms.txt
