Search in sources :

Example 1 with ProducerMessage

use of com.yahoo.pulsar.websocket.data.ProducerMessage in project pulsar by yahoo.

the class SimpleProducerSocket method getTestJsonPayload.

private static String getTestJsonPayload(int index) throws JsonProcessingException {
    ProducerMessage msg = new ProducerMessage();
    msg.payload = Base64.getEncoder().encodeToString(("test" + index).getBytes());
    msg.key = Integer.toString(index);
    return jsonMapper().writeValueAsString(msg);
}
Also used : ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage)

Example 2 with ProducerMessage

use of com.yahoo.pulsar.websocket.data.ProducerMessage 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

ProducerMessage (com.yahoo.pulsar.websocket.data.ProducerMessage)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CompressionType (com.yahoo.pulsar.client.api.CompressionType)1 Message (com.yahoo.pulsar.client.api.Message)1 MessageBuilder (com.yahoo.pulsar.client.api.MessageBuilder)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 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 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Base64 (java.util.Base64)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Session (org.eclipse.jetty.websocket.api.Session)1 Logger (org.slf4j.Logger)1