Search in sources :

Example 6 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class WebSocketTransport method connect.

@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
    final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<>();
    WebSocketClientSockJsSession session = new WebSocketClientSockJsSession(request, handler, future);
    handler = new ClientSockJsWebSocketHandler(session);
    request.addTimeoutTask(session.getTimeoutTask());
    URI url = request.getTransportUrl();
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
    if (logger.isDebugEnabled()) {
        logger.debug("Starting WebSocket session on " + url);
    }
    this.webSocketClient.doHandshake(handler, headers, url).addCallback(new ListenableFutureCallback<WebSocketSession>() {

        @Override
        public void onSuccess(WebSocketSession webSocketSession) {
        // WebSocket session ready, SockJS Session not yet
        }

        @Override
        public void onFailure(Throwable ex) {
            future.setException(ex);
        }
    });
    return future;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 7 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class AbstractXhrTransport method connect.

// Transport methods
@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
    SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>();
    XhrClientSockJsSession session = new XhrClientSockJsSession(request, handler, this, connectFuture);
    request.addTimeoutTask(session.getTimeoutTask());
    URI receiveUrl = request.getTransportUrl();
    if (logger.isDebugEnabled()) {
        logger.debug("Starting XHR " + (isXhrStreamingDisabled() ? "Polling" : "Streaming") + "session url=" + receiveUrl);
    }
    HttpHeaders handshakeHeaders = new HttpHeaders();
    handshakeHeaders.putAll(request.getHandshakeHeaders());
    connectInternal(request, handler, receiveUrl, handshakeHeaders, session, connectFuture);
    return connectFuture;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 8 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class ReactorNettyTcpClient method shutdown.

@Override
public ListenableFuture<Void> shutdown() {
    if (this.stopping) {
        SettableListenableFuture<Void> future = new SettableListenableFuture<>();
        future.set(null);
        return future;
    }
    this.stopping = true;
    ChannelGroupFuture close = this.channelGroup.close();
    Mono<Void> completion = FutureMono.from(close).doAfterTerminate((x, e) -> {
        // TODO: https://github.com/reactor/reactor-netty/issues/24
        shutdownGlobalResources();
        this.loopResources.dispose();
        this.poolResources.dispose();
        // TODO: https://github.com/reactor/reactor-netty/issues/25
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        // Scheduler after loop resources...
        this.scheduler.dispose();
    });
    return new MonoToListenableFutureAdapter<>(completion);
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) ChannelGroupFuture(io.netty.channel.group.ChannelGroupFuture)

Example 9 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class Netty4ClientHttpRequest method executeInternal.

@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
    final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture<>();
    ChannelFutureListener connectionListener = new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                Channel channel = future.channel();
                channel.pipeline().addLast(new RequestExecuteHandler(responseFuture));
                FullHttpRequest nettyRequest = createFullHttpRequest(headers);
                channel.writeAndFlush(nettyRequest);
            } else {
                responseFuture.setException(future.cause());
            }
        }
    };
    this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);
    return responseFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Aggregations

SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)9 Test (org.junit.Test)4 MethodParameter (org.springframework.core.MethodParameter)3 URI (java.net.URI)2 WebSocketSession (org.springframework.web.socket.WebSocketSession)2 ClientApplicationRegisteredEvent (de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent)1 Application (de.codecentric.boot.admin.model.Application)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelGroupFuture (io.netty.channel.group.ChannelGroupFuture)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 Future (java.util.concurrent.Future)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SpringApplication (org.springframework.boot.SpringApplication)1 HttpHeaders (org.springframework.http.HttpHeaders)1 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)1