Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

Amazon SQS: Extend or Release Message Visibility

See more Amazon SQS Examples

For an in-flight delivery, set 120 seconds of visibility from the change request, or use release mode to set zero. This does not acknowledge/delete the message or change the queue default. Release only after stopping local work to avoid concurrent processing by another consumer.

Use the latest receipt handle and extend before the existing timeout expires. Extensions cannot exceed the maximum remaining time within the 12-hour window measured from the original ReceiveMessage request; 120 seconds can therefore be rejected near that limit. The override is not retained for a later delivery. Visibility does not guarantee against duplicates. Production workers should renew with a bounded schedule and stop acknowledging if they lose ownership of the delivery.

Uses SQS's AWS JSON protocol: POST to the regional root / with Content-Type: application/x-amz-json-1.0 and an operation-specific X-Amz-Target: AmazonSQS.... Chilkat.Rest and AuthAws sign with AWS Signature Version 4, service sqs. AWS still supports the older Query/XML protocol; these examples use JSON for simpler request and response handling. No AWS SDK is required.

Provide current IAM credentials in a private qa_data/aws/sqs.json with string fields access_key, secret_key, and session_token for temporary credentials. Queue-specific examples also read queue_url from this file. Set the region in the code to match the queue. These examples use commercial regional endpoints; other partitions or custom endpoints require corresponding changes. Prefer short-lived role credentials. The file is explicitly loaded; profiles and credential refresh are not automatic. Keep it outside public content and source control. The temporary token is included in the signed query; keep URLs and diagnostics private. TLS certificate verification is enabled.

Transport completion, HTTP status, and response validity are checked separately. Use bounded backoff with jitter for retryable errors, and refresh expired credentials. A timed-out write or receive may already have been accepted. Standard queues can deliver duplicates: make application processing idempotent and delete only after successful processing. Automatic reconnection is disabled for writes and receives to avoid replay after a lost response. Read-only queue listing permits reconnection.

Required IAM action: sqs:ChangeMessageVisibility. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.

AWS ChangeMessageVisibility reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_TextCompare
string ls_ReceiptHandle
string ls_Operation
long li_SameText1
long li_SameText2
long li_SameText3
long li_SameText4
oleobject loo_Credentials
long li_SameText5
long li_SameText6
string ls_Region
oleobject loo_Endpoint
string ls_QueueUrl
long li_SameText7
oleobject loo_ExpectedPrefix
oleobject loo_QueueText
oleobject loo_Aws
oleobject loo_Rest
long li_SameText8
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_RequestBody
oleobject loo_ResponseBody
long li_VisibilitySeconds
long li_SameText9

li_Success = 0

//  StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_TextCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Use the latest handle while this delivery is in flight.
ls_ReceiptHandle = "LATEST_RECEIPT_HANDLE"
ls_Operation = "extend"
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_ReceiptHandle)
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_ReceiptHandle)
li_SameText2 = loo_TextCompare.ContentsEqual("LATEST_RECEIPT_HANDLE",1)
if (li_SameText1 = 1) OR (li_SameText2 = 1) then
    Write-Debug "Supply receiptHandle from the application before running."
    destroy loo_TextCompare
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText3 = loo_TextCompare.ContentsEqual("extend",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText4 = loo_TextCompare.ContentsEqual("release",1)
if (li_SameText3 <> 1) AND (li_SameText4 <> 1) then
    Write-Debug "Choose extend or release."
    destroy loo_TextCompare
    return
end if

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
//  Private configuration, not a file in public web content or source control.
//  Load current IAM credentials authorized for the SQS operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/sqs.json")
if li_Success <> 1 then
    Write-Debug "Could not load private AWS credentials."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("access_key"))
li_SameText5 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText5 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_SameText6 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText6 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

//  Set this once to the region containing the queue.
ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Endpoint.Append("sqs.")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(ls_Region)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".amazonaws.com")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

//  Use the exact QueueUrl returned by CreateQueue, GetQueueUrl, or ListQueues.
//  This example targets a standard commercial regional endpoint, not a custom endpoint.
ls_QueueUrl = loo_Credentials.StringOf("queue_url")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_QueueUrl)
li_SameText7 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("queue_url") <> 1) OR (li_SameText7 = 1) then
    Write-Debug "Missing or invalid queue_url in sqs.json."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

loo_ExpectedPrefix = create oleobject
li_rc = loo_ExpectedPrefix.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ExpectedPrefix.Append("https://")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    return
end if

li_Success = loo_ExpectedPrefix.Append(loo_Endpoint.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    return
end if

li_Success = loo_ExpectedPrefix.Append("/")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    return
end if

loo_QueueText = create oleobject
li_rc = loo_QueueText.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_QueueText.Append(ls_QueueUrl)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    return
end if

if loo_QueueText.StartsWith(loo_ExpectedPrefix.GetAsString(),1) <> 1 then
    Write-Debug "Queue URL must use HTTPS and the configured SQS region."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    return
end if

loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")

loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "sqs"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
    Write-Debug "Could not configure AWS signing."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    return
end if

//  Temporary IAM/STS credentials require their session token on every signed request.
//  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
//  Leave session_token absent or empty only for credentials that do not have one.
if loo_Credentials.HasMember("session_token") = 1 then
    if loo_Credentials.JsonTypeOf("session_token") <> 1 then
        Write-Debug "session_token must be a string."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_ExpectedPrefix
        destroy loo_QueueText
        destroy loo_Aws
        destroy loo_Rest
        return
    end if

    loo_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_SameText8 = loo_TextCompare.ContentsEqual("",1)
    if li_SameText8 <> 1 then
        li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
        if li_Success <> 1 then
            Write-Debug "Could not set the session-token parameter."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_ExpectedPrefix
            destroy loo_QueueText
            destroy loo_Aws
            destroy loo_Rest
            return
        end if

    end if

end if

//  Supply a Socket to explicitly enable TLS certificate verification.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

//  Do not automatically reconnect/replay a write or receive after a lost response.
li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
    Write-Debug "Could not attach the TLS connection."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.0")
if li_Success <> 1 then
    Write-Debug "Could not set the content type."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

loo_Result.Strict = 1
loo_RequestBody = create oleobject
li_rc = loo_RequestBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_ResponseBody = create oleobject
li_rc = loo_ResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Rest.AddHeader("X-Amz-Target","AmazonSQS.ChangeMessageVisibility")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Body.UpdateString("QueueUrl",ls_QueueUrl)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Body.UpdateString("ReceiptHandle",ls_ReceiptHandle)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_VisibilitySeconds = 120
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText9 = loo_TextCompare.ContentsEqual("release",1)
if li_SameText9 = 1 then
    li_VisibilitySeconds = 0
end if

li_Success = loo_Body.UpdateInt("VisibilityTimeout",li_VisibilitySeconds)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

//  EmitSb appends, so clear both builders before each request/page.
loo_RequestBody.Clear()
loo_ResponseBody.Clear()
li_Success = loo_Body.EmitSb(loo_RequestBody)
if li_Success <> 1 then
    Write-Debug "Could not serialize JSON."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Rest.FullRequestSb("POST","/",loo_RequestBody,loo_ResponseBody)
if li_Success <> 1 then
    Write-Debug "Request did not complete; inspect diagnostics privately."
    //  A write or receive may have reached SQS. Do not blindly replay it.
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "SQS HTTP status: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
    li_Success = loo_Result.LoadSb(loo_ResponseBody)
    if (li_Success = 1) AND (loo_Result.JsonTypeOf("__type") = 1) then
        Write-Debug "AWS error type: " + loo_Result.StringOf("__type")
    else
        Write-Debug "AWS error type: " + loo_Rest.ResponseHdrByName("x-amzn-errortype")
    end if

    //  Error messages, payloads, receipt handles and signed URLs can be private.
    //  HTTP 400 can be throttling, expired credentials, or invalid parameters.
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

//  This operation normally returns an empty HTTP body on success.
//  Also accept an empty JSON object; never require XML for this protocol.
if loo_ResponseBody.Length > 0 then
    li_Success = loo_ResponseBody.Trim()
    if li_Success <> 1 then
        Write-Debug "Could not inspect response."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_ExpectedPrefix
        destroy loo_QueueText
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_RequestBody
        destroy loo_ResponseBody
        return
    end if

    if loo_ResponseBody.StartsWith("{",1) <> 1 then
        Write-Debug "Expected a JSON object response."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_ExpectedPrefix
        destroy loo_QueueText
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_RequestBody
        destroy loo_ResponseBody
        return
    end if

    li_Success = loo_Result.LoadSb(loo_ResponseBody)
    if li_Success <> 1 then
        Write-Debug "Expected well-formed JSON."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_ExpectedPrefix
        destroy loo_QueueText
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_RequestBody
        destroy loo_ResponseBody
        return
    end if

    if loo_Result.Size <> 0 then
        Write-Debug "Unexpected fields in the empty-result response."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_ExpectedPrefix
        destroy loo_QueueText
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_RequestBody
        destroy loo_ResponseBody
        return
    end if

end if

Write-Debug "Visibility change accepted for this delivery."
//  Stop local work before releasing it. Another consumer can now receive it.


destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody