public void updateChannelAttr( ChannelHandlerContext ctx, InetSocketAddress remoteAddress, Client client) { getClientMap(ctx).put(remoteAddress, client); } }
public Client getClient(ChannelHandlerContext ctx, ClientConfig config) { return getHandlerClient(ctx, config.remote()) .orElseGet( () -> { Client client = createClient(ctx, config); updateChannelAttr(ctx, config.remote(), client); return client; }); }
Client closeClient = mock(Client.class); when(closeClient.isReusable()).thenReturn(false); factory.getClientMap(ctx).put(remoteAddress, closeClient); Optional<Client> client = factory.getHandlerClient(ctx, remoteAddress); assertTrue(factory.getClientMap(ctx).isEmpty());
Client client = factory.getClient(ctx, clientConfig.get());
protected Optional<Client> getHandlerClient( ChannelHandlerContext ctx, InetSocketAddress address) { return Optional.ofNullable(getClientMap(ctx).get(address)) .flatMap( it -> { if (it.isReusable()) { return Optional.of(it); } else { getClientMap(ctx).remove(address); return Optional.empty(); } }); }