Class PubSubTransport
- All Implemented Interfaces:
Closeable, AutoCloseable
Messaging over Google Cloud Pub/Sub's REST API, with no Google library.
cn1.messaging.url=pubsub://my-project
A destination is a topic, and a listener's group a subscription to it named
<topic>.<group>; both are created on first use. Every group therefore
sees every message, and the listeners of one group compete for them.
Pub/Sub keeps a message only for the subscriptions that exist when it is
published, so a message sent before its group ever subscribed is not
delivered to that group: create the subscriptions at deployment time, or
call declare(String, String) before the first send. Pub/Sub allows
letters, digits and -_.~+% in a name that starts with a letter; any
other character becomes -, and a name that does not start with a letter
gains a t-.
Credentials, in order: the service-account key file named by
cn1.messaging.pubsub.credentials or GOOGLE_APPLICATION_CREDENTIALS,
then the metadata server of the Google Cloud machine the server runs on.
PUBSUB_EMULATOR_HOST (or cn1.messaging.pubsub.emulator-host) sends
everything, unauthenticated, to the emulator at that host:port instead.
A message is handed to one consumer for cn1.messaging.lease milliseconds
(the subscription's ack deadline, at most ten minutes); one not settled in
that time is delivered again. A failed message is not copied, since a
copy published to the topic would reach every other group too: its
deadline is moved to when the retry is due, at most ten minutes away, and
its attempt is counted in this server's memory. After a restart, or when
another replica receives the retry, the count starts again, so a message
may be tried more often than the retry policy says before it reaches the
dead-letter topic.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRegisters this transport withMessaging; the generated wiring does it.Nested classes/interfaces inherited from class PollingTransport
PollingTransport.Delivery -
Field Summary
Fields inherited from class PollingTransport
retry -
Constructor Summary
ConstructorsConstructorDescriptionPubSubTransport(String url, String emulatorHost, String keyFile, RetryPolicy retry, int leaseMillis) A transport to the projecturlnames (pubsub://project), through the emulator atemulatorHostwhen it is set, otherwise authenticated with the key filekeyFileor, when that is null, the metadata server. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidack(String destination, String group, PollingTransport.Delivery delivery) Acknowledges a handled delivery.voidclose()Stops every subscription and closes the connection.Createsdestination's topic andgroup's subscription to it unless they exist, so messages sent from now on are kept for that group even before its listener starts.getName()The scheme this transport was opened for:memory,db,redis,amqp,kafka.booleanWhether the broker answers, for the health endpoint.protected List<PollingTransport.Delivery> Up tomaxmessages ofdestinationforgroup, waiting up towaitMillisfor the first; an empty list when none came.protected voidredeliver(String destination, String group, PollingTransport.Delivery delivery, int attempt, long notBefore) Keeps the message in its subscription, due again atnotBefore(at most ten minutes away), and remembers which attempt that will be.voidSendsmessage.Methods inherited from class PollingTransport
deadLetter, identified, notYet, stopSubscriptions, subscribeMethods inherited from class MessageTransport
isTransactional
-
Constructor Details
-
PubSubTransport
public PubSubTransport(String url, String emulatorHost, String keyFile, RetryPolicy retry, int leaseMillis) throws IOException A transport to the projecturlnames (pubsub://project), through the emulator atemulatorHostwhen it is set, otherwise authenticated with the key filekeyFileor, when that is null, the metadata server.- Throws:
IOException
-
-
Method Details
-
getName
Description copied from class:MessageTransportThe scheme this transport was opened for:memory,db,redis,amqp,kafka.- Specified by:
getNamein classMessageTransport
-
declare
Createsdestination's topic andgroup's subscription to it unless they exist, so messages sent from now on are kept for that group even before its listener starts.- Throws:
IOException
-
send
Description copied from class:MessageTransportSendsmessage. With a database transport inside a transaction the message is written in that transaction and is seen only if it commits.- Specified by:
sendin classMessageTransport- Throws:
IOException
-
poll
protected List<PollingTransport.Delivery> poll(String destination, String group, int max, long waitMillis) throws IOException Description copied from class:PollingTransportUp tomaxmessages ofdestinationforgroup, waiting up towaitMillisfor the first; an empty list when none came.- Specified by:
pollin classPollingTransport- Throws:
IOException
-
ack
protected void ack(String destination, String group, PollingTransport.Delivery delivery) throws IOException Description copied from class:PollingTransportAcknowledges a handled delivery.- Specified by:
ackin classPollingTransport- Throws:
IOException
-
redeliver
protected void redeliver(String destination, String group, PollingTransport.Delivery delivery, int attempt, long notBefore) throws IOException Keeps the message in its subscription, due again atnotBefore(at most ten minutes away), and remembers which attempt that will be.- Overrides:
redeliverin classPollingTransport- Throws:
IOException
-
isHealthy
public boolean isHealthy()Description copied from class:MessageTransportWhether the broker answers, for the health endpoint. Never throws.- Overrides:
isHealthyin classMessageTransport
-
close
public void close()Description copied from class:MessageTransportStops every subscription and closes the connection. Never throws.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classMessageTransport
-