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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.Channel#writeAndFlush() . 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: HttpTestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a request to to the server.
 *
 * <pre>
 * HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/overview");
 * request.headers().set(HttpHeaders.Names.HOST, host);
 * request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
 *
 * sendRequest(request);
 * </pre>
 *
 * @param request The {@link HttpRequest} to send to the server
 */
public void sendRequest(HttpRequest request, FiniteDuration timeout) throws InterruptedException, TimeoutException {
	LOG.debug("Writing {}.", request);

	// Make the connection attempt.
	ChannelFuture connect = bootstrap.connect(host, port);

	Channel channel;
	if (connect.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
		channel = connect.channel();
	}
	else {
		throw new TimeoutException("Connection failed");
	}

	channel.writeAndFlush(request);
}
 
Example 2
Source File: HttpTestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a request to to the server.
 *
 * <pre>
 * HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/overview");
 * request.headers().set(HttpHeaders.Names.HOST, host);
 * request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
 *
 * sendRequest(request);
 * </pre>
 *
 * @param request The {@link HttpRequest} to send to the server
 */
public void sendRequest(HttpRequest request, FiniteDuration timeout) throws InterruptedException, TimeoutException {
	LOG.debug("Writing {}.", request);

	// Make the connection attempt.
	ChannelFuture connect = bootstrap.connect(host, port);

	Channel channel;
	if (connect.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
		channel = connect.channel();
	}
	else {
		throw new TimeoutException("Connection failed");
	}

	channel.writeAndFlush(request);
}
 
Example 3
Source File: HttpTestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a request to to the server.
 *
 * <pre>
 * HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/overview");
 * request.headers().set(HttpHeaders.Names.HOST, host);
 * request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
 *
 * sendRequest(request);
 * </pre>
 *
 * @param request The {@link HttpRequest} to send to the server
 */
public void sendRequest(HttpRequest request, Duration timeout) throws InterruptedException, TimeoutException {
	LOG.debug("Writing {}.", request);

	// Make the connection attempt.
	ChannelFuture connect = bootstrap.connect(host, port);

	Channel channel;
	if (connect.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
		channel = connect.channel();
	}
	else {
		throw new TimeoutException("Connection failed");
	}

	channel.writeAndFlush(request);
}
 
Example 4
Source File: KeepAliveWrite.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example 5
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 6
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example 7
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 8
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
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());
}
 
Example 10
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void writeTo(Channel channel) {
	channel.writeAndFlush(httpRequest);
}
 
Example 11
Source File: ServerTransportErrorHandlingTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies remote closes trigger the release of all resources.
 */
@Test
public void testRemoteClose() throws Exception {
	final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

	final CountDownLatch sync = new CountDownLatch(1);

	final ResultPartitionManager partitionManager = mock(ResultPartitionManager.class);

	when(partitionManager
		.createSubpartitionView(any(ResultPartitionID.class), anyInt(), any(BufferAvailabilityListener.class)))
		.thenAnswer(new Answer<ResultSubpartitionView>() {
			@Override
			public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
				BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
				listener.notifyDataAvailable();
				return new CancelPartitionRequestTest.InfiniteSubpartitionView(outboundBuffers, sync);
			}
		});

	NettyProtocol protocol = new NettyProtocol(partitionManager, mock(TaskEventDispatcher.class), true) {

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[]{
				new NettyMessage.NettyMessageEncoder(),
				// Close on read
				new ChannelInboundHandlerAdapter() {
					@Override
					public void channelRead(ChannelHandlerContext ctx, Object msg)
						throws Exception {

						ctx.channel().close();
					}
				}
			};
		}
	};

	NettyTestUtil.NettyServerAndClient serverAndClient = null;

	try {
		serverAndClient = initServerAndClient(protocol, createConfig());

		Channel ch = connect(serverAndClient);

		// Write something to trigger close by server
		ch.writeAndFlush(new NettyMessage.PartitionRequest(new ResultPartitionID(), 0, new InputChannelID(), Integer.MAX_VALUE));

		// Wait for the notification
		if (!sync.await(TestingUtils.TESTING_DURATION().toMillis(), TimeUnit.MILLISECONDS)) {
			fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION().toMillis() +
				" ms to be notified about released partition.");
		}
	} finally {
		shutdown(serverAndClient);
	}
}
 
Example 12
Source File: RestClient.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void writeTo(Channel channel) {
	channel.writeAndFlush(httpRequest);
}
 
Example 13
Source File: ServerTransportErrorHandlingTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies remote closes trigger the release of all resources.
 */
@Test
public void testRemoteClose() throws Exception {
	final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

	final CountDownLatch sync = new CountDownLatch(1);

	final ResultPartitionManager partitionManager = mock(ResultPartitionManager.class);

	when(partitionManager
		.createSubpartitionView(any(ResultPartitionID.class), anyInt(), any(BufferAvailabilityListener.class)))
		.thenAnswer(new Answer<ResultSubpartitionView>() {
			@Override
			public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
				BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
				listener.notifyDataAvailable();
				return new CancelPartitionRequestTest.InfiniteSubpartitionView(outboundBuffers, sync);
			}
		});

	NettyProtocol protocol = new NettyProtocol(partitionManager, mock(TaskEventDispatcher.class), true) {

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[]{
				new NettyMessage.NettyMessageEncoder(),
				// Close on read
				new ChannelInboundHandlerAdapter() {
					@Override
					public void channelRead(ChannelHandlerContext ctx, Object msg)
						throws Exception {

						ctx.channel().close();
					}
				}
			};
		}
	};

	NettyTestUtil.NettyServerAndClient serverAndClient = null;

	try {
		serverAndClient = initServerAndClient(protocol, createConfig());

		Channel ch = connect(serverAndClient);

		// Write something to trigger close by server
		ch.writeAndFlush(new NettyMessage.PartitionRequest(new ResultPartitionID(), 0, new InputChannelID(), Integer.MAX_VALUE));

		// Wait for the notification
		if (!sync.await(TestingUtils.TESTING_DURATION().toMillis(), TimeUnit.MILLISECONDS)) {
			fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION().toMillis() +
				" ms to be notified about released partition.");
		}
	} finally {
		shutdown(serverAndClient);
	}
}
 
Example 14
Source File: RestClient.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void writeTo(Channel channel) {
	channel.writeAndFlush(httpRequest);
}
 
Example 15
Source File: ServerTransportErrorHandlingTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies remote closes trigger the release of all resources.
 */
@Test
public void testRemoteClose() throws Exception {
	final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

	final CountDownLatch sync = new CountDownLatch(1);

	final ResultPartitionManager partitionManager = mock(ResultPartitionManager.class);

	when(partitionManager
		.createSubpartitionView(any(ResultPartitionID.class), anyInt(), any(BufferAvailabilityListener.class)))
		.thenAnswer(new Answer<ResultSubpartitionView>() {
			@Override
			public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
				BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
				listener.notifyDataAvailable();
				return new CancelPartitionRequestTest.InfiniteSubpartitionView(outboundBuffers, sync);
			}
		});

	NettyProtocol protocol = new NettyProtocol(partitionManager, mock(TaskEventDispatcher.class)) {

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[]{
				new NettyMessage.NettyMessageEncoder(),
				// Close on read
				new ChannelInboundHandlerAdapter() {
					@Override
					public void channelRead(ChannelHandlerContext ctx, Object msg)
						throws Exception {

						ctx.channel().close();
					}
				}
			};
		}
	};

	NettyTestUtil.NettyServerAndClient serverAndClient = null;

	try {
		serverAndClient = initServerAndClient(protocol, createConfig());

		Channel ch = connect(serverAndClient);

		// Write something to trigger close by server
		ch.writeAndFlush(new NettyMessage.PartitionRequest(new ResultPartitionID(), 0, new InputChannelID(), Integer.MAX_VALUE));

		// Wait for the notification
		if (!sync.await(TestingUtils.TESTING_DURATION().toMillis(), TimeUnit.MILLISECONDS)) {
			fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION().toMillis() +
				" ms to be notified about released partition.");
		}
	} finally {
		shutdown(serverAndClient);
	}
}