@Provides @Singleton public Publisher publisher() throws IOException { // Publish request based on request size, messages count & time since last publish BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setElementCountThreshold(messageCountBatchSize) .setRequestByteThreshold(requestBytesThreshold) .setDelayThreshold(publishDelayThreshold) .build(); ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder() .setExecutorThreadCount(1).build(); final Publisher.Builder publisher = Publisher.newBuilder(topicName()) .setBatchingSettings(batchingSettings) .setExecutorProvider(executorProvider); final String emulatorHost = System.getenv("PUBSUB_EMULATOR_HOST"); if (emulatorHost != null) { ManagedChannel channel = ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build(); TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel)); publisher.setChannelProvider(channelProvider); publisher.setCredentialsProvider(NoCredentialsProvider.create()); } return publisher.build(); }
@Test(expected = IllegalStateException.class) public void testFailedChannel_fatalError_subscriberFails() throws Exception { Subscriber subscriber = startSubscriber( getTestSubscriberBuilder(testReceiver) .setSystemExecutorProvider( InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(10).build())); // Fatal error fakeSubscriberServiceImpl.sendError(new StatusException(Status.INVALID_ARGUMENT)); try { subscriber.awaitTerminated(); } finally { // The subscriber must finish with an state error because its FAILED status. assertEquals(Subscriber.State.FAILED, subscriber.state()); Throwable t = subscriber.failureCause(); assertTrue(t instanceof ApiException); ApiException ex = (ApiException) (t); assertTrue(ex.getStatusCode() instanceof GrpcStatusCode); GrpcStatusCode grpcCode = (GrpcStatusCode) ex.getStatusCode(); assertEquals(StatusCode.Code.INVALID_ARGUMENT, grpcCode.getCode()); } }
@Test public void testFailedChannel_recoverableError_channelReopened() throws Exception { int expectedChannelCount = 1; Subscriber subscriber = startSubscriber( getTestSubscriberBuilder(testReceiver) .setSystemExecutorProvider( InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build())); // Recoverable error fakeSubscriberServiceImpl.sendError(new StatusException(Status.INTERNAL)); assertEquals(1, fakeSubscriberServiceImpl.waitForClosedStreams(1)); assertEquals( expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount)); subscriber.stopAsync().awaitTerminated(); }
public Publisher getSingleThreadedPublisher(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_concurrency_control] // create a publisher with a single threaded executor ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build(); Publisher publisher = Publisher.newBuilder(topicName).setExecutorProvider(executorProvider).build(); // [END pubsub_publisher_concurrency_control] return publisher; }
private Subscriber createSingleThreadedSubscriber() throws Exception { // [START pubsub_subscriber_concurrency_control] // provide a separate executor service for polling ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build(); Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver) .setExecutorProvider(executorProvider) .build(); // [END pubsub_subscriber_concurrency_control] return subscriber; }