GitHub user JanYork closed a discussion: In nodejs, the SimpleConsumer receive
method cannot guarantee that messages are obtained in order?
my consumer code:
```js
require('module-alias/register');
import { SimpleConsumer } from '@/consumer';
const consumer = new SimpleConsumer({
consumerGroup: 'checkout-group',
endpoints: '192.168.1.162:8081',
subscriptions: new Map().set('checkout-topic-fifo', '*'),
requestTimeout: 3000,
awaitDuration: 30000 // long polling
});
console.log('checkout:consumer init success!');
const isShutdown = false;
async function startAndConsumeMessages() {
try {
await consumer.startup();
console.log('checkout:consumer startup success!');
async function consumeMessages() {
try {
const messages = await consumer.receive(10);
if (messages.length > 0) {
console.log('got %d messages', messages.length);
for (const message of messages) {
console.log('body=%o', message.body.toString());
await consumer.ack(message);
}
} else {
console.log('No messages received, waiting...');
}
} catch (error) {
console.error('An error occurred:', error);
} finally {
console.log('checkout:waiting for messages...');
if (!isShutdown) {
await consumeMessages();
}
}
}
await consumeMessages();
} catch (error) {
console.error('An error occurred:', error);
} finally {
// ...
}
}
startAndConsumeMessages().catch(console.error);
```
my producer code:
```js
require('module-alias/register');
import { Producer } from '@/producer';
const fifoProducer = new Producer({
endpoints: 'localhost:8081'
});
console.log('checkout:simpleProducer init success!');
(async () => {
fifoProducer.startup().then(async () => {
console.log('checkout:simpleProducer startup success!');
for (let i = 1; i <= 10; i++) {
await fifoProducer.send({
messageGroup: 'checkout-group',
topic: 'checkout-topic-fifo',
tag: 'checkout',
keys: ['checkout-key'],
body: Buffer.from(i.toString())
});
console.log('message %d sent', i);
console.log('checkout:send message success!');
}
process.exit(0);
});
})();
```
Producer execution log:
```
message 1 sent
checkout:send message success!
message 2 sent
checkout:send message success!
message 3 sent
checkout:send message success!
message 4 sent
checkout:send message success!
message 5 sent
checkout:send message success!
message 6 sent
checkout:send message success!
message 7 sent
checkout:send message success!
message 8 sent
checkout:send message success!
message 9 sent
checkout:send message success!
message 10 sent
checkout:send message success!
```
Consumer execution log:
```
checkout:consumer startup success!
got 10 messages
body='2'
body='3'
body='4'
body='5'
body='6'
body='7'
body='8'
body='9'
body='10'
body='1'
checkout:waiting for messages...
```
It seems that this situation only occurs when the consumer obtains messages for
the first time after startup. The order of monitoring messages thereafter is
consistent.
Isn't the receive method an atomic method? My Topic type is FIFO, and my
messages are produced to the same group in order, but the receive method does
not guarantee the order when it gets the message for the first time after the
consumer starts, and only the first message does this.
GitHub link: https://github.com/apache/rocketmq/discussions/8113
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]