v1 Subscribe with exactly once delivery (DEPRECATED)
Stay organized with collections
Save and categorize content based on your preferences.
(DEPRECATED) Subscribe with exactly once delivery
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 exactly once delivery (DEPRECATED)\n\n(DEPRECATED) Subscribe with exactly once delivery\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\"time\"\n\n \t\"cloud.google.com/go/pubsub\"\n \t\"google.golang.org/api/option\"\n )\n\n // receiveMessagesWithExactlyOnceDeliveryEnabled instantiates a subscriber client.\n // This differs from regular subscribing since you must call msg.AckWithResult()\n // or msg.NackWithResult() instead of the regular Ack/Nack methods.\n // When exactly once delivery is enabled on the subscription, the message is\n // guaranteed to not be delivered again if the ack result succeeds.\n func receiveMessagesWithExactlyOnceDeliveryEnabled(w io.Writer, projectID, subID string) error {\n \t// projectID := \"my-project-id\"\n \t// subID := \"my-sub\"\n \tctx := context.Background()\n\n \t// Pub/Sub's exactly once delivery guarantee only applies when subscribers connect to the service in the same region.\n \t// For list of locational endpoints for Pub/Sub, see https://cloud.google.com/pubsub/docs/reference/service_apis_overview#list_of_locational_endpoints\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, option.WithEndpoint(\"us-west1-pubsub.googleapis.com:443\"))\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// Set MinExtensionPeriod high to avoid any unintentional\n \t// acknowledgment expirations (e.g. due to network events).\n \t// This can lead to high tail latency in case of client crashes.\n \tsub.ReceiveSettings.MinExtensionPeriod = 600 * time.Second\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 \terr = sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {\n \t\tfmt.Fprintf(w, \"Got message: %q\\n\", string(msg.Data))\n \t\tr := msg.AckWithResult()\n \t\t// Block until the result is returned and a pubsub.AcknowledgeStatus\n \t\t// is returned for the acked message.\n \t\tstatus, err := r.Get(ctx)\n \t\tif err != nil {\n \t\t\tfmt.Fprintf(w, \"MessageID: %s failed when calling result.Get: %v\", msg.ID, err)\n \t\t}\n\n \t\tswitch status {\n \t\tcase pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_AcknowledgeStatusSuccess_AcknowledgeStatusPermissionDenied_AcknowledgeStatusFailedPrecondition_AcknowledgeStatusInvalidAckID_AcknowledgeStatusOther:\n \t\t\tfmt.Fprintf(w, \"Message successfully acked: %s\", msg.ID)\n \t\tcase pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_AcknowledgeStatusSuccess_AcknowledgeStatusPermissionDenied_AcknowledgeStatusFailedPrecondition_AcknowledgeStatusInvalidAckID_AcknowledgeStatusOther:\n \t\t\tfmt.Fprintf(w, \"Message failed to ack with response of Invalid. ID: %s\", msg.ID)\n \t\tcase pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_AcknowledgeStatusSuccess_AcknowledgeStatusPermissionDenied_AcknowledgeStatusFailedPrecondition_AcknowledgeStatusInvalidAckID_AcknowledgeStatusOther:\n \t\t\tfmt.Fprintf(w, \"Message failed to ack with response of Permission Denied. ID: %s\", msg.ID)\n \t\tcase pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_AcknowledgeStatusSuccess_AcknowledgeStatusPermissionDenied_AcknowledgeStatusFailedPrecondition_AcknowledgeStatusInvalidAckID_AcknowledgeStatusOther:\n \t\t\tfmt.Fprintf(w, \"Message failed to ack with response of Failed Precondition. ID: %s\", msg.ID)\n \t\tcase pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_AcknowledgeStatusSuccess_AcknowledgeStatusPermissionDenied_AcknowledgeStatusFailedPrecondition_AcknowledgeStatusInvalidAckID_AcknowledgeStatusOther:\n \t\t\tfmt.Fprintf(w, \"Message failed to ack with response of Other. ID: %s\", msg.ID)\n \t\tdefault:\n \t\t}\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"got err from sub.Receive: %w\", err)\n \t}\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 require \"google/cloud/pubsub\"\n\n # Shows how to register callback to acknowledge method and access the result passed in\n class PubsubSubscriberExactlyOnceDelivery\n def subscriber_exactly_once_delivery project_id:, topic_id:, subscription_id:\n pubsub = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud.html.new project_id: project_id\n topic = pubsub.topic topic_id\n subscription = pubsub.subscription subscription_id\n subscriber = subscription.listen do |received_message|\n puts \"Received message: #{received_message.data}\"\n\n # Pass in callback to access the acknowledge result.\n # For subscription with Exactly once delivery disabled the result will be success always.\n received_message.acknowledge! do |result|\n puts \"Acknowledge result's status: #{result.status}\"\n end\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 end\n\n def self.run\n # TODO(developer): Replace these variables before running the sample.\n project_id = \"your-project-id\"\n topic_id = \"your-topic-id\"\n subscription_id = \"id-for-new-subcription\" # subscription with exactly once delivery enabled\n PubsubSubscriberExactlyOnceDelivery.new.subscriber_exactly_once_delivery project_id: project_id,\n topic_id: topic_id,\n subscription_id: subscription_id\n end\n end\n\n if $PROGRAM_NAME == __FILE__\n PubsubSubscriberExactlyOnceDelivery.run\n end\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)."]]