use of com.google.cloud.pubsublite.TopicPath in project beam by apache.
the class ReadWriteIT method createTopic.
private TopicPath createTopic(ProjectId id) throws Exception {
TopicPath toReturn = TopicPath.newBuilder().setProject(id).setLocation(ZONE).setName(TopicName.of(randomName())).build();
Topic.Builder topic = Topic.newBuilder().setName(toReturn.toString());
topic.getPartitionConfigBuilder().setCount(2).setCapacity(Capacity.newBuilder().setPublishMibPerSec(4).setSubscribeMibPerSec(4));
topic.getRetentionConfigBuilder().setPerPartitionBytes(30 * (1L << 30));
cleanupActions.addLast(() -> {
try (AdminClient client = newAdminClient()) {
client.deleteTopic(toReturn).get();
} catch (Throwable t) {
LOG.error("Failed to clean up topic.", t);
}
});
try (AdminClient client = newAdminClient()) {
client.createTopic(topic.build()).get();
}
return toReturn;
}
use of com.google.cloud.pubsublite.TopicPath in project beam by apache.
the class ReadWriteIT method testReadWrite.
@Test
public void testReadWrite() throws Exception {
pipeline.getOptions().as(StreamingOptions.class).setStreaming(true);
pipeline.getOptions().as(TestPipelineOptions.class).setBlockOnRun(false);
TopicPath topic = createTopic(getProject(pipeline.getOptions()));
SubscriptionPath subscription = null;
Exception lastException = null;
for (int i = 0; i < 30; ++i) {
// Sleep for topic creation to propagate.
Thread.sleep(1000);
try {
subscription = createSubscription(topic);
break;
} catch (Exception e) {
lastException = e;
LOG.info("Retrying exception on subscription creation.", e);
}
}
if (subscription == null) {
throw lastException;
}
// Publish some messages
writeMessages(topic, pipeline);
// Read some messages. They should be deduplicated by the time we see them, so there should be
// exactly numMessages, one for every index in [0,MESSAGE_COUNT).
PCollection<SequencedMessage> messages = readMessages(subscription, pipeline);
PCollection<Integer> ids = messages.apply(MapElements.via(extractIds()));
ids.apply("PubsubSignalTest", signal.signalSuccessWhen(BigEndianIntegerCoder.of(), testIds()));
Supplier<Void> start = signal.waitForStart(Duration.standardMinutes(5));
pipeline.apply(signal.signalStart());
PipelineResult job = pipeline.run();
start.get();
LOG.info("Running!");
signal.waitForSuccess(Duration.standardMinutes(5));
// A runner may not support cancel
try {
job.cancel();
} catch (UnsupportedOperationException exc) {
// noop
}
}
Aggregations