use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.
the class CmdProduce method run.
/**
* Run the producer.
*
* @return 0 for success, < 0 otherwise
* @throws Exception
*/
public int run() throws PulsarClientException {
if (mainOptions.size() != 1)
throw (new ParameterException("Please provide one and only one topic name."));
if (this.serviceURL == null || this.serviceURL.isEmpty())
throw (new ParameterException("Broker URL is not provided."));
if (this.numTimesProduce <= 0)
throw (new ParameterException("Number of times need to be positive number."));
if (messages.size() == 0 && messageFileNames.size() == 0)
throw (new ParameterException("Please supply message content with either --messages or --files"));
int totalMessages = (messages.size() + messageFileNames.size()) * numTimesProduce;
if (totalMessages > MAX_MESSAGES) {
String msg = "Attempting to send " + totalMessages + " messages. Please do not send more than " + MAX_MESSAGES + " messages";
throw new ParameterException(msg);
}
String topic = this.mainOptions.get(0);
int numMessagesSent = 0;
int returnCode = 0;
try {
PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig);
Producer producer = client.createProducer(topic);
List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames);
RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null;
for (int i = 0; i < this.numTimesProduce; i++) {
List<Message> messages = generateMessages(messageBodies);
for (Message msg : messages) {
if (limiter != null)
limiter.acquire();
producer.send(msg);
numMessagesSent++;
}
}
client.close();
} catch (Exception e) {
LOG.error("Error while producing messages");
LOG.error(e.getMessage(), e);
returnCode = -1;
} finally {
LOG.info("{} messages successfully produced", numMessagesSent);
}
return returnCode;
}
use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.
the class PulsarSpout method nextTuple.
/**
* Emits a tuple received from the Pulsar consumer unless there are any failed messages
*/
@Override
public void nextTuple() {
Message msg;
// check if there are any failed messages to re-emit in the topology
msg = failedMessages.peek();
if (msg != null) {
MessageRetries messageRetries = pendingMessageRetries.get(msg.getMessageId());
if (Backoff.shouldBackoff(messageRetries.getTimeStamp(), TimeUnit.NANOSECONDS, messageRetries.getNumRetries())) {
Utils.sleep(100);
} else {
// remove the message from the queue and emit to the topology, only if it should not be backedoff
LOG.info("[{}] Retrying failed message {}", spoutId, msg.getMessageId());
failedMessages.remove();
mapToValueAndEmit(msg);
}
return;
}
// receive from consumer if no failed messages
if (consumer != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("[{}] Receiving the next message from pulsar consumer to emit to the collector", spoutId);
}
try {
msg = consumer.receive(1, TimeUnit.SECONDS);
if (msg != null) {
++messagesReceived;
messageSizeReceived += msg.getData().length;
}
mapToValueAndEmit(msg);
} catch (PulsarClientException e) {
LOG.error("[{}] Error receiving message from pulsar consumer", spoutId, e);
}
}
}
Aggregations