Search in sources :

Example 1 with UnknownError

use of org.apache.pulsar.websocket.WebSocketError.UnknownError in project incubator-pulsar by apache.

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;
    }
    final long msgSize = rawPayload.length;
    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<byte[]> msg = builder.build();
    final long now = System.nanoTime();
    producer.sendAsync(msg).thenAccept(msgId -> {
        updateSentMsgStats(msgSize, TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - now));
        if (isConnected()) {
            String messageId = Base64.getEncoder().encodeToString(msgId.toByteArray());
            sendAckResponse(new ProducerAck(messageId, sendRequest.context));
        }
    }).exceptionally(exception -> {
        numMsgsFailed.increment();
        sendAckResponse(new ProducerAck(UnknownError, exception.getMessage(), null, sendRequest.context));
        return null;
    });
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Producer(org.apache.pulsar.client.api.Producer) LoggerFactory(org.slf4j.LoggerFactory) Message(org.apache.pulsar.client.api.Message) Enums(com.google.common.base.Enums) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HttpServletRequest(javax.servlet.http.HttpServletRequest) MessageRoutingMode(org.apache.pulsar.client.api.ProducerConfiguration.MessageRoutingMode) StatsBuckets(org.apache.pulsar.websocket.stats.StatsBuckets) ProducerBusyException(org.apache.pulsar.client.api.PulsarClientException.ProducerBusyException) ProducerBlockedQuotaExceededException(org.apache.pulsar.client.api.PulsarClientException.ProducerBlockedQuotaExceededException) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Logger(org.slf4j.Logger) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) CompressionType(org.apache.pulsar.client.api.CompressionType) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashingScheme(org.apache.pulsar.client.api.ProducerConfiguration.HashingScheme) IOException(java.io.IOException) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) String.format(java.lang.String.format) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) TimeUnit(java.util.concurrent.TimeUnit) PayloadEncodingError(org.apache.pulsar.websocket.WebSocketError.PayloadEncodingError) UnknownError(org.apache.pulsar.websocket.WebSocketError.UnknownError) Base64(java.util.Base64) ProducerBlockedQuotaExceededError(org.apache.pulsar.client.api.PulsarClientException.ProducerBlockedQuotaExceededError) FailedToDeserializeFromJSON(org.apache.pulsar.websocket.WebSocketError.FailedToDeserializeFromJSON) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) IOException(java.io.IOException) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Enums (com.google.common.base.Enums)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Base64 (java.util.Base64)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLongFieldUpdater (java.util.concurrent.atomic.AtomicLongFieldUpdater)1 LongAdder (java.util.concurrent.atomic.LongAdder)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AuthenticationDataSource (org.apache.pulsar.broker.authentication.AuthenticationDataSource)1 CompressionType (org.apache.pulsar.client.api.CompressionType)1 Message (org.apache.pulsar.client.api.Message)1 MessageBuilder (org.apache.pulsar.client.api.MessageBuilder)1 Producer (org.apache.pulsar.client.api.Producer)1 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)1 HashingScheme (org.apache.pulsar.client.api.ProducerConfiguration.HashingScheme)1 MessageRoutingMode (org.apache.pulsar.client.api.ProducerConfiguration.MessageRoutingMode)1 ProducerBlockedQuotaExceededError (org.apache.pulsar.client.api.PulsarClientException.ProducerBlockedQuotaExceededError)1