@@ -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
179198func 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+ }
0 commit comments