You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "BewareMyPower (via GitHub)" <gi...@apache.org> on 2025/09/26 06:12:12 UTC
Re: [PR] Support custom router for producer [pulsar-client-node]
BewareMyPower commented on code in PR #433:
URL: https://github.com/apache/pulsar-client-node/pull/433#discussion_r2380972705
##########
tests/producer.test.js:
##########
@@ -156,5 +159,95 @@ const Pulsar = require('../index');
await producer2.close();
});
});
+ describe('Message Routing', () => {
+ test('Custom Message Router', async () => {
+ // 1. Define a partitioned topic and a custom router
+ const targetPartition = 1;
+ const partitionedTopicName = `test-custom-router-${Date.now()}`;
+ const partitionedTopic = `persistent://public/default/${partitionedTopicName}`;
+ const numPartitions = 10;
+
+ // Use admin client to create a partitioned topic. This is more robust.
+ // Assuming 'adminUrl' and 'httpRequest' are available from your test setup.
+ const partitionedTopicAdminURL = `${adminUrl}/admin/v2/persistent/public/default/${partitionedTopicName}/partitions`;
+ const createPartitionedTopicRes = await httpRequest(
+ partitionedTopicAdminURL, {
+ headers: {
+ 'Content-Type': 'application/json', // Use application/json for REST API
+ },
+ data: numPartitions,
+ method: 'PUT',
+ },
+ );
+ // 204 No Content is success for PUT create
+ expect(createPartitionedTopicRes.statusCode).toBe(204);
+
+ // 2. Create a producer with the custom message router
+ const producer = await client.createProducer({
+ topic: partitionedTopic, // Note: For producer, use the base topic name
+ messageRouter: (message, topicMetadata) => targetPartition,
Review Comment:
It's better to show the router can get the message's key or value, as well as the number of partitions from `topicMetadata`, otherwise the parameters will be meaningless
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org