@@ -15,6 +15,7 @@ import (
15
15
"runtime"
16
16
"strconv"
17
17
"strings"
18
+ "sync"
18
19
19
20
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
20
21
ibmmq "github.com/ibm-messaging/mq-golang/v5/ibmmq"
@@ -109,24 +110,7 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
109
110
// Set a finalizer on the message handle to allow it to be deleted
110
111
// when it is no longer referenced by an active object, to reduce/prevent
111
112
// memory leaks.
112
- runtime .SetFinalizer (& thisMsgHandle , func (msgHandle * ibmmq.MQMessageHandle ) {
113
- consumer .ctx .ctxLock .Lock ()
114
- dmho := ibmmq .NewMQDMHO ()
115
- err := msgHandle .DltMH (dmho )
116
- if err != nil {
117
-
118
- mqret := err .(* ibmmq.MQReturn )
119
-
120
- if mqret .MQRC == ibmmq .MQRC_HCONN_ERROR {
121
- // Expected if the connection is closed before the finalizer executes
122
- // (at which point it should get tidied up automatically by the connection)
123
- } else {
124
- fmt .Println ("DltMH finalizer" , err )
125
- }
126
-
127
- }
128
- consumer .ctx .ctxLock .Unlock ()
129
- })
113
+ setMessageHandlerFinalizer (thisMsgHandle , consumer .ctx .ctxLock )
130
114
131
115
// Message received successfully (without error).
132
116
// Determine on the basis of the format field what sort of message to create.
@@ -198,6 +182,36 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
198
182
return msg , jmsErr
199
183
}
200
184
185
+ /*
186
+ * Set a finalizer on the message handle to allow it to be deleted
187
+ * when it is no longer referenced by an active object, to reduce/prevent
188
+ * memory leaks.
189
+ */
190
+ func setMessageHandlerFinalizer (thisMsgHandle ibmmq.MQMessageHandle , ctxLock * sync.Mutex ) {
191
+
192
+ runtime .SetFinalizer (& thisMsgHandle , func (msgHandle * ibmmq.MQMessageHandle ) {
193
+ ctxLock .Lock ()
194
+ defer ctxLock .Unlock ()
195
+
196
+ dmho := ibmmq .NewMQDMHO ()
197
+ err := msgHandle .DltMH (dmho )
198
+ if err != nil {
199
+
200
+ mqret := err .(* ibmmq.MQReturn )
201
+
202
+ if mqret .MQRC == ibmmq .MQRC_HCONN_ERROR {
203
+ // Expected if the connection is closed before the finalizer executes
204
+ // (at which point it should get tidied up automatically by the connection)
205
+ } else {
206
+ fmt .Println ("DltMH finalizer" , err )
207
+ }
208
+
209
+ }
210
+
211
+ })
212
+
213
+ }
214
+
201
215
// ReceiveStringBodyNoWait implements the IBM MQ logic necessary to receive a
202
216
// message from a Destination and return its body as a string.
203
217
//
0 commit comments