18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
20
21
+ import com .google .cloud .pubsub .spi .v1 .PublisherApi ;
22
+ import com .google .cloud .pubsub .spi .v1 .SubscriberApi ;
21
23
import com .google .common .base .MoreObjects ;
22
24
23
25
import java .io .Serializable ;
24
26
import java .util .Objects ;
27
+ import java .util .concurrent .TimeUnit ;
25
28
26
29
/**
27
- * Pub/Sub subscription information.
30
+ * A Google Cloud Pub/Sub subscription. A subscription represents the stream of messages from a
31
+ * single, specific topic, to be delivered to the subscribing application. Pub/Sub subscriptions
32
+ * support both push and pull message delivery.
33
+ *
34
+ * <p>In a push subscription, the Pub/Sub server sends a request to the subscriber application, at a
35
+ * preconfigured endpoint (see {@link PushConfig}). The subscriber's HTTP response serves as an
36
+ * implicit acknowledgement: a success response indicates that the message has been succesfully
37
+ * processed and the Pub/Sub system can delete it from the subscription; a non-success response
38
+ * indicates that the Pub/Sub server should resend it (implicit "nack").
39
+ *
40
+ * <p>In a pull subscription, the subscribing application must explicitly pull messages using one of
41
+ * {@link PubSub#pull(String, PubSub.PullOption...)},
42
+ * {@link PubSub#pullAsync(String, PubSub.MessageProcessor)} or
43
+ * {@link PubSub#pullAsync(String, PubSub.PullOption...)}. The subscribing application must then
44
+ * explicitly acknowledge the messages using one of {@link PubSub#ack(String, Iterable)},
45
+ * {@link PubSub#ack(String, String, String...)}, {@link PubSub#ackAsync(String, Iterable)} or
46
+ * {@link PubSub#ackAsync(String, String, String...)}.
47
+ *
48
+ * @see <a href="https://cloud.google.com/pubsub/overview#data_model">Pub/Sub Data Model</a>
49
+ * @see <a href="https://cloud.google.com/pubsub/subscriber">Subscriber Guide</a>
28
50
*/
29
51
public class SubscriptionInfo implements Serializable {
30
52
@@ -35,20 +57,47 @@ public class SubscriptionInfo implements Serializable {
35
57
private final PushConfig pushConfig ;
36
58
private final int ackDeadlineSeconds ;
37
59
38
-
39
60
/**
40
- * Builder for Subscription .
61
+ * Builder for {@code SubscriptionInfo} objects .
41
62
*/
42
63
public abstract static class Builder {
43
64
65
+ /**
66
+ * Sets the name of the subscription. The name must start with a letter, and contain only
67
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
68
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
69
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
70
+ * string {@code goog}.
71
+ */
44
72
public abstract Builder name (String name );
45
73
74
+ /**
75
+ * Sets the name of the topic the subscription refers to.
76
+ */
46
77
public abstract Builder topic (String name );
47
78
79
+ /**
80
+ * Sets the push configuration for the subscription. If set, the subscription will be in
81
+ * push mode and the {@code pushConfig} parameter provides the push endpoint. If not set, the
82
+ * subscription will be in pull mode.
83
+ */
48
84
public abstract Builder pushConfig (PushConfig pushConfig );
49
85
86
+ /**
87
+ * Sets the maximum time after a subscriber receives a message before the subscriber should
88
+ * acknowledge the message. After message delivery but before the ack deadline expires and
89
+ * before the message is acknowledged, it is an outstanding message and will not be delivered
90
+ * again during that time (on a best-effort basis). For pull subscriptions, this value is used
91
+ * as the initial value for the ack deadline. To override the ack deadline value for a given
92
+ * message, use {@link PubSub#modifyAckDeadline(String, int, TimeUnit, Iterable)}. For push
93
+ * delivery, this value is used to set the request timeout for the call to the push endpoint. If
94
+ * not specified, the default value of 10 seconds is used.
95
+ */
50
96
public abstract Builder ackDeadLineSeconds (int ackDeadLineSeconds );
51
97
98
+ /**
99
+ * Creates a subscription object.
100
+ */
52
101
public abstract SubscriptionInfo build ();
53
102
}
54
103
@@ -108,31 +157,60 @@ public SubscriptionInfo build() {
108
157
ackDeadlineSeconds = builder .ackDeadlineSeconds ;
109
158
}
110
159
160
+ /**
161
+ * Returns the name of the topic this subscription refers to.
162
+ */
111
163
public String topic () {
112
164
return topic ;
113
165
}
114
166
167
+ /**
168
+ * Sets the name of the subscription. The name must start with a letter, and contain only
169
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
170
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
171
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
172
+ * string {@code goog}.
173
+ */
115
174
public String name () {
116
175
return name ;
117
176
}
118
177
178
+ /**
179
+ * Returns the push configuration for the subscription. If set, the subscription is in push mode
180
+ * and the returned value defines the push endpoint. If {@code null}, the subscription is in pull
181
+ * mode.
182
+ */
119
183
public PushConfig pushConfig () {
120
184
return pushConfig ;
121
185
}
122
186
187
+ /**
188
+ * Returns the maximum time after a subscriber receives a message before the subscriber should
189
+ * acknowledge the message. After message delivery but before the ack deadline expires and
190
+ * before the message is acknowledged, it is an outstanding message and will not be delivered
191
+ * again during that time (on a best-effort basis). For pull subscriptions, this value is used
192
+ * as the initial value for the ack deadline. To override the ack deadline value for a given
193
+ * message, use {@link PubSub#modifyAckDeadline(String, int, TimeUnit, Iterable)}. For push
194
+ * delivery, this value is used to set the request timeout for the call to the push endpoint. If
195
+ * not specified, the default value of 10 seconds is used.
196
+ */
123
197
public long ackDeadlineSeconds () {
124
198
return ackDeadlineSeconds ;
125
199
}
126
200
127
201
@ Override
128
- public boolean equals (Object o ) {
129
- if (this == o ) {
202
+ public boolean equals (Object obj ) {
203
+ if (this == obj ) {
130
204
return true ;
131
205
}
132
- if (o == null || getClass () != o . getClass ()) {
206
+ if (obj == null || ! obj . getClass (). equals ( this . getClass () )) {
133
207
return false ;
134
208
}
135
- return Objects .equals (toPb (), ((SubscriptionInfo ) o ).toPb ());
209
+ SubscriptionInfo other = (SubscriptionInfo ) obj ;
210
+ return Objects .equals (topic , other .topic )
211
+ && Objects .equals (name , other .name )
212
+ && Objects .equals (pushConfig , other .pushConfig )
213
+ && ackDeadlineSeconds == other .ackDeadlineSeconds ;
136
214
}
137
215
138
216
@ Override
@@ -150,11 +228,11 @@ public String toString() {
150
228
.toString ();
151
229
}
152
230
153
- com .google .pubsub .v1 .Subscription toPb () {
231
+ com .google .pubsub .v1 .Subscription toPb (String projectId ) {
154
232
com .google .pubsub .v1 .Subscription .Builder builder =
155
233
com .google .pubsub .v1 .Subscription .newBuilder ();
156
- builder .setTopic (topic );
157
- builder .setName (name );
234
+ builder .setTopic (PublisherApi . formatTopicName ( projectId , topic ) );
235
+ builder .setName (SubscriberApi . formatSubscriptionName ( projectId , name ) );
158
236
builder .setAckDeadlineSeconds (ackDeadlineSeconds );
159
237
if (pushConfig != null ) {
160
238
builder .setPushConfig (pushConfig .toPb ());
@@ -163,26 +241,67 @@ com.google.pubsub.v1.Subscription toPb() {
163
241
}
164
242
165
243
static SubscriptionInfo fromPb (com .google .pubsub .v1 .Subscription subscription ) {
166
- Builder builder = builder (subscription .getTopic (), subscription .getName ());
244
+ Builder builder = builder (PublisherApi .parseTopicFromTopicName (subscription .getTopic ()),
245
+ SubscriberApi .parseSubscriptionFromSubscriptionName (subscription .getName ()));
167
246
builder .ackDeadLineSeconds (subscription .getAckDeadlineSeconds ());
168
- if (subscription .hasPushConfig ()) {
247
+ // A subscription with an "empty" push config is a pull subscription
248
+ if (subscription .hasPushConfig ()
249
+ && !subscription .getPushConfig ().getPushEndpoint ().equals ("" )) {
169
250
builder .pushConfig (PushConfig .fromPb (subscription .getPushConfig ()));
170
251
}
171
252
return builder .build ();
172
253
}
173
254
255
+ /**
256
+ * Returns a builder for the subscription object.
257
+ */
174
258
public Builder toBuilder () {
175
259
return new BuilderImpl (this );
176
260
}
177
261
262
+ /**
263
+ * Creates a pull {@code SubscriptionInfo} object given the name of the topic and the name of the
264
+ * subscription.
265
+ *
266
+ * @param topic the name of the topic the subscription refers to
267
+ * @param name the name of the subscription. The name must start with a letter, and contain only
268
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
269
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
270
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
271
+ * string {@code goog}
272
+ */
178
273
public static SubscriptionInfo of (String topic , String name ) {
179
274
return builder (topic , name ).build ();
180
275
}
181
276
277
+ /**
278
+ * Creates a push {@code SubscriptionInfo} object given the name of the topic, the name of the
279
+ * subscription and the push endpoint.
280
+ *
281
+ * @param topic the name of the topic the subscription refers to
282
+ * @param name the name of the subscription. The name must start with a letter, and contain only
283
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
284
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
285
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
286
+ * string {@code goog}
287
+ * @param endpoint a URL locating the endpoint to which messages should be pushed. For example,
288
+ * an endpoint might use {@code https://example.com/push}.
289
+ */
182
290
public static SubscriptionInfo of (String topic , String name , String endpoint ) {
183
291
return builder (topic , name ).pushConfig (PushConfig .of (endpoint )).build ();
184
292
}
185
293
294
+ /**
295
+ * Creates a builder for {@code SubscriptionInfo} objects given the name of the topic and the name
296
+ * of the subscription.
297
+ *
298
+ * @param topic the name of the topic the subscription refers to
299
+ * @param name the name of the subscription. The name must start with a letter, and contain only
300
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
301
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
302
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
303
+ * string {@code goog}
304
+ */
186
305
public static Builder builder (String topic , String name ) {
187
306
return new BuilderImpl (topic , name );
188
307
}
0 commit comments