Example usage for org.springframework.util.concurrent SettableListenableFuture SettableListenableFuture

List of usage examples for org.springframework.util.concurrent SettableListenableFuture SettableListenableFuture

Introduction

In this page you can find the example usage for org.springframework.util.concurrent SettableListenableFuture SettableListenableFuture.

Prototype

SettableListenableFuture

Source Link

Usage

From source file:org.springframework.integration.jms.JmsOutboundGateway.java

private SettableListenableFuture<AbstractIntegrationMessageBuilder<?>> createFuture(
        final String correlationId) {
    SettableListenableFuture<AbstractIntegrationMessageBuilder<?>> future = new SettableListenableFuture<AbstractIntegrationMessageBuilder<?>>();
    this.futures.put(correlationId, future);
    if (this.receiveTimeout > 0) {
        getTaskScheduler().schedule((Runnable) () -> expire(correlationId),
                new Date(System.currentTimeMillis() + this.receiveTimeout));
    }//from  w  w  w.  j  a v  a 2 s  . co  m
    return future;
}

From source file:org.springframework.kafka.core.KafkaTemplate.java

/**
 * Send the producer record.//from   w w  w .j  av  a  2 s  .co  m
 * @param producerRecord the producer record.
 * @return a Future for the {@link RecordMetadata}.
 */
protected ListenableFuture<SendResult<K, V>> doSend(final ProducerRecord<K, V> producerRecord) {
    getTheProducer();
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Sending: " + producerRecord);
    }
    final SettableListenableFuture<SendResult<K, V>> future = new SettableListenableFuture<>();
    getTheProducer().send(producerRecord, new Callback() {

        @Override
        public void onCompletion(RecordMetadata metadata, Exception exception) {
            if (exception == null) {
                future.set(new SendResult<>(producerRecord, metadata));
                if (KafkaTemplate.this.producerListener != null
                        && KafkaTemplate.this.producerListener.isInterestedInSuccess()) {
                    KafkaTemplate.this.producerListener.onSuccess(producerRecord.topic(),
                            producerRecord.partition(), producerRecord.key(), producerRecord.value(), metadata);
                }
            } else {
                future.setException(new KafkaProducerException(producerRecord, "Failed to send", exception));
                if (KafkaTemplate.this.producerListener != null) {
                    KafkaTemplate.this.producerListener.onError(producerRecord.topic(),
                            producerRecord.partition(), producerRecord.key(), producerRecord.value(),
                            exception);
                }
            }
        }

    });
    if (this.autoFlush) {
        flush();
    }
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Sent: " + producerRecord);
    }
    return future;
}

From source file:org.springframework.messaging.tcp.reactor.ReactorNettyTcpClient.java

@Override
public ListenableFuture<Void> shutdown() {
    if (this.stopping) {
        SettableListenableFuture<Void> future = new SettableListenableFuture<>();
        future.set(null);/*from  ww  w.ja v  a  2 s.  co m*/
        return future;
    }

    this.stopping = true;

    Mono<Void> result;
    if (this.channelGroup != null) {
        result = FutureMono.from(this.channelGroup.close());
        if (this.loopResources != null) {
            result = result.onErrorResume(ex -> Mono.empty()).then(this.loopResources.disposeLater());
        }
        if (this.poolResources != null) {
            result = result.onErrorResume(ex -> Mono.empty()).then(this.poolResources.disposeLater());
        }
        result = result.onErrorResume(ex -> Mono.empty()).then(stopScheduler());
    } else {
        result = stopScheduler();
    }

    return new MonoToListenableFutureAdapter<>(result);
}

From source file:org.springframework.web.socket.sockjs.client.AbstractXhrTransport.java

@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);// www.  java 2s.  c o m
    }

    HttpHeaders handshakeHeaders = new HttpHeaders();
    handshakeHeaders.putAll(request.getHandshakeHeaders());

    connectInternal(request, handler, receiveUrl, handshakeHeaders, session, connectFuture);
    return connectFuture;
}

From source file:org.springframework.web.socket.sockjs.client.SockJsClient.java

@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler,
        @Nullable WebSocketHttpHeaders headers, URI url) {

    Assert.notNull(handler, "WebSocketHandler is required");
    Assert.notNull(url, "URL is required");

    String scheme = url.getScheme();
    if (!supportedProtocols.contains(scheme)) {
        throw new IllegalArgumentException("Invalid scheme: '" + scheme + "'");
    }//w  ww . j av  a 2s  .c  o m

    SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>();
    try {
        SockJsUrlInfo sockJsUrlInfo = new SockJsUrlInfo(url);
        ServerInfo serverInfo = getServerInfo(sockJsUrlInfo, getHttpRequestHeaders(headers));
        createRequest(sockJsUrlInfo, headers, serverInfo).connect(handler, connectFuture);
    } catch (Throwable exception) {
        if (logger.isErrorEnabled()) {
            logger.error("Initial SockJS \"Info\" request to server failed, url=" + url, exception);
        }
        connectFuture.setException(exception);
    }
    return connectFuture;
}

From source file:org.springframework.web.socket.sockjs.client.WebSocketTransport.java

@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);
    }/* ww  w  .  jav  a  2  s  .  c om*/
    this.webSocketClient.doHandshake(handler, headers, url)
            .addCallback(new ListenableFutureCallback<WebSocketSession>() {
                @Override
                public void onSuccess(@Nullable WebSocketSession webSocketSession) {
                    // WebSocket session ready, SockJS Session not yet
                }

                @Override
                public void onFailure(Throwable ex) {
                    future.setException(ex);
                }
            });
    return future;
}