Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Limiting the number of threads used by PubSub publisher
//Thanks to ajaaym https://github.com/googleapis/java-pubsub/issues/27#issuecomment-538508100
final ExecutorProvider fixedExecutorProvider = FixedExecutorProvider.create(
new ScheduledThreadPoolExecutor(1)); //adjust the size
Publisher publisher =
Publisher.newBuilder("topic_name")
.setExecutorProvider(fixedExecutorProvider)
.setChannelProvider(
PublisherStubSettings.defaultGrpcTransportProviderBuilder()
.setExecutorProvider(fixedExecutorProvider)
.setChannelConfigurator(
new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
@Override
public ManagedChannelBuilder apply(
ManagedChannelBuilder managedChannelBuilder) {
NettyChannelBuilder nettyChannelBuilder =
(NettyChannelBuilder)
managedChannelBuilder.executor(
fixedExecutorProvider.getExecutor());
nettyChannelBuilder.eventLoopGroup(
new NioEventLoopGroup(1, fixedExecutorProvider.getExecutor()));
nettyChannelBuilder.channelType(
NioSocketChannel.class); // Use EPoll if available, if using EPoll update above line to use EPollEventLoopGroup
return nettyChannelBuilder;
}
})
.build())
.build();
publisher.publish(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("testdata")).build()).get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment