Search in sources :

Example 1 with ConsumerMessage

use of org.apache.pulsar.websocket.data.ConsumerMessage in project incubator-pulsar by apache.

the class ConsumerHandler method receiveMessage.

private void receiveMessage() {
    if (log.isDebugEnabled()) {
        log.debug("[{}:{}] [{}] [{}] Receive next message", request.getRemoteAddr(), request.getRemotePort(), topic, subscription);
    }
    consumer.receiveAsync().thenAccept(msg -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] [{}] Got message {}", getSession().getRemoteAddress(), topic, subscription, msg.getMessageId());
        }
        ConsumerMessage dm = new ConsumerMessage();
        dm.messageId = Base64.getEncoder().encodeToString(msg.getMessageId().toByteArray());
        dm.payload = Base64.getEncoder().encodeToString(msg.getData());
        dm.properties = msg.getProperties();
        dm.publishTime = DateFormatter.format(msg.getPublishTime());
        if (msg.getEventTime() != 0) {
            dm.eventTime = DateFormatter.format(msg.getEventTime());
        }
        if (msg.hasKey()) {
            dm.key = msg.getKey();
        }
        final long msgSize = msg.getData().length;
        try {
            getSession().getRemote().sendString(ObjectMapperFactory.getThreadLocal().writeValueAsString(dm), new WriteCallback() {

                @Override
                public void writeFailed(Throwable th) {
                    log.warn("[{}/{}] Failed to deliver msg to {} {}", consumer.getTopic(), subscription, getRemote().getInetSocketAddress().toString(), th.getMessage());
                    pendingMessages.decrementAndGet();
                    // schedule receive as one of the delivery failed
                    service.getExecutor().execute(() -> receiveMessage());
                }

                @Override
                public void writeSuccess() {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}/{}] message is delivered successfully to {} ", consumer.getTopic(), subscription, getRemote().getInetSocketAddress().toString());
                    }
                    updateDeliverMsgStat(msgSize);
                }
            });
        } catch (JsonProcessingException e) {
            close(WebSocketError.FailedToSerializeToJSON);
        }
        int pending = pendingMessages.incrementAndGet();
        if (pending < maxPendingMessages) {
            // Start next read in a separate thread to avoid recursion
            service.getExecutor().execute(() -> receiveMessage());
        }
    }).exceptionally(exception -> {
        return null;
    });
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) LoggerFactory(org.slf4j.LoggerFactory) Enums(com.google.common.base.Enums) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HttpServletRequest(javax.servlet.http.HttpServletRequest) DateFormatter(org.apache.pulsar.common.util.DateFormatter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Session(org.eclipse.jetty.websocket.api.Session) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Splitter(com.google.common.base.Splitter) ConsumerBusyException(org.apache.pulsar.client.api.PulsarClientException.ConsumerBusyException) Logger(org.slf4j.Logger) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) TimeUnit(java.util.concurrent.TimeUnit) Consumer(org.apache.pulsar.client.api.Consumer) Base64(java.util.Base64) List(java.util.List) ConsumerMessage(org.apache.pulsar.websocket.data.ConsumerMessage) MessageId(org.apache.pulsar.client.api.MessageId) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) ConsumerAck(org.apache.pulsar.websocket.data.ConsumerAck) ConsumerMessage(org.apache.pulsar.websocket.data.ConsumerMessage) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with ConsumerMessage

use of org.apache.pulsar.websocket.data.ConsumerMessage in project incubator-pulsar by apache.

the class ReaderHandler method receiveMessage.

private void receiveMessage() {
    if (log.isDebugEnabled()) {
        log.debug("[{}:{}] [{}] [{}] Receive next message", request.getRemoteAddr(), request.getRemotePort(), topic, subscription);
    }
    reader.readNextAsync().thenAccept(msg -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] [{}] Got message {}", getSession().getRemoteAddress(), topic, subscription, msg.getMessageId());
        }
        ConsumerMessage dm = new ConsumerMessage();
        dm.messageId = Base64.getEncoder().encodeToString(msg.getMessageId().toByteArray());
        dm.payload = Base64.getEncoder().encodeToString(msg.getData());
        dm.properties = msg.getProperties();
        dm.publishTime = DateFormatter.format(msg.getPublishTime());
        if (msg.getEventTime() != 0) {
            dm.eventTime = DateFormatter.format(msg.getEventTime());
        }
        if (msg.hasKey()) {
            dm.key = msg.getKey();
        }
        final long msgSize = msg.getData().length;
        try {
            getSession().getRemote().sendString(ObjectMapperFactory.getThreadLocal().writeValueAsString(dm), new WriteCallback() {

                @Override
                public void writeFailed(Throwable th) {
                    log.warn("[{}/{}] Failed to deliver msg to {} {}", reader.getTopic(), subscription, getRemote().getInetSocketAddress().toString(), th.getMessage());
                    pendingMessages.decrementAndGet();
                    // schedule receive as one of the delivery failed
                    service.getExecutor().execute(() -> receiveMessage());
                }

                @Override
                public void writeSuccess() {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}/{}] message is delivered successfully to {} ", reader.getTopic(), subscription, getRemote().getInetSocketAddress().toString());
                    }
                    updateDeliverMsgStat(msgSize);
                }
            });
        } catch (JsonProcessingException e) {
            close(WebSocketError.FailedToSerializeToJSON);
        }
        int pending = pendingMessages.incrementAndGet();
        if (pending < maxPendingMessages) {
            // Start next read in a separate thread to avoid recursion
            service.getExecutor().execute(() -> receiveMessage());
        }
    }).exceptionally(exception -> {
        log.warn("[{}/{}] Failed to deliver msg to {} {}", reader.getTopic(), subscription, getRemote().getInetSocketAddress().toString(), exception);
        return null;
    });
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) LoggerFactory(org.slf4j.LoggerFactory) ReaderImpl(org.apache.pulsar.client.impl.ReaderImpl) HttpServletRequest(javax.servlet.http.HttpServletRequest) DateFormatter(org.apache.pulsar.common.util.DateFormatter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Session(org.eclipse.jetty.websocket.api.Session) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Logger(org.slf4j.Logger) HttpServletResponse(javax.servlet.http.HttpServletResponse) Reader(org.apache.pulsar.client.api.Reader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) Consumer(org.apache.pulsar.client.api.Consumer) Base64(java.util.Base64) ConsumerMessage(org.apache.pulsar.websocket.data.ConsumerMessage) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) MessageId(org.apache.pulsar.client.api.MessageId) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) ReaderBuilder(org.apache.pulsar.client.api.ReaderBuilder) ConsumerMessage(org.apache.pulsar.websocket.data.ConsumerMessage) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 Base64 (java.util.Base64)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLongFieldUpdater (java.util.concurrent.atomic.AtomicLongFieldUpdater)2 LongAdder (java.util.concurrent.atomic.LongAdder)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AuthenticationDataSource (org.apache.pulsar.broker.authentication.AuthenticationDataSource)2 Consumer (org.apache.pulsar.client.api.Consumer)2 MessageId (org.apache.pulsar.client.api.MessageId)2 SubscriptionType (org.apache.pulsar.client.api.SubscriptionType)2 DateFormatter (org.apache.pulsar.common.util.DateFormatter)2 ObjectMapperFactory (org.apache.pulsar.common.util.ObjectMapperFactory)2 ConsumerMessage (org.apache.pulsar.websocket.data.ConsumerMessage)2 Session (org.eclipse.jetty.websocket.api.Session)2 WriteCallback (org.eclipse.jetty.websocket.api.WriteCallback)2 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2