Search in sources :

Example 1 with MessageBuilder

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;
}
Also used : Message(com.yahoo.pulsar.client.api.Message) MessageBuilder(com.yahoo.pulsar.client.api.MessageBuilder) ArrayList(java.util.ArrayList) ParameterException(com.beust.jcommander.ParameterException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 2 with MessageBuilder

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;
    });
}
Also used : ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage) WebSocketError(com.yahoo.pulsar.websocket.WebSocketError) Logger(org.slf4j.Logger) Producer(com.yahoo.pulsar.client.api.Producer) LoggerFactory(org.slf4j.LoggerFactory) MessageRoutingMode(com.yahoo.pulsar.client.api.ProducerConfiguration.MessageRoutingMode) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) String.format(java.lang.String.format) ObjectMapperFactory(com.yahoo.pulsar.common.util.ObjectMapperFactory) TimeUnit(java.util.concurrent.TimeUnit) MessageBuilder(com.yahoo.pulsar.client.api.MessageBuilder) Base64(java.util.Base64) HttpServletRequest(javax.servlet.http.HttpServletRequest) CompressionType(com.yahoo.pulsar.client.api.CompressionType) Session(org.eclipse.jetty.websocket.api.Session) Message(com.yahoo.pulsar.client.api.Message) ProducerAck(com.yahoo.pulsar.websocket.data.ProducerAck) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) MessageBuilder(com.yahoo.pulsar.client.api.MessageBuilder) ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage) Message(com.yahoo.pulsar.client.api.Message) ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage) IOException(java.io.IOException) ProducerAck(com.yahoo.pulsar.websocket.data.ProducerAck)

Aggregations

Message (com.yahoo.pulsar.client.api.Message)2 MessageBuilder (com.yahoo.pulsar.client.api.MessageBuilder)2 ParameterException (com.beust.jcommander.ParameterException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CompressionType (com.yahoo.pulsar.client.api.CompressionType)1 Producer (com.yahoo.pulsar.client.api.Producer)1 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)1 MessageRoutingMode (com.yahoo.pulsar.client.api.ProducerConfiguration.MessageRoutingMode)1 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)1 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)1 ObjectMapperFactory (com.yahoo.pulsar.common.util.ObjectMapperFactory)1 WebSocketError (com.yahoo.pulsar.websocket.WebSocketError)1 ProducerAck (com.yahoo.pulsar.websocket.data.ProducerAck)1 ProducerMessage (com.yahoo.pulsar.websocket.data.ProducerMessage)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1