@@ -85,17 +85,7 @@ To make authenticated requests to Google Cloud Pub/Sub, you must create a servic |
85 | 85 | credentials. You can then make API calls by calling methods on the Pub/Sub service object. The |
86 | 86 | simplest way to authenticate is to use |
87 | 87 | [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials). |
88 | | -These credentials are automatically inferred from your environment, so you only need the following |
89 | | -code to create your service object: |
90 | | - |
91 | | -```java |
92 | | -import com.google.cloud.pubsub.PubSub; |
93 | | -import com.google.cloud.pubsub.PubSubOptions; |
94 | | - |
95 | | -try(PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) { |
96 | | - // use pubsub here |
97 | | -} |
98 | | -``` |
| 88 | +These credentials are automatically inferred from your environment. |
99 | 89 |
|
100 | 90 | For other authentication options, see the |
101 | 91 | [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication) page. |
@@ -105,65 +95,108 @@ With Pub/Sub you can create topics. A topic is a named resource to which message |
105 | 95 | publishers. Add the following imports at the top of your file: |
106 | 96 |
|
107 | 97 | ```java |
108 | | -import com.google.cloud.pubsub.Topic; |
109 | | -import com.google.cloud.pubsub.TopicInfo; |
| 98 | +import com.google.cloud.pubsub.spi.v1.PublisherClient; |
| 99 | +import com.google.pubsub.v1.TopicName; |
110 | 100 | ``` |
111 | 101 | Then, to create the topic, use the following code: |
112 | 102 |
|
113 | 103 | ```java |
114 | | -Topic topic = pubsub.create(TopicInfo.of("test-topic")); |
| 104 | +TopicName topic = TopicName.create("test-project", "test-topic"); |
| 105 | +try (PublisherClient publisherClient = PublisherClient.create()) { |
| 106 | + publisherClient.createTopic(topic); |
| 107 | +} |
115 | 108 | ``` |
116 | 109 |
|
117 | 110 | #### Publishing messages |
118 | 111 | With Pub/Sub you can publish messages to a topic. Add the following import at the top of your file: |
119 | 112 |
|
120 | 113 | ```java |
121 | | -import com.google.cloud.pubsub.Message; |
| 114 | +import com.google.api.gax.core.RpcFuture; |
| 115 | +import com.google.cloud.pubsub.spi.v1.Publisher; |
| 116 | +import com.google.protobuf.ByteString; |
| 117 | +import com.google.pubsub.v1.PubsubMessage; |
122 | 118 | ``` |
123 | 119 | Then, to publish messages asynchronously, use the following code: |
124 | 120 |
|
125 | 121 | ```java |
126 | | -Message message1 = Message.of("First message"); |
127 | | -Message message2 = Message.of("Second message"); |
128 | | -topic.publishAsync(message1, message2); |
| 122 | +Publisher publisher = null; |
| 123 | +try { |
| 124 | + publisher = Publisher.newBuilder(topic).build(); |
| 125 | + ByteString data = ByteString.copyFromUtf8("my-message"); |
| 126 | + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); |
| 127 | + RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage); |
| 128 | +} finally { |
| 129 | + if (publisher != null) { |
| 130 | + publisher.shutdown(); |
| 131 | + } |
| 132 | +} |
129 | 133 | ``` |
130 | 134 |
|
131 | 135 | #### Creating a subscription |
132 | 136 | With Pub/Sub you can create subscriptions. A subscription represents the stream of messages from a |
133 | 137 | single, specific topic. Add the following imports at the top of your file: |
134 | 138 |
|
135 | 139 | ```java |
136 | | -import com.google.cloud.pubsub.Subscription; |
137 | | -import com.google.cloud.pubsub.SubscriptionInfo; |
| 140 | +import com.google.cloud.pubsub.spi.v1.SubscriberClient; |
| 141 | +import com.google.pubsub.v1.PushConfig; |
| 142 | +import com.google.pubsub.v1.SubscriptionName; |
| 143 | +import com.google.pubsub.v1.TopicName; |
138 | 144 | ``` |
139 | 145 | Then, to create the subscription, use the following code: |
140 | 146 |
|
141 | 147 | ```java |
142 | | -Subscription subscription = |
143 | | - pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription")); |
| 148 | +TopicName topic = TopicName.create("test-project", "test-topic"); |
| 149 | +SubscriptionName subscription = SubscriptionName.create("test-project", "test-subscription"); |
| 150 | + |
| 151 | +try (SubscriberClient subscriberClient = SubscriberClient.create()) { |
| 152 | + subscriberClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0); |
| 153 | +} |
144 | 154 | ``` |
145 | 155 |
|
146 | 156 | #### Pulling messages |
147 | 157 | With Pub/Sub you can pull messages from a subscription. Add the following imports at the top of your |
148 | 158 | file: |
149 | 159 |
|
150 | 160 | ```java |
151 | | -import com.google.cloud.pubsub.Message; |
152 | | -import com.google.cloud.pubsub.PubSub.MessageConsumer; |
153 | | -import com.google.cloud.pubsub.PubSub.MessageProcessor; |
| 161 | +import com.google.cloud.pubsub.spi.v1.AckReply; |
| 162 | +import com.google.cloud.pubsub.spi.v1.AckReplyConsumer; |
| 163 | +import com.google.cloud.pubsub.spi.v1.MessageReceiver; |
| 164 | +import com.google.cloud.pubsub.spi.v1.Subscriber; |
| 165 | +import com.google.common.util.concurrent.MoreExecutors; |
| 166 | +import com.google.pubsub.v1.PubsubMessage; |
| 167 | +import com.google.pubsub.v1.SubscriptionName; |
| 168 | +import com.google.pubsub.v1.TopicName; |
154 | 169 | ``` |
155 | 170 | Then, to pull messages asynchronously, use the following code: |
156 | 171 |
|
157 | 172 | ```java |
158 | | -MessageProcessor callback = new MessageProcessor() { |
159 | | - @Override |
160 | | - public void process(Message message) throws Exception { |
161 | | - System.out.printf("Received message \"%s\"%n", message.getPayloadAsString()); |
| 173 | +MessageReceiver receiver = |
| 174 | + new MessageReceiver() { |
| 175 | + @Override |
| 176 | + public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { |
| 177 | + System.out.println("got message: " + message.getData().toStringUtf8()); |
| 178 | + consumer.accept(AckReply.ACK, null); |
| 179 | + } |
| 180 | + }; |
| 181 | +Subscriber subscriber = null; |
| 182 | +try { |
| 183 | + subscriber = Subscriber.newBuilder(subscription, receiver).build(); |
| 184 | + subscriber.addListener( |
| 185 | + new Subscriber.SubscriberListener() { |
| 186 | + @Override |
| 187 | + public void failed(Subscriber.State from, Throwable failure) { |
| 188 | + // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down. |
| 189 | + System.err.println(failure); |
| 190 | + } |
| 191 | + }, |
| 192 | + MoreExecutors.directExecutor()); |
| 193 | + subscriber.startAsync().awaitRunning(); |
| 194 | + // Pull messages for 60 seconds. |
| 195 | + Thread.sleep(60000); |
| 196 | +} finally { |
| 197 | + if (subscriber != null) { |
| 198 | + subscriber.stopAsync(); |
162 | 199 | } |
163 | | -}; |
164 | | -// Create a message consumer and pull messages (for 60 seconds) |
165 | | -try (MessageConsumer consumer = subscription.pullAsync(callback)) { |
166 | | - Thread.sleep(60_000); |
167 | 200 | } |
168 | 201 | ``` |
169 | 202 | #### Complete source code |
|