Java Code Examples for reactor.netty.ConnectionObserver#emptyListener()

The following examples show how to use reactor.netty.ConnectionObserver#emptyListener() . 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: HttpServerTests.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
private void doTestStatus(HttpResponseStatus status) {
	EmbeddedChannel channel = new EmbeddedChannel();
	HttpServerOperations ops = new HttpServerOperations(
			Connection.from(channel),
			ConnectionObserver.emptyListener(),
			null,
			new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"),
			null,
			ServerCookieEncoder.STRICT,
			ServerCookieDecoder.STRICT);
	ops.status(status);
	HttpMessage response = ops.newFullBodyMessage(Unpooled.EMPTY_BUFFER);
	assertThat(((FullHttpResponse) response).status().reasonPhrase()).isEqualTo(status.reasonPhrase());
	// "FutureReturnValueIgnored" is suppressed deliberately
	channel.close();
}
 
Example 2
Source File: UdpServerConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
protected ConnectionObserver defaultConnectionObserver() {
	if (channelGroup() == null && doOnBound() == null && doOnUnbound() == null) {
		return ConnectionObserver.emptyListener();
	}
	return new UdpServerDoOn(channelGroup(), doOnBound(), doOnUnbound());
}
 
Example 3
Source File: ClientTransportConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
protected ConnectionObserver defaultConnectionObserver() {
	if (channelGroup() == null && doOnConnected() == null && doOnDisconnected() == null) {
		return ConnectionObserver.emptyListener();
	}
	return new ClientTransportDoOn(channelGroup(), doOnConnected(), doOnDisconnected());
}
 
Example 4
Source File: ServerTransportConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Default ServerTransportConfig with options
 *
 * @param options default options for the selector
 * @param childOptions default options for each connected channel
 * @param bindAddress the local address
 */
protected ServerTransportConfig(Map<ChannelOption<?>, ?> options, Map<ChannelOption<?>, ?> childOptions,
			Supplier<? extends SocketAddress> bindAddress) {
	super(options, bindAddress);
	this.childAttrs = Collections.emptyMap();
	this.childObserver = ConnectionObserver.emptyListener();
	this.childOptions = Objects.requireNonNull(childOptions, "childOptions");
}
 
Example 5
Source File: ServerTransportConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Return the configured child lifecycle {@link ConnectionObserver} if any or {@link ConnectionObserver#emptyListener()}.
 *
 * @return the configured child lifecycle {@link ConnectionObserver} if any or {@link ConnectionObserver#emptyListener()}
 */
protected ConnectionObserver defaultChildObserver() {
	if (channelGroup() == null && doOnConnection() == null) {
		return ConnectionObserver.emptyListener();
	}
	else {
		return new ServerTransportDoOnConnection(channelGroup(), doOnConnection());
	}
}
 
Example 6
Source File: ServerTransportConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
protected ConnectionObserver defaultConnectionObserver() {
	if (doOnBound() == null && doOnUnbound() == null) {
		return ConnectionObserver.emptyListener();
	}
	return new ServerTransportDoOn(doOnBound(), doOnUnbound());
}
 
Example 7
Source File: TransportConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Default TransportConfig with options
 */
protected TransportConfig(Map<ChannelOption<?>, ?> options) {
	this.attrs = Collections.emptyMap();
	this.doOnChannelInit = ChannelPipelineConfigurer.emptyConfigurer();
	this.observer = ConnectionObserver.emptyListener();
	this.options = Objects.requireNonNull(options, "options");
	this.preferNative = LoopResources.DEFAULT_NATIVE;
}
 
Example 8
Source File: TransportConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Default TransportConfig with options
 */
protected TransportConfig(Map<ChannelOption<?>, ?> options, Supplier<? extends SocketAddress> bindAddress) {
	this.attrs = Collections.emptyMap();
	this.bindAddress = Objects.requireNonNull(bindAddress, "bindAddress");
	this.doOnChannelInit = ChannelPipelineConfigurer.emptyConfigurer();
	this.observer = ConnectionObserver.emptyListener();
	this.options = Objects.requireNonNull(options, "options");
	this.preferNative = LoopResources.DEFAULT_NATIVE;
}
 
Example 9
Source File: HttpClientOperationsTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructorWithProvidedReplacement() {
	EmbeddedChannel channel = new EmbeddedChannel();
	channel.pipeline().addFirst(NettyPipeline.SslHandler, new ChannelHandlerAdapter() {
	});

	HttpClientOperations ops1 = new HttpClientOperations(() -> channel,
			ConnectionObserver.emptyListener(),
			ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT);
	ops1.followRedirectPredicate((req, res) -> true);
	ops1.started = true;
	ops1.retrying = true;
	ops1.redirecting = new RedirectClientException(new DefaultHttpHeaders().add(HttpHeaderNames.LOCATION, "/"));

	HttpClientOperations ops2 = new HttpClientOperations(ops1);

	assertSame(ops1.channel(), ops2.channel());
	assertSame(ops1.started, ops2.started);
	assertSame(ops1.retrying, ops2.retrying);
	assertSame(ops1.redirecting, ops2.redirecting);
	assertSame(ops1.redirectedFrom, ops2.redirectedFrom);
	assertSame(ops1.isSecure, ops2.isSecure);
	assertSame(ops1.nettyRequest, ops2.nettyRequest);
	assertSame(ops1.responseState, ops2.responseState);
	assertSame(ops1.followRedirectPredicate, ops2.followRedirectPredicate);
	assertSame(ops1.requestHeaders, ops2.requestHeaders);
}
 
Example 10
Source File: HttpClientOperationsTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
private void doTestStatus(HttpResponseStatus status) {
	EmbeddedChannel channel = new EmbeddedChannel();
	HttpClientOperations ops = new HttpClientOperations(() -> channel,
			ConnectionObserver.emptyListener(),
			ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT);
	ops.setNettyResponse(new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.EMPTY_BUFFER));
	assertEquals(status.reasonPhrase(), ops.status().reasonPhrase());
}
 
Example 11
Source File: DefaultPooledConnectionProviderTest.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
@Test
public void fixedPoolTwoAcquire()
		throws ExecutionException, InterruptedException, IOException {
	final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
	int echoServerPort = SocketUtils.findAvailableTcpPort();
	TcpClientTests.EchoServer echoServer = new TcpClientTests.EchoServer(echoServerPort);

	java.util.concurrent.Future<?> f1 = null;
	java.util.concurrent.Future<?> f2 = null;
	ScheduledFuture<?> sf = null;
	try {
		final InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", echoServerPort);
		ConnectionProvider pool = ConnectionProvider.create("fixedPoolTwoAcquire", 2);

		Supplier<? extends SocketAddress> remoteAddress = () -> address;
		ConnectionObserver observer = ConnectionObserver.emptyListener();
		EventLoopGroup group = new NioEventLoopGroup(2);
		ClientTransportConfig<?> config =
				new ClientTransportConfigImpl(group, pool, Collections.emptyMap(), remoteAddress);

		//fail a couple
		StepVerifier.create(pool.acquire(config, observer, remoteAddress, config.resolver()))
		            .verifyErrorMatches(msg -> msg.getMessage().contains("Connection refused"));
		StepVerifier.create(pool.acquire(config, observer, remoteAddress, config.resolver()))
		            .verifyErrorMatches(msg -> msg.getMessage().contains("Connection refused"));

		//start the echo server
		f1 = service.submit(echoServer);
		Thread.sleep(100);

		//acquire 2
		final PooledConnection c1 = (PooledConnection) pool.acquire(config, observer, remoteAddress, config.resolver())
		                                                   .block(Duration.ofSeconds(30));
		assertThat(c1).isNotNull();
		final PooledConnection c2 = (PooledConnection) pool.acquire(config, observer, remoteAddress, config.resolver())
		                                                   .block(Duration.ofSeconds(30));
		assertThat(c2).isNotNull();

		//make room for 1 more
		c2.disposeNow();


		final PooledConnection c3 = (PooledConnection) pool.acquire(config, observer, remoteAddress, config.resolver())
		                                                   .block(Duration.ofSeconds(30));
		assertThat(c3).isNotNull();

		//next one will block until a previous one is released
		long start = System.currentTimeMillis();
		sf = service.schedule(() -> c1.onStateChange(c1, ConnectionObserver.State.DISCONNECTING), 500, TimeUnit.MILLISECONDS);


		final PooledConnection c4 = (PooledConnection) pool.acquire(config, observer, remoteAddress, config.resolver())
		                                                   .block(Duration.ofSeconds(30));
		assertThat(c4).isNotNull();

		long end = System.currentTimeMillis();

		assertThat(end - start)
				.as("channel4 acquire blocked until channel1 released")
				.isGreaterThanOrEqualTo(500);

		c3.onStateChange(c3, ConnectionObserver.State.DISCONNECTING);

		c4.onStateChange(c4, ConnectionObserver.State.DISCONNECTING);

		assertThat(c1).isEqualTo(c4);

		assertThat(c1.pool).isEqualTo(c2.pool)
		                   .isEqualTo(c3.pool)
		                   .isEqualTo(c4.pool);

		InstrumentedPool<PooledConnection> defaultPool = c1.pool;

		CountDownLatch latch = new CountDownLatch(1);
		f2 = service.submit(() -> {
			while (defaultPool.metrics().acquiredSize() > 0) {
				LockSupport.parkNanos(100);
			}
			latch.countDown();
		});


		assertThat(latch.await(5, TimeUnit.SECONDS))
				.as("activeConnections fully released")
				.isTrue();
	}
	finally {
		service.shutdownNow();
		echoServer.close();
		assertThat(f1).isNotNull();
		assertThat(f1.get()).isNull();
		assertThat(f2).isNotNull();
		assertThat(f2.get()).isNull();
		assertNotNull(sf);
		assertThat(sf.get()).isNull();
	}
}