Skip to content

Commit ad1eb73

Browse files
committed
Update amqsget sample to show use of PutDateTime
1 parent 614090d commit ad1eb73

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

samples/amqsget.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package main
1818

1919
/*
20-
Copyright (c) IBM Corporation 2018
20+
Copyright (c) IBM Corporation 2018, 2021
2121
2222
Licensed under the Apache License, Version 2.0 (the "License");
2323
you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@ import (
4040
"fmt"
4141
"os"
4242
"strings"
43+
"time"
4344

4445
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
4546
)
@@ -114,6 +115,8 @@ func mainWithRc() int {
114115
for msgAvail == true && err == nil {
115116
var datalen int
116117

118+
gotMsg := false // So we can do some common work on the message if one were retrieved
119+
117120
// The GET requires control structures, the Message Descriptor (MQMD)
118121
// and Get Options (MQGMO). Create those with default values.
119122
getmqmd := ibmmq.NewMQMD()
@@ -173,6 +176,7 @@ func mainWithRc() int {
173176
// if it's been created by the amqsput program
174177
fmt.Printf("Got message of length %d: ", datalen)
175178
fmt.Println(strings.TrimSpace(string(buffer)))
179+
gotMsg = true
176180
}
177181
} else {
178182
// Create a buffer for the message data. This one is large enough
@@ -196,6 +200,20 @@ func mainWithRc() int {
196200
// if it's been created by the amqsput program
197201
fmt.Printf("Got message of length %d: ", datalen)
198202
fmt.Println(strings.TrimSpace(string(buffer[:datalen])))
203+
gotMsg = true
204+
}
205+
}
206+
207+
// Demonstrate how the PutDateTime value can be used
208+
if gotMsg {
209+
t := getmqmd.PutDateTime
210+
if !t.IsZero() {
211+
diff := time.Now().Sub(t)
212+
round, _ := time.ParseDuration("1s")
213+
diff = diff.Round(round)
214+
fmt.Printf("Message was put %d seconds ago\n", int(diff.Seconds()))
215+
} else {
216+
fmt.Printf("Message has empty PutDateTime - MQMD PutDate:'%s' PutTime:'%s'\n", getmqmd.PutDate, getmqmd.PutTime)
199217
}
200218
}
201219
}

0 commit comments

Comments
 (0)