47
47
import com .google .cloud .pubsublite .v1 .PartitionAssignmentServiceSettings ;
48
48
import com .google .cloud .pubsublite .v1 .SubscriberServiceClient ;
49
49
import com .google .cloud .pubsublite .v1 .SubscriberServiceSettings ;
50
+ import java .util .Optional ;
50
51
import org .apache .kafka .clients .consumer .Consumer ;
51
52
52
53
@ AutoValue
@@ -61,29 +62,55 @@ public abstract class ConsumerSettings {
61
62
// Optional parameters.
62
63
abstract boolean autocommit ();
63
64
65
+ abstract Optional <TopicPath > topicPathOverride ();
66
+
64
67
public static Builder newBuilder () {
65
68
return (new AutoValue_ConsumerSettings .Builder ()).setAutocommit (false );
66
69
}
67
70
68
71
@ AutoValue .Builder
69
72
public abstract static class Builder {
70
73
// Required parameters.
74
+ /**
75
+ * The subscription path to use. Only the topic corresponding to this subscription can be
76
+ * subscribed to.
77
+ */
71
78
public abstract Builder setSubscriptionPath (SubscriptionPath path );
72
79
80
+ /** The per-partition flow control settings. */
73
81
public abstract Builder setPerPartitionFlowControlSettings (FlowControlSettings settings );
74
82
75
83
// Optional parameters.
84
+ /** The autocommit mode. */
76
85
public abstract Builder setAutocommit (boolean autocommit );
77
86
87
+ /**
88
+ * An override for the TopicPath used by this consumer.
89
+ *
90
+ * <p>When this is set, the topic path of the subscription will not be fetched: instead, the
91
+ * topic used in methods will be compared with the provided TopicPath object.
92
+ *
93
+ * <p>This is useful if you do not have the pubsublite.subscriptions.get permission for the
94
+ * subscription.
95
+ */
96
+ public abstract Builder setTopicPathOverride (TopicPath topicPath );
97
+
78
98
public abstract ConsumerSettings build ();
79
99
}
80
100
81
101
public Consumer <byte [], byte []> instantiate () throws ApiException {
82
- CloudRegion region = subscriptionPath ().location ().extractRegion ();
83
- try (AdminClient adminClient =
84
- AdminClient .create (AdminClientSettings .newBuilder ().setRegion (region ).build ())) {
85
- Subscription subscription = adminClient .getSubscription (subscriptionPath ()).get ();
86
- TopicPath topic = TopicPath .parse (subscription .getTopic ());
102
+ try {
103
+ CloudRegion region = subscriptionPath ().location ().extractRegion ();
104
+ TopicPath topic ;
105
+ if (topicPathOverride ().isPresent ()) {
106
+ topic = topicPathOverride ().get ();
107
+ } else {
108
+ try (AdminClient adminClient =
109
+ AdminClient .create (AdminClientSettings .newBuilder ().setRegion (region ).build ())) {
110
+ Subscription subscription = adminClient .getSubscription (subscriptionPath ()).get ();
111
+ topic = TopicPath .parse (subscription .getTopic ());
112
+ }
113
+ }
87
114
AssignerFactory assignerFactory =
88
115
receiver -> {
89
116
try {
0 commit comments