yosiat commented on issue #78: Error: Failed to send message: AlreadyClosed 
URL: 
https://github.com/apache/pulsar-client-node/issues/78#issuecomment-601153727
 
 
   How I avoid client destruction?
   
   In the following example, I create a producer and produce a message every 1 
second.
   
   ```javascript
   const Pulsar = require('pulsar-client');
   
   (async () => {
     // Create a client
     const client = new Pulsar.Client({
       serviceUrl: 'pulsar://localhost:6650'
     });
   
     // Create a producer
     const producer = await client.createProducer({
       topic: 'persistent://public/default/my-topic',
       sendTimeoutMs: 30000,
       batchingEnabled: true,
     });
   
     let i =0;
     async function produceMessage() {
       i = i + 1;
   
       const msg = `my-message-${i}`;
       const ret = await producer.send({
         data: Buffer.from(msg),
       });
       console.log(`Sent message: ${msg}`);
       console.log(ret);
   
       setTimeout(produceMessage, 1000);
     }
   
     produceMessage();
   })();
   ```
   
   Output:
   ```
   2020-03-19 14:33:49.010 INFO  ConnectionPool:85 | Created connection for 
pulsar://localhost:6650
   2020-03-19 14:33:49.011 INFO  ClientConnection:330 | [127.0.0.1:64239 -> 
127.0.0.1:6650] Connected to broker
   2020-03-19 14:33:49.111 INFO  BatchMessageContainer:43 | { BatchContainer 
[size = 0] [batchSizeInBytes_ = 0] [maxAllowedMessageBatchSizeInBytes_ = 
131072] [maxAllowedNumMessagesInBatch_ = 1000] [topicName = 
persistent://public/default/my-topic] [producerName_ = ] [batchSizeInBytes_ = 
0] [numberOfBatchesSent = 0] [averageBatchSize = 0]} BatchMessageContainer 
constructed
   2020-03-19 14:33:49.111 INFO  HandlerBase:53 | 
[persistent://public/default/my-topic, ] Getting connection from pool
   2020-03-19 14:33:49.155 INFO  ProducerImpl:151 | 
[persistent://public/default/my-topic, ] Created producer on broker 
[127.0.0.1:64239 -> 127.0.0.1:6650]
   Sent message: my-message-1
   null
   Sent message: my-message-2
   null
   Sent message: my-message-3
   null
   Sent message: my-message-4
   null
   Sent message: my-message-5
   null
   Sent message: my-message-6
   null
   Sent message: my-message-7
   null
   Sent message: my-message-8
   null
   2020-03-19 14:33:57.128 INFO  ClientConnection:1349 | [127.0.0.1:64239 -> 
127.0.0.1:6650] Connection closed
   (node:37259) UnhandledPromiseRejectionWarning: Error: Failed to send 
message: AlreadyClosed
   (node:37259) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
This error originated either by throwing inside of an async function without a 
catch block, or by rejecting a promise which was not handled with .catch(). 
(rejection id: 1)
   (node:37259) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.
   2020-03-19 14:33:57.312 INFO  ProducerImpl:472 | Producer - 
[persistent://public/default/my-topic, standalone-1-71] , 
[batchMessageContainer = { BatchContainer [size = 0] [batchSizeInBytes_ = 0] 
[maxAllowedMessageBatchSizeInBytes_ = 131072] [maxAllowedNumMessagesInBatch_ = 
1000] [topicName = persistent://public/default/my-topic] [producerName_ = 
standalone-1-71] [batchSizeInBytes_ = 0] [numberOfBatchesSent = 8] 
[averageBatchSize = 1]}]
   2020-03-19 14:33:57.312 INFO  BatchMessageContainer:170 | 
[numberOfBatchesSent = 8] [averageBatchSize = 1]
   2020-03-19 14:33:57.312 INFO  ClientConnection:235 | [127.0.0.1:64239 -> 
127.0.0.1:6650] Destroyed connection
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to