@@ -18,6 +18,14 @@ If you are using Maven, add this to your pom.xml file
1818 <artifactId >google-cloud-pubsublite</artifactId >
1919 <version >0.1.3</version >
2020</dependency >
21+
22+ <!-- A logging dependency used by the underlying library -->
23+ <dependency >
24+ <groupId >com.google.flogger</groupId >
25+ <artifactId >flogger-system-backend</artifactId >
26+ <version >0.5.1</version >
27+ <scope >runtime</scope >
28+ </dependency >
2129```
2230If you are using Gradle, add this to your dependencies
2331``` Groovy
@@ -85,6 +93,9 @@ publishers. Add the following imports at the top of your file:
8593
8694``` java
8795import com.google.cloud.pubsublite.* ;
96+ import com.google.cloud.pubsublite.proto.Topic ;
97+ import com.google.cloud.pubsublite.proto.Topic.* ;
98+ import com.google.protobuf.util.Durations ;
8899```
89100Then, to create the topic, use the following code:
90101
@@ -93,28 +104,28 @@ CloudRegion region = CloudRegion.of("us-central1");
93104TopicPath topicPath =
94105 TopicPaths . newBuilder()
95106 .setZone(CloudZone . of(region, ' b' ))
96- .setProjectNumber(ProjectNumber . of(123 ))
107+ .setProjectNumber(ProjectNumber . of(123L )) // Your project number.
97108 .setTopicName(TopicName . of(" my-new-topic" ))
98109 .build();
99-
110+
100111Topic topic =
101112 Topic . newBuilder()
102113 .setPartitionConfig(
103114 PartitionConfig . newBuilder()
104115 .setScale(1 ) // Set publishing throughput to 1*4 MiB per sec. This must be 1-4.
105- .setCount(PARTITIONS ))
116+ .setCount(2 )) // The number of partitions.
106117 .setRetentionConfig(
107118 RetentionConfig . newBuilder()
108119 .setPeriod(Durations . fromDays(7 ))
109120 .setPerPartitionBytes(100000000000L )) // 100 GiB. This must be 30 GiB-10 TiB.
110121 .setName(topicPath. value())
111122 .build();
112123
113- try ( AdminClient client =
114- AdminClientBuilder . builder()
115- .setRegion(region)
116- .build()) {
117- client . createTopic(topic) . get( );
124+ AdminClientSettings settings =
125+ AdminClientSettings . newBuilder() . setRegion(region) . build();
126+ try ( AdminClient client = AdminClient . create(settings)) {
127+ Topic response = client . createTopic(topic) . get();
128+ System . out . println(response . getAllFields() );
118129}
119130```
120131
@@ -123,8 +134,12 @@ try (AdminClient client =
123134With Pub/Sub Lite, you can publish messages to a topic. Add the following import at the top of your file:
124135
125136``` java
137+ import com.google.api.core.* ;
126138import com.google.cloud.pubsublite.* ;
127139import com.google.cloud.pubsublite.cloudpubsub.* ;
140+ import com.google.protobuf.ByteString ;
141+ import com.google.pubsub.v1.PubsubMessage ;
142+ import java.util.* ;
128143```
129144Then, to publish messages asynchronously, use the following code:
130145
@@ -139,12 +154,12 @@ public class PublisherExample {
139154 // Load the topic name from a commandline flag.
140155 private static final String TOPIC_NAME = " my-new-topic" ;
141156
142- public static List<ApiFuture<String > > runPublisher (PublisherInterface publisher ) throws Exception {
157+ public static List<ApiFuture<String > > runPublisher (Publisher publisher ) throws Exception {
143158 List<ApiFuture<String > > futures = new ArrayList<> ();
144159 for (int i = 0 ; i < MESSAGE_COUNT ; i++ ) {
145160 String message = " message-" + i;
146161
147- // convert message to bytes
162+ // Convert the message to a byte string.
148163 ByteString data = ByteString . copyFromUtf8(message);
149164 PubsubMessage pubsubMessage = PubsubMessage . newBuilder(). setData(data). build();
150165
@@ -157,25 +172,27 @@ public class PublisherExample {
157172
158173 // Publish messages to a topic.
159174 public static void main (String [] args ) throws Exception {
160- PublisherApiService publisherService =
161- PublisherBuilder . newBuilder()
175+ PublisherSettings settings =
176+ PublisherSettings . newBuilder()
162177 .setTopicPath(
163178 TopicPaths . newBuilder()
164179 .setProjectNumber(ProjectNumber . of(PROJECT_NUMBER ))
165180 .setZone(CloudZone . parse(ZONE ))
166181 .setTopicName(TopicName . of(TOPIC_NAME ))
167182 .build())
168183 .build();
169- publisherService. startAsync(). awaitRunning();
170- List<ApiFuture<String > > futureAckIds = runPublisher(publisherService);
171- publisherService. stopAsync(). awaitTerminated();
184+ Publisher publisher = Publisher . create(settings);
185+ publisher. startAsync(). awaitRunning();
186+ List<ApiFuture<String > > futureAckIds = runPublisher(publisher);
187+ publisher. stopAsync(). awaitTerminated();
188+
172189 List<String > ackIds = ApiFutures . allAsList(futureAckIds). get();
173190 ArrayList<PublishMetadata > metadata = new ArrayList<> ();
174191 for (String id : ackIds) {
175192 metadata. add(PublishMetadata . decode(id));
176193 }
177194 for (PublishMetadata one : metadata) {
178- System . out. println(one. offset() );
195+ System . out. println(one);
179196 }
180197 }
181198}
@@ -188,15 +205,17 @@ single, specific topic. Add the following imports at the top of your file:
188205
189206``` java
190207import com.google.cloud.pubsublite.* ;
208+ import com.google.cloud.pubsublite.proto.Subscription ;
209+ import com.google.cloud.pubsublite.proto.Subscription.* ;
210+ import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig.* ;
191211```
192212Then, to create the subscription, use the following code:
193213
194214``` java
195- // CloudZone must be equivalent to the topic.
196- CloudRegion cloudRegion = CloudRegion . of(" us-central1" );
197- CloudZone zone = CloudZone . of(cloudRegion, ' b' );
198- // ProjectNumber must the equivalent to the topic.
199- ProjectNumber projectNum = ProjectNumber . of(123 );
215+ // CloudZone must be the zone of the topic.
216+ CloudRegion region = CloudRegion . of(" us-central1" );
217+ CloudZone zone = CloudZone . of(region, ' b' );
218+ ProjectNumber projectNum = ProjectNumber . of(123L );
200219TopicName topicName = TopicName . of(" my-new-topic" );
201220SubscriptionName subscriptionName = SubscriptionName . of(" my-new-sub" );
202221
@@ -223,11 +242,11 @@ Subscription.newBuilder()
223242 .setTopic(topicPath. value())
224243 .build();
225244
226- try ( AdminClient client =
227- AdminClientBuilder . builder()
228- .setRegion(region)
229- .build()) {
230- client . createSubscription(subscription) . get( );
245+ AdminClientSettings settings =
246+ AdminClientSettings . newBuilder() . setRegion(region) . build();
247+ try ( AdminClient client = AdminClient . create(settings)) {
248+ Subscription response = client . createSubscription(subscription) . get();
249+ System . out . println(response . getAllFields() );
231250}
232251```
233252
@@ -237,29 +256,37 @@ With Pub/Sub Lite you can receive messages from a subscription. Add the
237256following imports at the top of your file:
238257
239258``` java
259+ import com.google.cloud.pubsub.v1.AckReplyConsumer ;
260+ import com.google.cloud.pubsub.v1.MessageReceiver ;
240261import com.google.cloud.pubsublite.* ;
241262import com.google.cloud.pubsublite.cloudpubsub.* ;
242- import com.google.pubsub.v1.MessageReceiver ;
243- import com.google.pubsub.v1.SubscriberInterface ;
263+ import com.google.common.util.concurrent.MoreExecutors ;
264+ import com.google.pubsub.v1.PubsubMessage ;
265+ import java.util.* ;
244266```
245267Then, to pull messages asynchronously, use the following code:
246268
247269``` java
270+ CloudZone zone = CloudZone . parse(" us-central1-b" );
271+ ProjectNumber projectNum = ProjectNumber . of(123L );
272+ SubscriptionName subscriptionName = SubscriptionName . of(" my-new-sub" );
273+
248274SubscriptionPath subscriptionPath =
249- SubscriptionPaths . newBuilder()
250- .setZone(zone)
251- .setProjectNumber(projectNum)
252- .setSubscriptionName(subscriptionName)
253- .build();
254- // Connect to partition 0.
255- Partition PARTITION = Partition . of(0 );
275+ SubscriptionPaths . newBuilder()
276+ .setZone(zone)
277+ .setProjectNumber(projectNum)
278+ .setSubscriptionName(subscriptionName)
279+ .build();
256280
257281FlowControlSettings flowControlSettings =
258282 FlowControlSettings . builder()
259- .setBytesOutstanding(10_000_000 ) // 10 MB per partition.
260- .setMessagesOutstanding(Long . MAX_VALUE )
261- .build();
283+ .setBytesOutstanding(10 * 1024 * 1024L ) // 10 MiB per partition.
284+ .setMessagesOutstanding(Long . MAX_VALUE )
285+ .build();
262286
287+ // Connect to partitions 0, 1. Note that we configured the topic with 2
288+ // partitions.
289+ List<Partition > partitions = Arrays . asList(Partition . of(0 ), Partition . of(1 ));
263290
264291MessageReceiver receiver =
265292 new MessageReceiver () {
@@ -276,19 +303,20 @@ MessageReceiver receiver =
276303 }
277304 };
278305
279- SubscriberInterface subscriber = null ;
306+ Subscriber subscriber = null ;
280307try {
281- subscriber = Subscriber . create(SubscriberSetttings . newBuilder()
308+ subscriber = Subscriber . create(SubscriberSettings . newBuilder()
282309 .setSubscriptionPath(subscriptionPath)
283- .setFlowControlSettings (flowControlSettings)
310+ .setPerPartitionFlowControlSettings (flowControlSettings)
284311 .setReceiver(receiver)
285- .setPartitions(List . of( Partition . of( 0 )) )
312+ .setPartitions(partitions )
286313 .build());
287314 subscriber. addListener(
288315 new Subscriber .Listener () {
289316 @Override
290317 public void failed (Subscriber .State from , Throwable failure ) {
291- // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
318+ // Handle failure. This is called when the Subscriber encountered a
319+ // fatal error and is shutting down.
292320 System . err. println(failure);
293321 }
294322 },
@@ -302,6 +330,10 @@ try {
302330}
303331```
304332
333+ ## Samples
334+
335+ More sample code can be found in the [ samples] ( samples ) folder.
336+
305337## Troubleshooting
306338
307339To get help, follow the instructions in the [ shared Troubleshooting document] [ troubleshooting ] .
0 commit comments