-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
278 lines (224 loc) · 6.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//go:build amd64 && (linux || windows)
/*
This is a short sample to show how to call IBM MQ from
a Go program.
The flow is to connect to a queue manager,
open the queue named on the command line,
put a message and then get it back.
The queue is closed.
The program then subscribes to the topic corresponding
to collecting activity trace for itself - this requires MQ V9.
Finally, it closes the subscription and target queue, and
disconnects.
If an error occurs at any stage, the error is reported and
subsequent steps skipped.
*/
package main
/*
Copyright (c) IBM Corporation 2016
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the license.
Contributors:
Mark Taylor - Initial Contribution
*/
import (
"fmt"
"os"
"strings"
"time"
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
)
func main() {
var openOptions int32
var qMgrObject ibmmq.MQObject
var qObject ibmmq.MQObject
var managedQObject ibmmq.MQObject
var subObject ibmmq.MQObject
var qMgrName string
if len(os.Args) != 3 {
fmt.Println("mqitest <qname> <qmgrname>")
fmt.Println(" Both parms required")
os.Exit(1)
}
qMgrName = os.Args[2]
connected := false
qMgr, err := ibmmq.Conn(qMgrName)
if err != nil {
fmt.Println(err)
} else {
connected = true
fmt.Println("Connected to queue manager ", qMgrName)
}
// MQOPEN of the queue named on command line
if err == nil {
mqod := ibmmq.NewMQOD()
openOptions = ibmmq.MQOO_OUTPUT + ibmmq.MQOO_FAIL_IF_QUIESCING
openOptions |= ibmmq.MQOO_INPUT_AS_Q_DEF
mqod.ObjectType = ibmmq.MQOT_Q
mqod.ObjectName = os.Args[1]
qObject, err = qMgr.Open(mqod, openOptions)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Opened queue", qObject.Name)
}
}
// MQPUT a message
// Create the standard MQI structures MQMD, MQPMO and
// set the values.
// The message is always sent as bytes, so has to be converted
// before the MQPUT.
if err == nil {
putmqmd := ibmmq.NewMQMD()
pmo := ibmmq.NewMQPMO()
pmo.Options = ibmmq.MQPMO_SYNCPOINT | ibmmq.MQPMO_NEW_MSG_ID | ibmmq.MQPMO_NEW_CORREL_ID
putmqmd.Format = "MQSTR"
msgData := "Hello from Go at " + time.Now().Format("02 Jan 2006 03:04:05")
buffer := []byte(msgData)
err = qObject.Put(putmqmd, pmo, buffer)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Put message to", qObject.Name)
}
}
// The message was put in syncpoint so it needs
// to be committed.
if err == nil {
err = qMgr.Cmit()
if err != nil {
fmt.Println(err)
}
}
// MQGET all messages on the queue. Wait 3 seconds for any more
// to arrive.
if err == nil {
msgAvail := true
for msgAvail == true {
var datalen int
getmqmd := ibmmq.NewMQMD()
gmo := ibmmq.NewMQGMO()
gmo.Options = ibmmq.MQGMO_NO_SYNCPOINT | ibmmq.MQGMO_FAIL_IF_QUIESCING
gmo.Options |= ibmmq.MQGMO_WAIT
gmo.WaitInterval = 3000
buffer := make([]byte, 32768)
datalen, err = qObject.Get(getmqmd, gmo, buffer)
if err != nil {
msgAvail = false
fmt.Println(err)
mqret := err.(*ibmmq.MQReturn)
if mqret.MQRC == ibmmq.MQRC_NO_MSG_AVAILABLE {
// not a real error so reset err
err = nil
}
} else {
fmt.Printf("Got message of length %d: ", datalen)
fmt.Println(strings.TrimSpace(string(buffer[:datalen])))
}
}
}
// MQCLOSE the queue
if err == nil {
err = qObject.Close(0)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Closed queue")
}
}
// This section demonstrates subscribing to a topic
// where the topic string is set to collect activity trace
// from this program - it needs MQ V9 for publications to
// automatically be generated on this topic.
if err == nil {
mqsd := ibmmq.NewMQSD()
mqsd.Options = ibmmq.MQSO_CREATE
mqsd.Options |= ibmmq.MQSO_NON_DURABLE
mqsd.Options |= ibmmq.MQSO_FAIL_IF_QUIESCING
mqsd.Options |= ibmmq.MQSO_MANAGED
mqsd.ObjectString = "$SYS/MQ/INFO/QMGR/" + qMgrName + "/ActivityTrace/ApplName/mqitest"
subObject, err = qMgr.Sub(mqsd, &managedQObject)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Subscribed to topic ", mqsd.ObjectString)
}
}
// Loop on the managed queue created by the MQSUB call until there
// are no more messages. Because these are going to be PCF-format
// events, they cannot be simply printed so here I'm just
// printing the format of the message to show that something has
// been retrieved.
if err == nil {
msgAvail := true
for msgAvail == true {
var datalen int
getmqmd := ibmmq.NewMQMD()
gmo := ibmmq.NewMQGMO()
gmo.Options = ibmmq.MQGMO_NO_SYNCPOINT | ibmmq.MQGMO_FAIL_IF_QUIESCING
gmo.Options |= ibmmq.MQGMO_WAIT
gmo.WaitInterval = 3000
buffer := make([]byte, 32768)
datalen, err = managedQObject.Get(getmqmd, gmo, buffer)
if err != nil {
msgAvail = false
fmt.Println(err)
mqret := err.(*ibmmq.MQReturn)
if mqret.MQRC == ibmmq.MQRC_NO_MSG_AVAILABLE {
// not a real error so reset err, but
// end retrieval loop
err = nil
}
} else {
fmt.Printf("Got message of length %d. Format = %s\n", datalen, getmqmd.Format)
}
}
}
// MQCLOSE the subscription, ignoring errors.
if err == nil {
subObject.Close(0)
}
if err == nil {
mqod := ibmmq.NewMQOD()
openOptions = ibmmq.MQOO_INQUIRE + ibmmq.MQOO_FAIL_IF_QUIESCING
mqod.ObjectType = ibmmq.MQOT_Q_MGR
mqod.ObjectName = ""
qMgrObject, err = qMgr.Open(mqod, openOptions)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Opened QMgr for MQINQ\n")
}
}
if err == nil {
selectors := []int32{ibmmq.MQCA_Q_MGR_NAME,
ibmmq.MQCA_DEAD_LETTER_Q_NAME,
ibmmq.MQIA_MSG_MARK_BROWSE_INTERVAL}
values, err := qMgrObject.Inq(selectors)
if err != nil {
fmt.Println(err)
} else {
returnedName := values[ibmmq.MQCA_Q_MGR_NAME]
fmt.Printf("MQINQ returned %v \n", values)
fmt.Printf(" QM is '%s'\n", returnedName)
}
}
// MQDISC regardless of other errors
if connected {
err = qMgr.Disc()
fmt.Println("Disconnected from queue manager ", qMgrName)
}
if err == nil {
os.Exit(0)
} else {
mqret := err.(*ibmmq.MQReturn)
os.Exit((int)(mqret.MQCC))
}
}