use of com.yahoo.pulsar.client.api.MessageBuilder in project pulsar by yahoo.
the class CmdProduce method generateMessages.
/*
* Generates a list of messages that can be produced
*
* @param stringMessages List of strings to send as messages
*
* @param messageFileNames List of file names to read and send as messages
*
* @return list of messages to send
*/
private List<Message> generateMessages(List<byte[]> messageBodies) {
List<Message> messagesToSend = new ArrayList<Message>();
try {
for (byte[] msgBody : messageBodies) {
MessageBuilder msgBuilder = MessageBuilder.create();
msgBuilder.setContent(msgBody);
messagesToSend.add(msgBuilder.build());
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return messagesToSend;
}
use of com.yahoo.pulsar.client.api.MessageBuilder in project pulsar by yahoo.
the class ProducerHandler method onWebSocketText.
@Override
public void onWebSocketText(String message) {
ProducerMessage sendRequest;
byte[] rawPayload = null;
String requestContext = null;
try {
sendRequest = ObjectMapperFactory.getThreadLocal().readValue(message, ProducerMessage.class);
requestContext = sendRequest.context;
rawPayload = Base64.getDecoder().decode(sendRequest.payload);
} catch (IOException e) {
sendAckResponse(new ProducerAck(FailedToDeserializeFromJSON, e.getMessage(), null, null));
return;
} catch (IllegalArgumentException e) {
String msg = format("Invalid Base64 message-payload error=%s", e.getMessage());
sendAckResponse(new ProducerAck(PayloadEncodingError, msg, null, requestContext));
return;
}
MessageBuilder builder = MessageBuilder.create().setContent(rawPayload);
if (sendRequest.properties != null) {
builder.setProperties(sendRequest.properties);
}
if (sendRequest.key != null) {
builder.setKey(sendRequest.key);
}
if (sendRequest.replicationClusters != null) {
builder.setReplicationClusters(sendRequest.replicationClusters);
}
Message msg = builder.build();
producer.sendAsync(msg).thenAccept(msgId -> {
if (isConnected()) {
String messageId = Base64.getEncoder().encodeToString(msgId.toByteArray());
sendAckResponse(new ProducerAck(messageId, sendRequest.context));
}
}).exceptionally(exception -> {
sendAckResponse(new ProducerAck(UnknownError, exception.getMessage(), null, sendRequest.context));
return null;
});
}
Aggregations