@@ -71,13 +71,13 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
71
71
72
72
getmqmd := ibmmq .NewMQMD ()
73
73
74
- myBufferSize := 32768
74
+ bufferSize := 32768
75
75
76
76
if consumer .ctx .receiveBufferSize > 0 {
77
- myBufferSize = consumer .ctx .receiveBufferSize
77
+ bufferSize = consumer .ctx .receiveBufferSize
78
78
}
79
79
80
- buffer := make ([]byte , myBufferSize )
80
+ buffer := make ([]byte , bufferSize )
81
81
82
82
// Calculate the syncpoint value
83
83
syncpointSetting := ibmmq .MQGMO_NO_SYNCPOINT
@@ -88,6 +88,7 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
88
88
// Set the GMO (get message options)
89
89
gmo .Options |= syncpointSetting
90
90
gmo .Options |= ibmmq .MQGMO_FAIL_IF_QUIESCING
91
+ gmo .Options |= ibmmq .MQGMO_ACCEPT_TRUNCATED_MSG
91
92
92
93
// Include the message properties in the msgHandle
93
94
gmo .Options |= ibmmq .MQGMO_PROPERTIES_IN_HANDLE
@@ -105,7 +106,24 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
105
106
// Use the prepared objects to ask for a message from the queue.
106
107
datalen , err := consumer .qObject .Get (getmqmd , gmo , buffer )
107
108
108
- if err == nil {
109
+ // Establish the read length so that it does not exceed the size of the buffer.
110
+ readLength := datalen
111
+ if datalen > bufferSize {
112
+ readLength = bufferSize
113
+ }
114
+
115
+ // Check whether this is a truncated message.
116
+ isTruncatedMessage := false
117
+ if err != nil && ((err .(* ibmmq.MQReturn )).MQRC == ibmmq .MQRC_TRUNCATED_MSG_ACCEPTED ) {
118
+ isTruncatedMessage = true
119
+
120
+ // In the truncated message case we want to return the warning object as well as the message
121
+ // so that the application can tell that some of the data is missing.
122
+ jmsErr = CreateJMSExceptionFromMQReturn (err )
123
+ }
124
+
125
+ // Golden path - typically a message was received without error.
126
+ if err == nil || isTruncatedMessage {
109
127
110
128
// Set a finalizer on the message handle to allow it to be deleted
111
129
// when it is no longer referenced by an active object, to reduce/prevent
@@ -119,8 +137,8 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
119
137
120
138
var msgBodyStr * string
121
139
122
- if datalen > 0 {
123
- strContent := string (buffer [:datalen ])
140
+ if readLength > 0 {
141
+ strContent := string (buffer [:readLength ])
124
142
msgBodyStr = & strContent
125
143
}
126
144
@@ -135,11 +153,11 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
135
153
136
154
} else {
137
155
138
- if datalen == 0 {
156
+ if readLength == 0 {
139
157
buffer = []byte {}
140
158
}
141
159
142
- trimmedBuffer := buffer [0 :datalen ]
160
+ trimmedBuffer := buffer [0 :readLength ]
143
161
144
162
// Not a string, so fall back to BytesMessage
145
163
msg = & BytesMessageImpl {
@@ -172,11 +190,7 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
172
190
173
191
// Parse the details of the error and return it to the caller as
174
192
// a JMSException
175
- rcInt := int (mqret .MQRC )
176
- errCode := strconv .Itoa (rcInt )
177
- reason := ibmmq .MQItoString ("RC" , rcInt )
178
-
179
- jmsErr = jms20subset .CreateJMSException (reason , errCode , err )
193
+ jmsErr = CreateJMSExceptionFromMQReturn (err )
180
194
}
181
195
182
196
}
@@ -417,3 +431,20 @@ func (consumer ConsumerImpl) Close() {
417
431
418
432
return
419
433
}
434
+
435
+ func CreateJMSExceptionFromMQReturn (mqretError error ) jms20subset.JMSException {
436
+
437
+ // Assumes this error code was returned from MQ call.
438
+ mqret := mqretError .(* ibmmq.MQReturn )
439
+
440
+ // Parse the details of the error and return it to the caller as
441
+ // a JMSException
442
+ rcInt := int (mqret .MQRC )
443
+ errCode := strconv .Itoa (rcInt )
444
+ reason := ibmmq .MQItoString ("RC" , rcInt )
445
+
446
+ jmsErr := jms20subset .CreateJMSException (reason , errCode , mqretError )
447
+
448
+ return jmsErr
449
+
450
+ }
0 commit comments