Skip to content

Commit 57d7ea4

Browse files
committed
Add MQDLTMH calls to sample programs
1 parent 85b69be commit 57d7ea4

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

samples/amqscb.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ func mainWithRc() int {
9292
defer disc(qMgrObject)
9393
}
9494

95+
if err == nil {
96+
cmho := ibmmq.NewMQCMHO()
97+
mh, err = qMgrObject.CrtMH(cmho)
98+
if err == nil {
99+
defer dltMh(mh)
100+
}
101+
}
102+
95103
// Open the queue
96104
if err == nil {
97105
// Create the Object Descriptor that allows us to give the queue name
@@ -114,11 +122,6 @@ func mainWithRc() int {
114122
}
115123
}
116124

117-
if err == nil {
118-
cmho := ibmmq.NewMQCMHO()
119-
mh, err = qMgrObject.CrtMH(cmho)
120-
}
121-
122125
if err == nil {
123126
// The GET/MQCB requires control structures, the Message Descriptor (MQMD)
124127
// and Get Options (MQGMO). Create those with default values.
@@ -145,6 +148,9 @@ func mainWithRc() int {
145148
// Register the callback function along with any selection criteria from the
146149
// MQMD and MQGMO parameters
147150
err = qObject.CB(ibmmq.MQOP_REGISTER, cbd, getmqmd, gmo)
151+
if err == nil {
152+
defer dereg(qObject, cbd, getmqmd, gmo)
153+
}
148154
}
149155

150156
if err == nil {
@@ -155,7 +161,7 @@ func mainWithRc() int {
155161
if err == nil {
156162
// Use defer to disable the message consumer when we are ready to exit.
157163
// Otherwise the shutdown will give MQRC_HCONN_ASYNC_ACTIVE error
158-
defer qMgrObject.Ctl(ibmmq.MQOP_STOP, ctlo)
164+
defer stopCB(qMgrObject)
159165
}
160166
}
161167

@@ -175,6 +181,19 @@ func mainWithRc() int {
175181
return mqret
176182
}
177183

184+
// Various cleanup functions that ought to be called at the end of the program for neatness.
185+
186+
// Stop the callback function from being called again
187+
func stopCB(qMgrObject ibmmq.MQQueueManager) {
188+
ctlo := ibmmq.NewMQCTLO()
189+
err := qMgrObject.Ctl(ibmmq.MQOP_STOP, ctlo)
190+
if err == nil {
191+
fmt.Printf("Stopped callback function\n")
192+
} else {
193+
fmt.Println(err)
194+
}
195+
}
196+
178197
// Disconnect from the queue manager
179198
func disc(qMgrObject ibmmq.MQQueueManager) error {
180199
err := qMgrObject.Disc()
@@ -196,3 +215,27 @@ func close(object ibmmq.MQObject) error {
196215
}
197216
return err
198217
}
218+
219+
// Deallocate the message handle
220+
func dltMh(mh ibmmq.MQMessageHandle) error {
221+
dmho := ibmmq.NewMQDMHO()
222+
err := mh.DltMH(dmho)
223+
if err == nil {
224+
fmt.Println("Closed a Msg Handle")
225+
} else {
226+
fmt.Println(err)
227+
}
228+
return err
229+
}
230+
231+
// Deregister the callback function - have to do this before the message handle can be
232+
// successfully deleted
233+
func dereg(qObject ibmmq.MQObject, cbd *ibmmq.MQCBD, getmqmd *ibmmq.MQMD, gmo *ibmmq.MQGMO) error {
234+
err := qObject.CB(ibmmq.MQOP_DEREGISTER, cbd, getmqmd, gmo)
235+
if err == nil {
236+
fmt.Println("Deregistered callback")
237+
} else {
238+
fmt.Println(err)
239+
}
240+
return err
241+
}

samples/amqsprop.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ func mainWithRc() int {
246246
putMsgHandle, err = qMgrObject.CrtMH(cmho)
247247
if err != nil {
248248
fmt.Println(err)
249+
} else {
250+
defer dltMh(putMsgHandle)
249251
}
250252
}
251253

@@ -255,6 +257,8 @@ func mainWithRc() int {
255257
getMsgHandle, err = qMgrObject.CrtMH(cmho)
256258
if err != nil {
257259
fmt.Println(err)
260+
} else {
261+
defer dltMh(getMsgHandle)
258262
}
259263
}
260264

@@ -351,3 +355,15 @@ func close(object ibmmq.MQObject) error {
351355
}
352356
return err
353357
}
358+
359+
// Clean up message handle
360+
func dltMh(mh ibmmq.MQMessageHandle) error {
361+
dmho := ibmmq.NewMQDMHO()
362+
err := mh.DltMH(dmho)
363+
if err == nil {
364+
fmt.Println("Closed a Msg Handle")
365+
} else {
366+
fmt.Println(err)
367+
}
368+
return err
369+
}

0 commit comments

Comments
 (0)