Search in sources :

Example 1 with WriteCallback

use of org.eclipse.jetty.websocket.api.WriteCallback in project jetty.project by eclipse.

the class DeflateFrameExtensionTest method testCompressAndDecompressBigPayload.

@Test
public void testCompressAndDecompressBigPayload() throws Exception {
    byte[] input = new byte[1024 * 1024];
    // Make them not compressible.
    new Random().nextBytes(input);
    int maxMessageSize = (1024 * 1024) + 8192;
    DeflateFrameExtension clientExtension = new DeflateFrameExtension();
    clientExtension.setBufferPool(bufferPool);
    clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
    clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    clientExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
    serverExtension.setBufferPool(bufferPool);
    serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
    serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    serverExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    // Chain the next element to decompress.
    clientExtension.setNextOutgoingFrames(new OutgoingFrames() {

        @Override
        public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode) {
            LOG.debug("outgoingFrame({})", frame);
            serverExtension.incomingFrame(frame);
            callback.writeSuccess();
        }
    });
    final ByteArrayOutputStream result = new ByteArrayOutputStream(input.length);
    serverExtension.setNextIncomingFrames(new IncomingFrames() {

        @Override
        public void incomingFrame(Frame frame) {
            LOG.debug("incomingFrame({})", frame);
            try {
                result.write(BufferUtil.toArray(frame.getPayload()));
            } catch (IOException x) {
                throw new RuntimeIOException(x);
            }
        }

        @Override
        public void incomingError(Throwable t) {
        }
    });
    BinaryFrame frame = new BinaryFrame();
    frame.setPayload(input);
    frame.setFin(true);
    clientExtension.outgoingFrame(frame, null, BatchMode.OFF);
    Assert.assertArrayEquals(input, result.toByteArray());
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) BatchMode(org.eclipse.jetty.websocket.api.BatchMode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) Random(java.util.Random) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 2 with WriteCallback

use of org.eclipse.jetty.websocket.api.WriteCallback in project joynr by bmwcarit.

the class WebSocketJettyClient method writeBytes.

@Override
public synchronized void writeBytes(Address to, byte[] message, long timeout, TimeUnit unit, final SuccessAction successAction, final FailureAction failureAction) {
    if (messageListener == null) {
        throw new JoynrDelayMessageException(20, "WebSocket write failed: receiver has not been set yet");
    }
    if (sessionFuture == null) {
        try {
            reconnect();
        } catch (Exception e) {
            throw new JoynrDelayMessageException(10, "WebSocket reconnect failed. Will try later", e);
        }
    }
    try {
        Session session = sessionFuture.get(timeout, unit);
        session.getRemote().sendBytes(ByteBuffer.wrap(message), new WriteCallback() {

            @Override
            public void writeSuccess() {
                successAction.execute();
            }

            @Override
            public void writeFailed(Throwable error) {
                if (error instanceof WebSocketException) {
                    reconnect();
                    failureAction.execute(new JoynrDelayMessageException(reconnectDelay, error.getMessage()));
                } else {
                    failureAction.execute(error);
                }
            }
        });
    } catch (WebSocketException | ExecutionException e) {
        reconnect();
        throw new JoynrDelayMessageException(10, "WebSocket write timed out", e);
    } catch (TimeoutException e) {
        throw new JoynrDelayMessageException("WebSocket write timed out", e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) ExecutionException(java.util.concurrent.ExecutionException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) TimeoutException(java.util.concurrent.TimeoutException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) Session(org.eclipse.jetty.websocket.api.Session) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with WriteCallback

use of org.eclipse.jetty.websocket.api.WriteCallback 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 4 with WriteCallback

use of org.eclipse.jetty.websocket.api.WriteCallback 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)

Example 5 with WriteCallback

use of org.eclipse.jetty.websocket.api.WriteCallback in project joynr by bmwcarit.

the class WebSocketJettyServer method writeBytes.

@Override
public synchronized void writeBytes(Address toAddress, byte[] message, long timeout, TimeUnit unit, final SuccessAction successAction, final FailureAction failureAction) {
    if (!(toAddress instanceof WebSocketClientAddress)) {
        throw new JoynrIllegalStateException("Web Socket Server can only send to WebSocketClientAddresses");
    }
    WebSocketClientAddress toClientAddress = (WebSocketClientAddress) toAddress;
    Session session = sessionMap.get(toClientAddress.getId());
    if (session == null) {
        // TODO We need a delay with invalidation of the stub
        throw new JoynrDelayMessageException("no active session for WebSocketClientAddress: " + toClientAddress.getId());
    }
    try {
        session.getRemote().sendBytes(ByteBuffer.wrap(message), new WriteCallback() {

            @Override
            public void writeSuccess() {
                successAction.execute();
            }

            @Override
            public void writeFailed(Throwable error) {
                if (shutdown) {
                    return;
                }
                failureAction.execute(error);
            }
        });
    } catch (WebSocketException e) {
        // Jetty throws WebSocketException when expecting [OPEN or CONNECTED] but found a different state
        // The client must reconnect, but the message can be queued in the mean time.
        sessionMap.remove(toClientAddress.getId());
        // TODO We need a delay with invalidation of the stub
        throw new JoynrDelayMessageException(e.getMessage(), e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) WebSocketClientAddress(joynr.system.RoutingTypes.WebSocketClientAddress) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) Session(org.eclipse.jetty.websocket.api.Session)

Aggregations

WriteCallback (org.eclipse.jetty.websocket.api.WriteCallback)5 IOException (java.io.IOException)4 Session (org.eclipse.jetty.websocket.api.Session)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)2 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)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 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)2