Last active
November 1, 2019 16:48
-
-
Save adamatti/1221f9aacf8ae32c4b47c95f62a70739 to your computer and use it in GitHub Desktop.
Sending a message to AWS / local #sample #aws
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='software.amazon.awssdk', module='sqs', version='2.5.36') | |
import software.amazon.awssdk.regions.Region | |
import software.amazon.awssdk.services.sqs.SqsClient | |
import software.amazon.awssdk.services.sqs.model.* | |
import software.amazon.awssdk.auth.credentials.* | |
def credentials = StaticCredentialsProvider.create( | |
AwsBasicCredentials.create("test","test") | |
) | |
def sqsClient = SqsClient.builder() | |
.region(Region.US_EAST_1) | |
.endpointOverride("http://localhost:4576".toURI()) | |
.credentialsProvider(credentials) | |
.build() | |
def queueName = "sampleQueue" | |
// Create queue (optional) | |
def createQueueRequest = CreateQueueRequest.builder().queueName(queueName).build(); | |
def createQueueResponse = sqsClient.createQueue(createQueueRequest) | |
println "Create queue response: ${createQueueResponse}" | |
// Send a message | |
def queue = sqsClient.getQueueUrl( | |
GetQueueUrlRequest.builder().queueName(queueName).build() | |
) | |
def file = new File("payload.json") | |
def payload = file.exists() ? file.getText() : "Hello world!" | |
def request = SendMessageRequest.builder() | |
.queueUrl(queue.queueUrl()) | |
.messageBody(payload) | |
.delaySeconds(10) | |
.build() | |
println "Sending ${payload}" | |
def response = sqsClient.sendMessage(request) | |
println "Send msg response: ${response}" | |
println "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment