static <S> S create(Class<S> serviceType, RetryPolicy retryPolicy, ServicePool<S> pool, PartitionContextSupplier partitionContextSupplier, boolean shutdownPoolOnClose) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class<?>[] interfaces = shutdownPoolOnClose ? new Class<?>[] {serviceType, Closeable.class} : new Class<?>[] {serviceType}; ServicePoolProxy<S> proxy = new ServicePoolProxy<S>( serviceType, retryPolicy, pool, partitionContextSupplier, shutdownPoolOnClose); return serviceType.cast(Proxy.newProxyInstance(loader, interfaces, proxy)); }
/** * Builds a dynamic proxy that wraps a {@code ServicePool} and implements the service interface directly. This is * appropriate for stateless services where it's sensible for the same retry policy to apply to every method. * <p/> * It is the caller's responsibility to shutdown the service pool when they're done with it by casting the proxy * to {@link java.io.Closeable} and calling the {@link java.io.Closeable#close()} method. * * @param retryPolicy The retry policy to apply for every service call. * @return The dynamic proxy instance that implements the service interface {@code S} and the * {@link java.io.Closeable} interface. */ public S buildProxy(RetryPolicy retryPolicy) { return ServicePoolProxy.create(_serviceType, retryPolicy, build(), _partitionContextSupplier, true); }
/** * Returns the {@link com.bazaarvoice.ostrich.ServicePool} used by the specified dynamic service proxy. This can be * used for various reasons, including making a service call using non-standard retry parameters or getting access * to service pool statistics. * @param dynamicProxy A service pool dynamic proxy created by {@link ServicePoolBuilder#buildProxy}. * @param <S> The service interface type. * @return The {@link com.bazaarvoice.ostrich.ServicePool} used by the specified dynamic service proxy. */ public static <S> com.bazaarvoice.ostrich.ServicePool<S> getPool(S dynamicProxy) { checkNotNull(dynamicProxy); checkArgument(isProxy(dynamicProxy)); @SuppressWarnings("unchecked") ServicePoolProxy<S> poolProxy = (ServicePoolProxy<S>) Proxy.getInvocationHandler(dynamicProxy); return poolProxy.getServicePool(); } }
/** * Returns the {@link com.bazaarvoice.ostrich.ServicePool} used by the specified dynamic service proxy. This can be * used for various reasons, including making a service call using non-standard retry parameters or getting access * to service pool statistics. * @param dynamicProxy A service pool dynamic proxy created by {@link ServicePoolBuilder#buildProxy}. * @param <S> The service interface type. * @return The {@link com.bazaarvoice.ostrich.ServicePool} used by the specified dynamic service proxy. */ public static <S> com.bazaarvoice.ostrich.ServicePool<S> getPool(S dynamicProxy) { checkNotNull(dynamicProxy); checkArgument(isProxy(dynamicProxy)); @SuppressWarnings("unchecked") ServicePoolProxy<S> poolProxy = (ServicePoolProxy<S>) Proxy.getInvocationHandler(dynamicProxy); return poolProxy.getServicePool(); } }
static <S> S create(Class<S> serviceType, RetryPolicy retryPolicy, ServicePool<S> pool, PartitionContextSupplier partitionContextSupplier, boolean shutdownPoolOnClose) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class<?>[] interfaces = shutdownPoolOnClose ? new Class<?>[] {serviceType, Closeable.class} : new Class<?>[] {serviceType}; ServicePoolProxy<S> proxy = new ServicePoolProxy<S>( serviceType, retryPolicy, pool, partitionContextSupplier, shutdownPoolOnClose); return serviceType.cast(Proxy.newProxyInstance(loader, interfaces, proxy)); }
/** * Builds a dynamic proxy that wraps a {@code ServicePool} and implements the service interface directly. This is * appropriate for stateless services where it's sensible for the same retry policy to apply to every method. * <p/> * It is the caller's responsibility to shutdown the service pool when they're done with it by casting the proxy * to {@link java.io.Closeable} and calling the {@link java.io.Closeable#close()} method. * * @param retryPolicy The retry policy to apply for every service call. * @return The dynamic proxy instance that implements the service interface {@code S} and the * {@link java.io.Closeable} interface. */ public S buildProxy(RetryPolicy retryPolicy) { return ServicePoolProxy.create(_serviceType, retryPolicy, build(), _partitionContextSupplier, true); }