Last active
October 27, 2020 01:19
-
-
Save JoshuaFox/d10fa52666a7f729867b1f61273d02cb to your computer and use it in GitHub Desktop.
Limiting the number of threads used by PubSub publisher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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