2

I saw in some example code that appears to read directly from a topic?

PubsubIO.readStrings().fromTopic(fullTopic))

Are there differences between that and

PubsubIO.readStrings().fromSubscription(fullTopic))

(I was always under the impression you had to have a subscription for a topic...)

edit: Adding link to example using fromTopic

Bryce Fischer
  • 5,336
  • 9
  • 30
  • 36

1 Answers1

8

Yes, there are differences. Note that the fromSubscription method takes a path to a subscription, not a path to a topic. If one uses fromTopic, then a new subscription will be created. As a result, any messages published before the code runs will not be read. A new subscription will end up being created every time the code runs, too.

In contrast, fromSubscription uses an existing subscription, which means messages published any time after the subscription was created will be read. If the job goes down and comes back up, then the read can continue from where it left off.

Kamal Aboul-Hosn
  • 15,111
  • 1
  • 34
  • 46
  • Thanks. So in effect, a subscription is used in both cases, just that fromTopic doesn't create it until its run... Thanks. I get it now! – Bryce Fischer Nov 08 '18 at 16:33