Java Code Examples for org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture#addListener()

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture#addListener() . 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 File: HandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example 2
Source File: HandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static CompletableFuture<Void> toCompletableFuture(final ChannelFuture channelFuture) {
	final CompletableFuture<Void> completableFuture = new CompletableFuture<>();
	channelFuture.addListener(future -> {
		if (future.isSuccess()) {
			completableFuture.complete(null);
		} else {
			completableFuture.completeExceptionally(future.cause());
		}
	});
	return completableFuture;
}
 
Example 3
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Channel channel) {
	ChannelFuture future = channel.writeAndFlush(httpRequest);
	// this should never be false as we explicitly set the encoder to use multipart messages
	if (bodyRequestEncoder.isChunked()) {
		future = channel.writeAndFlush(bodyRequestEncoder);
	}

	// release data and remove temporary files if they were created, once the writing is complete
	future.addListener((ignored) -> bodyRequestEncoder.cleanFiles());
}
 
Example 4
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example 5
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static CompletableFuture<Void> toCompletableFuture(final ChannelFuture channelFuture) {
	final CompletableFuture<Void> completableFuture = new CompletableFuture<>();
	channelFuture.addListener(future -> {
		if (future.isSuccess()) {
			completableFuture.complete(null);
		} else {
			completableFuture.completeExceptionally(future.cause());
		}
	});
	return completableFuture;
}
 
Example 6
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Channel channel) {
	ChannelFuture future = channel.writeAndFlush(httpRequest);
	// this should never be false as we explicitly set the encoder to use multipart messages
	if (bodyRequestEncoder.isChunked()) {
		future = channel.writeAndFlush(bodyRequestEncoder);
	}

	// release data and remove temporary files if they were created, once the writing is complete
	future.addListener((ignored) -> bodyRequestEncoder.cleanFiles());
}
 
Example 7
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example 8
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static CompletableFuture<Void> toCompletableFuture(final ChannelFuture channelFuture) {
	final CompletableFuture<Void> completableFuture = new CompletableFuture<>();
	channelFuture.addListener(future -> {
		if (future.isSuccess()) {
			completableFuture.complete(null);
		} else {
			completableFuture.completeExceptionally(future.cause());
		}
	});
	return completableFuture;
}
 
Example 9
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Channel channel) {
	ChannelFuture future = channel.writeAndFlush(httpRequest);
	// this should never be false as we explicitly set the encoder to use multipart messages
	if (bodyRequestEncoder.isChunked()) {
		future = channel.writeAndFlush(bodyRequestEncoder);
	}

	// release data and remove temporary files if they were created, once the writing is complete
	future.addListener((ignored) -> bodyRequestEncoder.cleanFiles());
}