v1 Subscribe with concurrency control (DEPRECATED)
Stay organized with collections
Save and categorize content based on your preferences.
(DEPRECATED) Subscribe with concurrency control
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# v1 Subscribe with concurrency control (DEPRECATED)\n\n(DEPRECATED) Subscribe with concurrency control\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Pub/Sub quickstart using\nclient libraries](/pubsub/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Pub/Sub Go API\nreference documentation](https://godoc.org/cloud.google.com/go/pubsub).\n\n\nTo authenticate to Pub/Sub, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"sync/atomic\"\n \t\"time\"\n\n \t\"cloud.google.com/go/pubsub\"\n )\n\n func pullMsgsConcurrencyControl(w io.Writer, projectID, subID string) error {\n \t// projectID := \"my-project-id\"\n \t// subID := \"my-sub\"\n \tctx := context.Background()\n \tclient, err := pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_Client_NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"pubsub.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tsub := client.Subscription(subID)\n \t// Must set ReceiveSettings.Synchronous to false (or leave as default) to enable\n \t// concurrency pulling of messages. Otherwise, NumGoroutines will be set to 1.\n \tsub.ReceiveSettings.Synchronous = false\n \t// NumGoroutines determines the number of goroutines sub.Receive will spawn to pull\n \t// messages.\n \tsub.ReceiveSettings.NumGoroutines = 16\n \t// MaxOutstandingMessages limits the number of concurrent handlers of messages.\n \t// In this case, up to 8 unacked messages can be handled concurrently.\n \t// Note, even in synchronous mode, messages pulled in a batch can still be handled\n \t// concurrently.\n \tsub.ReceiveSettings.MaxOutstandingMessages = 8\n\n \t// Receive messages for 10 seconds, which simplifies testing.\n \t// Comment this out in production, since `Receive` should\n \t// be used as a long running operation.\n \tctx, cancel := context.WithTimeout(ctx, 10*time.Second)\n \tdefer cancel()\n\n \tvar received int32\n\n \t// Receive blocks until the context is cancelled or an error occurs.\n \terr = sub.Receive(ctx, func(_ context.Context, msg *pubsub.Message) {\n \t\tatomic.AddInt32(&received, 1)\n \t\tmsg.Ack()\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"sub.Receive returned error: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Received %d messages\\n\", received)\n\n \treturn nil\n }\n\n### Ruby\n\n\nBefore trying this sample, follow the Ruby setup instructions in the\n[Pub/Sub quickstart using\nclient libraries](/pubsub/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Pub/Sub Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html).\n\n\nTo authenticate to Pub/Sub, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # subscription_id = \"your-subscription-id\"\n\n pubsub = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud.html.new\n\n subscription = pubsub.subscription subscription_id\n # Use 2 threads for streaming, 4 threads for executing callbacks and 2 threads\n # for sending acknowledgements and/or delays\n subscriber = subscription.listen streams: 2, threads: {\n callback: 4,\n push: 2\n } do |received_message|\n puts \"Received message: #{received_message.data}\"\n received_message.acknowledge!\n end\n\n subscriber.start\n # Let the main thread sleep for 60 seconds so the thread for listening\n # messages does not quit\n sleep 60\n subscriber.stop.wait!\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=pubsub)."]]