You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

370 lines
9.1 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub_test
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "cloud.google.com/go/pubsub"
  20. "google.golang.org/api/iterator"
  21. )
  22. func ExampleNewClient() {
  23. ctx := context.Background()
  24. _, err := pubsub.NewClient(ctx, "project-id")
  25. if err != nil {
  26. // TODO: Handle error.
  27. }
  28. // See the other examples to learn how to use the Client.
  29. }
  30. func ExampleClient_CreateTopic() {
  31. ctx := context.Background()
  32. client, err := pubsub.NewClient(ctx, "project-id")
  33. if err != nil {
  34. // TODO: Handle error.
  35. }
  36. // Create a new topic with the given name.
  37. topic, err := client.CreateTopic(ctx, "topicName")
  38. if err != nil {
  39. // TODO: Handle error.
  40. }
  41. _ = topic // TODO: use the topic.
  42. }
  43. // Use TopicInProject to refer to a topic that is not in the client's project, such
  44. // as a public topic.
  45. func ExampleClient_TopicInProject() {
  46. ctx := context.Background()
  47. client, err := pubsub.NewClient(ctx, "project-id")
  48. if err != nil {
  49. // TODO: Handle error.
  50. }
  51. topic := client.TopicInProject("topicName", "another-project-id")
  52. _ = topic // TODO: use the topic.
  53. }
  54. func ExampleClient_CreateSubscription() {
  55. ctx := context.Background()
  56. client, err := pubsub.NewClient(ctx, "project-id")
  57. if err != nil {
  58. // TODO: Handle error.
  59. }
  60. // Create a new topic with the given name.
  61. topic, err := client.CreateTopic(ctx, "topicName")
  62. if err != nil {
  63. // TODO: Handle error.
  64. }
  65. // Create a new subscription to the previously created topic
  66. // with the given name.
  67. sub, err := client.CreateSubscription(ctx, "subName", pubsub.SubscriptionConfig{
  68. Topic: topic,
  69. AckDeadline: 10 * time.Second,
  70. })
  71. if err != nil {
  72. // TODO: Handle error.
  73. }
  74. _ = sub // TODO: use the subscription.
  75. }
  76. func ExampleTopic_Delete() {
  77. ctx := context.Background()
  78. client, err := pubsub.NewClient(ctx, "project-id")
  79. if err != nil {
  80. // TODO: Handle error.
  81. }
  82. topic := client.Topic("topicName")
  83. if err := topic.Delete(ctx); err != nil {
  84. // TODO: Handle error.
  85. }
  86. }
  87. func ExampleTopic_Exists() {
  88. ctx := context.Background()
  89. client, err := pubsub.NewClient(ctx, "project-id")
  90. if err != nil {
  91. // TODO: Handle error.
  92. }
  93. topic := client.Topic("topicName")
  94. ok, err := topic.Exists(ctx)
  95. if err != nil {
  96. // TODO: Handle error.
  97. }
  98. if !ok {
  99. // Topic doesn't exist.
  100. }
  101. }
  102. func ExampleTopic_Publish() {
  103. ctx := context.Background()
  104. client, err := pubsub.NewClient(ctx, "project-id")
  105. if err != nil {
  106. // TODO: Handle error.
  107. }
  108. topic := client.Topic("topicName")
  109. defer topic.Stop()
  110. var results []*pubsub.PublishResult
  111. r := topic.Publish(ctx, &pubsub.Message{
  112. Data: []byte("hello world"),
  113. })
  114. results = append(results, r)
  115. // Do other work ...
  116. for _, r := range results {
  117. id, err := r.Get(ctx)
  118. if err != nil {
  119. // TODO: Handle error.
  120. }
  121. fmt.Printf("Published a message with a message ID: %s\n", id)
  122. }
  123. }
  124. func ExampleTopic_Subscriptions() {
  125. ctx := context.Background()
  126. client, err := pubsub.NewClient(ctx, "project-id")
  127. if err != nil {
  128. // TODO: Handle error.
  129. }
  130. topic := client.Topic("topic-name")
  131. // List all subscriptions of the topic (maybe of multiple projects).
  132. for subs := topic.Subscriptions(ctx); ; {
  133. sub, err := subs.Next()
  134. if err == iterator.Done {
  135. break
  136. }
  137. if err != nil {
  138. // TODO: Handle error.
  139. }
  140. _ = sub // TODO: use the subscription.
  141. }
  142. }
  143. func ExampleSubscription_Delete() {
  144. ctx := context.Background()
  145. client, err := pubsub.NewClient(ctx, "project-id")
  146. if err != nil {
  147. // TODO: Handle error.
  148. }
  149. sub := client.Subscription("subName")
  150. if err := sub.Delete(ctx); err != nil {
  151. // TODO: Handle error.
  152. }
  153. }
  154. func ExampleSubscription_Exists() {
  155. ctx := context.Background()
  156. client, err := pubsub.NewClient(ctx, "project-id")
  157. if err != nil {
  158. // TODO: Handle error.
  159. }
  160. sub := client.Subscription("subName")
  161. ok, err := sub.Exists(ctx)
  162. if err != nil {
  163. // TODO: Handle error.
  164. }
  165. if !ok {
  166. // Subscription doesn't exist.
  167. }
  168. }
  169. func ExampleSubscription_Config() {
  170. ctx := context.Background()
  171. client, err := pubsub.NewClient(ctx, "project-id")
  172. if err != nil {
  173. // TODO: Handle error.
  174. }
  175. sub := client.Subscription("subName")
  176. config, err := sub.Config(ctx)
  177. if err != nil {
  178. // TODO: Handle error.
  179. }
  180. fmt.Println(config)
  181. }
  182. func ExampleSubscription_Receive() {
  183. ctx := context.Background()
  184. client, err := pubsub.NewClient(ctx, "project-id")
  185. if err != nil {
  186. // TODO: Handle error.
  187. }
  188. sub := client.Subscription("subName")
  189. err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
  190. // TODO: Handle message.
  191. // NOTE: May be called concurrently; synchronize access to shared memory.
  192. m.Ack()
  193. })
  194. if err != context.Canceled {
  195. // TODO: Handle error.
  196. }
  197. }
  198. // This example shows how to configure keepalive so that unacknoweldged messages
  199. // expire quickly, allowing other subscribers to take them.
  200. func ExampleSubscription_Receive_maxExtension() {
  201. ctx := context.Background()
  202. client, err := pubsub.NewClient(ctx, "project-id")
  203. if err != nil {
  204. // TODO: Handle error.
  205. }
  206. sub := client.Subscription("subName")
  207. // This program is expected to process and acknowledge messages in 30 seconds. If
  208. // not, the Pub/Sub API will assume the message is not acknowledged.
  209. sub.ReceiveSettings.MaxExtension = 30 * time.Second
  210. err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
  211. // TODO: Handle message.
  212. m.Ack()
  213. })
  214. if err != context.Canceled {
  215. // TODO: Handle error.
  216. }
  217. }
  218. // This example shows how to throttle Subscription.Receive, which aims for high
  219. // throughput by default. By limiting the number of messages and/or bytes being
  220. // processed at once, you can bound your program's resource consumption.
  221. func ExampleSubscription_Receive_maxOutstanding() {
  222. ctx := context.Background()
  223. client, err := pubsub.NewClient(ctx, "project-id")
  224. if err != nil {
  225. // TODO: Handle error.
  226. }
  227. sub := client.Subscription("subName")
  228. sub.ReceiveSettings.MaxOutstandingMessages = 5
  229. sub.ReceiveSettings.MaxOutstandingBytes = 10e6
  230. err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
  231. // TODO: Handle message.
  232. m.Ack()
  233. })
  234. if err != context.Canceled {
  235. // TODO: Handle error.
  236. }
  237. }
  238. func ExampleSubscription_Update() {
  239. ctx := context.Background()
  240. client, err := pubsub.NewClient(ctx, "project-id")
  241. if err != nil {
  242. // TODO: Handle error.
  243. }
  244. sub := client.Subscription("subName")
  245. subConfig, err := sub.Update(ctx, pubsub.SubscriptionConfigToUpdate{
  246. PushConfig: &pubsub.PushConfig{Endpoint: "https://example.com/push"},
  247. })
  248. if err != nil {
  249. // TODO: Handle error.
  250. }
  251. _ = subConfig // TODO: Use SubscriptionConfig.
  252. }
  253. func ExampleSubscription_CreateSnapshot() {
  254. ctx := context.Background()
  255. client, err := pubsub.NewClient(ctx, "project-id")
  256. if err != nil {
  257. // TODO: Handle error.
  258. }
  259. sub := client.Subscription("subName")
  260. snapConfig, err := sub.CreateSnapshot(ctx, "snapshotName")
  261. if err != nil {
  262. // TODO: Handle error.
  263. }
  264. _ = snapConfig // TODO: Use SnapshotConfig.
  265. }
  266. func ExampleSubscription_SeekToSnapshot() {
  267. ctx := context.Background()
  268. client, err := pubsub.NewClient(ctx, "project-id")
  269. if err != nil {
  270. // TODO: Handle error.
  271. }
  272. sub := client.Subscription("subName")
  273. snap := client.Snapshot("snapshotName")
  274. if err := sub.SeekToSnapshot(ctx, snap); err != nil {
  275. // TODO: Handle error.
  276. }
  277. }
  278. func ExampleSubscription_SeekToTime() {
  279. ctx := context.Background()
  280. client, err := pubsub.NewClient(ctx, "project-id")
  281. if err != nil {
  282. // TODO: Handle error.
  283. }
  284. sub := client.Subscription("subName")
  285. if err := sub.SeekToTime(ctx, time.Now().Add(-time.Hour)); err != nil {
  286. // TODO: Handle error.
  287. }
  288. }
  289. func ExampleSnapshot_Delete() {
  290. ctx := context.Background()
  291. client, err := pubsub.NewClient(ctx, "project-id")
  292. if err != nil {
  293. // TODO: Handle error.
  294. }
  295. snap := client.Snapshot("snapshotName")
  296. if err := snap.Delete(ctx); err != nil {
  297. // TODO: Handle error.
  298. }
  299. }
  300. func ExampleClient_Snapshots() {
  301. ctx := context.Background()
  302. client, err := pubsub.NewClient(ctx, "project-id")
  303. if err != nil {
  304. // TODO: Handle error.
  305. }
  306. // List all snapshots for the project.
  307. iter := client.Snapshots(ctx)
  308. _ = iter // TODO: iterate using Next.
  309. }
  310. func ExampleSnapshotConfigIterator_Next() {
  311. ctx := context.Background()
  312. client, err := pubsub.NewClient(ctx, "project-id")
  313. if err != nil {
  314. // TODO: Handle error.
  315. }
  316. // List all snapshots for the project.
  317. iter := client.Snapshots(ctx)
  318. for {
  319. snapConfig, err := iter.Next()
  320. if err == iterator.Done {
  321. break
  322. }
  323. if err != nil {
  324. // TODO: Handle error.
  325. }
  326. _ = snapConfig // TODO: use the SnapshotConfig.
  327. }
  328. }
  329. // TODO(jba): write an example for PublishResult.Ready
  330. // TODO(jba): write an example for Subscription.IAM
  331. // TODO(jba): write an example for Topic.IAM
  332. // TODO(jba): write an example for Topic.Stop