Java Code Examples for org.springframework.util.concurrent.SettableListenableFuture#setException()

The following examples show how to use org.springframework.util.concurrent.SettableListenableFuture#setException() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   File: SockJsClient.java    License: MIT License 6 votes vote down vote up
@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 + "'");
	}

	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;
}
 
Example 2
@Override
public ListenableFuture<Void> send(Message<byte[]> message) {
	updateLastWriteTime();
	SettableListenableFuture<Void> future = new SettableListenableFuture<>();
	try {
		WebSocketSession session = this.session;
		Assert.state(session != null, "No WebSocketSession available");
		session.sendMessage(this.codec.encode(message, session.getClass()));
		future.set(null);
	}
	catch (Throwable ex) {
		future.setException(ex);
	}
	finally {
		updateLastWriteTime();
	}
	return future;
}
 
Example 3
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
	final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture<>();

	ChannelFutureListener connectionListener = future -> {
		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;
}
 
Example 4
@Test
public void sendWithExecutionException() {
	this.session.afterConnected(this.connection);
	assertTrue(this.session.isConnected());

	IllegalStateException exception = new IllegalStateException("simulated exception");
	SettableListenableFuture<Void> future = new SettableListenableFuture<>();
	future.setException(exception);

	when(this.connection.send(any())).thenReturn(future);
	this.expected.expect(MessageDeliveryException.class);
	this.expected.expectCause(Matchers.sameInstance(exception));

	this.session.send("/topic/foo", "sample payload".getBytes(StandardCharsets.UTF_8));

	verifyNoMoreInteractions(this.connection);
}
 
Example 5
Source Project: java-technology-stack   File: SockJsClient.java    License: MIT License 6 votes vote down vote up
@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 + "'");
	}

	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;
}
 
Example 6
@Override
public ListenableFuture<Void> send(Message<byte[]> message) {
	updateLastWriteTime();
	SettableListenableFuture<Void> future = new SettableListenableFuture<>();
	try {
		WebSocketSession session = this.session;
		Assert.state(session != null, "No WebSocketSession available");
		session.sendMessage(this.codec.encode(message, session.getClass()));
		future.set(null);
	}
	catch (Throwable ex) {
		future.setException(ex);
	}
	finally {
		updateLastWriteTime();
	}
	return future;
}
 
Example 7
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
	final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture<>();

	ChannelFutureListener connectionListener = future -> {
		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;
}
 
Example 8
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
	final SettableListenableFuture<ClientHttpResponse> responseFuture =
			new SettableListenableFuture<ClientHttpResponse>();

	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;
}
 
Example 9
@Test
public void sendWithExecutionException() throws Exception {

	this.session.afterConnected(this.connection);
	assertTrue(this.session.isConnected());

	IllegalStateException exception = new IllegalStateException("simulated exception");
	SettableListenableFuture<Void> future = new SettableListenableFuture<>();
	future.setException(exception);

	when(this.connection.send(any())).thenReturn(future);
	this.expected.expect(MessageDeliveryException.class);
	this.expected.expectCause(Matchers.sameInstance(exception));

	this.session.send("/topic/foo", "sample payload".getBytes(UTF_8));

	verifyNoMoreInteractions(this.connection);
}
 
Example 10
@Override
public final ListenableFuture<WebSocketSession> doHandshake(
		WebSocketHandler handler, 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 + "'");
	}

	SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<WebSocketSession>();
	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;
}
 
Example 11
@Override
public ListenableFuture<Void> send(Message<byte[]> message) {
	updateLastWriteTime();
	SettableListenableFuture<Void> future = new SettableListenableFuture<Void>();
	try {
		this.session.sendMessage(this.codec.encode(message, this.session.getClass()));
		future.set(null);
	}
	catch (Throwable ex) {
		future.setException(ex);
	}
	finally {
		updateLastWriteTime();
	}
	return future;
}
 
Example 12
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
	final SettableListenableFuture<ClientHttpResponse> responseFuture =
			new SettableListenableFuture<ClientHttpResponse>();

	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;
}
 
Example 13
Source Project: haven-platform   File: NettyRequest.java    License: Apache License 2.0 6 votes vote down vote up
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) {
    final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture<>();

    ChannelFutureListener connectionListener = future -> {
        if (future.isSuccess()) {
            Channel channel = future.channel();
            channel.pipeline().addLast(new NettyResponseHandler(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;
}
 
Example 14
@Test
public void sendWithExecutionException() {
	this.session.afterConnected(this.connection);
	assertTrue(this.session.isConnected());

	IllegalStateException exception = new IllegalStateException("simulated exception");
	SettableListenableFuture<Void> future = new SettableListenableFuture<>();
	future.setException(exception);

	given(this.connection.send(any())).willReturn(future);
	assertThatExceptionOfType(MessageDeliveryException.class).isThrownBy(() ->
			this.session.send("/topic/foo", "sample payload".getBytes(StandardCharsets.UTF_8)))
		.withCause(exception);
}
 
Example 15
@Override
public ListenableFuture<Void> ack() {
	SettableListenableFuture<Void> settableListenableFuture = new SettableListenableFuture<>();

	try {
		this.ackReplyConsumer.ack();
		settableListenableFuture.set(null);
	}
	catch (Throwable throwable) {
		settableListenableFuture.setException(throwable);
	}

	return settableListenableFuture;
}
 
Example 16
@Override
public ListenableFuture<Void> nack() {
	SettableListenableFuture<Void> settableListenableFuture = new SettableListenableFuture<>();

	try {
		this.ackReplyConsumer.nack();
		settableListenableFuture.set(null);
	}
	catch (Throwable throwable) {
		settableListenableFuture.setException(throwable);
	}

	return settableListenableFuture;
}