Skip to content

Commit 1a8f533

Browse files
committed
Return truncated message length in JMSExceptionImpl - #77
1 parent 9746790 commit 1a8f533

File tree

3 files changed

+98
-25
lines changed

3 files changed

+98
-25
lines changed

jms20subset/JMSException.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ type JMSException interface {
2424

2525
// JMSExceptionImpl is a struct that implements the JMSException interface
2626
type JMSExceptionImpl struct {
27-
reason string
28-
errorCode string
29-
linkedErr error
27+
reason string
28+
errorCode string
29+
linkedErr error
30+
messageLength int
3031
}
3132

3233
// GetReason returns the provider-specific reason string describing the error.
@@ -51,6 +52,15 @@ func (ex JMSExceptionImpl) GetLinkedError() error {
5152

5253
}
5354

55+
// GetMessageLength gives the data length that was returned by a Receive (GET) call,
56+
// for example if the message was truncated, or could not be read because the receive
57+
// buffer was too small.
58+
func (ex JMSExceptionImpl) GetMessageLength() int {
59+
60+
return ex.messageLength
61+
62+
}
63+
5464
// Error allows the JMSExceptionImpl struct to be treated as a Golang error,
5565
// while also returning a human readable string representation of the error.
5666
func (ex JMSExceptionImpl) Error() string {
@@ -66,11 +76,17 @@ func (ex JMSExceptionImpl) Error() string {
6676

6777
// CreateJMSException is a helper function for creating a JMSException
6878
func CreateJMSException(reason string, errorCode string, linkedErr error) JMSException {
79+
return CreateJMSExceptionWithExtraParams(reason, errorCode, linkedErr, 0)
80+
}
81+
82+
// CreateJMSException is a helper function for creating a JMSException
83+
func CreateJMSExceptionWithExtraParams(reason string, errorCode string, linkedErr error, messageLength int) JMSException {
6984

7085
ex := JMSExceptionImpl{
71-
reason: reason,
72-
errorCode: errorCode,
73-
linkedErr: linkedErr,
86+
reason: reason,
87+
errorCode: errorCode,
88+
linkedErr: linkedErr,
89+
messageLength: messageLength,
7490
}
7591

7692
return ex

largemessage_test.go

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,20 @@ func TestLargeTextMessage(t *testing.T) {
6262

6363
// The message is still left on the queue since it failed to be received successfully.
6464

65-
// Since the buffer size is configured using the ConnectionFactoy we will
66-
// create a second connection in order to successfully retrieve the message.
67-
cf.ReceiveBufferSize = len(txtOver32kb) + 50
65+
// Use a special attribute of the returned exception to look up what the actual length of the
66+
// message is, so that we can correctly increase the buffer size in order to receive the message.
67+
switch jmsExc := errRcv.(type) {
68+
case jms20subset.JMSExceptionImpl:
69+
realMessageLength := jmsExc.GetMessageLength()
70+
assert.Equal(t, len(txtOver32kb), realMessageLength) // check it matches the original (long) length
71+
72+
// Since the buffer size is configured using the ConnectionFactory we will
73+
// create a second connection in order to successfully retrieve the message.
74+
cf.ReceiveBufferSize = realMessageLength
75+
76+
default:
77+
assert.Fail(t, "Got something other than a JMSExceptionImpl")
78+
}
6879

6980
context2, ctxErr2 := cf.CreateContext()
7081
assert.Nil(t, ctxErr2)
@@ -78,7 +89,7 @@ func TestLargeTextMessage(t *testing.T) {
7889
defer consumer2.Close()
7990
}
8091

81-
rcvMsg2, errRcv2 := consumer2.ReceiveNoWait()
92+
rcvMsg2, errRcv2 := consumer2.ReceiveNoWait() // receive the message using the correct (larger) buffer size
8293
assert.Nil(t, errRcv2)
8394
assert.NotNil(t, rcvMsg2)
8495

@@ -134,9 +145,20 @@ func TestLargeReceiveStringBodyTextMessage(t *testing.T) {
134145

135146
// The message is still left on the queue since it failed to be received successfully.
136147

137-
// Since the buffer size is configured using the ConnectionFactory we will
138-
// create a second connection in order to successfully retrieve the message.
139-
cf.ReceiveBufferSize = len(txtOver32kb) + 50
148+
// Use a special attribute of the returned exception to look up what the actual length of the
149+
// message is, so that we can correctly increase the buffer size in order to receive the message.
150+
switch jmsExc := errRcv.(type) {
151+
case jms20subset.JMSExceptionImpl:
152+
realMessageLength := jmsExc.GetMessageLength()
153+
assert.Equal(t, len(txtOver32kb), realMessageLength) // check it matches the original (long) length
154+
155+
// Since the buffer size is configured using the ConnectionFactory we will
156+
// create a second connection in order to successfully retrieve the message.
157+
cf.ReceiveBufferSize = realMessageLength
158+
159+
default:
160+
assert.Fail(t, "Got something other than a JMSExceptionImpl")
161+
}
140162

141163
context2, ctxErr2 := cf.CreateContext()
142164
assert.Nil(t, ctxErr2)
@@ -200,9 +222,20 @@ func TestLargeBytesMessage(t *testing.T) {
200222

201223
// The message is still left on the queue since it failed to be received successfully.
202224

203-
// Since the buffer size is configured using the ConnectionFactoy we will
204-
// create a second connection in order to successfully retrieve the message.
205-
cf.ReceiveBufferSize = len(bytesOver32kb) + 50
225+
// Use a special attribute of the returned exception to look up what the actual length of the
226+
// message is, so that we can correctly increase the buffer size in order to receive the message.
227+
switch jmsExc := errRcv.(type) {
228+
case jms20subset.JMSExceptionImpl:
229+
realMessageLength := jmsExc.GetMessageLength()
230+
assert.Equal(t, len(txtOver32kb), realMessageLength) // check it matches the original (long) length
231+
232+
// Since the buffer size is configured using the ConnectionFactory we will
233+
// create a second connection in order to successfully retrieve the message.
234+
cf.ReceiveBufferSize = realMessageLength
235+
236+
default:
237+
assert.Fail(t, "Got something other than a JMSExceptionImpl")
238+
}
206239

207240
context2, ctxErr2 := cf.CreateContext()
208241
assert.Nil(t, ctxErr2)
@@ -273,9 +306,20 @@ func TestLargeReceiveBytesBodyBytesMessage(t *testing.T) {
273306

274307
// The message is still left on the queue since it failed to be received successfully.
275308

276-
// Since the buffer size is configured using the ConnectionFactoy we will
277-
// create a second connection in order to successfully retrieve the message.
278-
cf.ReceiveBufferSize = len(bytesOver32kb) + 50
309+
// Use a special attribute of the returned exception to look up what the actual length of the
310+
// message is, so that we can correctly increase the buffer size in order to receive the message.
311+
switch jmsExc := errRcv.(type) {
312+
case jms20subset.JMSExceptionImpl:
313+
realMessageLength := jmsExc.GetMessageLength()
314+
assert.Equal(t, len(txtOver32kb), realMessageLength) // check it matches the original (long) length
315+
316+
// Since the buffer size is configured using the ConnectionFactory we will
317+
// create a second connection in order to successfully retrieve the message.
318+
cf.ReceiveBufferSize = realMessageLength
319+
320+
default:
321+
assert.Fail(t, "Got something other than a JMSExceptionImpl")
322+
}
279323

280324
context2, ctxErr2 := cf.CreateContext()
281325
assert.Nil(t, ctxErr2)
@@ -348,8 +392,21 @@ func TestTruncatedTextMessage(t *testing.T) {
348392
assert.Fail(t, "Got something other than a text message")
349393
}
350394

351-
// Make sure we tidy up in case the previous part of the test failed.
352-
cf.ReceiveBufferSize = len(txtOver32kb) + 50
395+
// Use a special attribute of the returned exception to look up what the actual length of the
396+
// message is, so that we can tidy up the message (read successfully) in the event the previous
397+
// step failed.
398+
switch jmsExc := errRcv.(type) {
399+
case jms20subset.JMSExceptionImpl:
400+
realMessageLength := jmsExc.GetMessageLength()
401+
assert.Equal(t, len(txtOver32kb), realMessageLength) // check it matches the original (long) length
402+
403+
// Since the buffer size is configured using the ConnectionFactory we will
404+
// create a second connection in order to successfully retrieve the message.
405+
cf.ReceiveBufferSize = realMessageLength
406+
407+
default:
408+
assert.Fail(t, "Got something other than a JMSExceptionImpl")
409+
}
353410

354411
context2, ctxErr2 := cf.CreateContext()
355412
assert.Nil(t, ctxErr2)

mqjms/ConsumerImpl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
123123

124124
// In the truncated message case we want to return the warning object as well as the message
125125
// so that the application can tell that some of the data is missing.
126-
jmsErr = CreateJMSExceptionFromMQReturn(err)
126+
jmsErr = CreateJMSExceptionFromMQReturn(err, datalen)
127127
}
128128

129129
// Golden path - typically a message was received without error.
@@ -194,7 +194,7 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
194194

195195
// Parse the details of the error and return it to the caller as
196196
// a JMSException
197-
jmsErr = CreateJMSExceptionFromMQReturn(err)
197+
jmsErr = CreateJMSExceptionFromMQReturn(err, datalen)
198198
}
199199

200200
}
@@ -436,7 +436,7 @@ func (consumer ConsumerImpl) Close() {
436436
return
437437
}
438438

439-
func CreateJMSExceptionFromMQReturn(mqretError error) jms20subset.JMSException {
439+
func CreateJMSExceptionFromMQReturn(mqretError error, datalen int) jms20subset.JMSException {
440440

441441
// Assumes this error code was returned from MQ call.
442442
mqret := mqretError.(*ibmmq.MQReturn)
@@ -447,7 +447,7 @@ func CreateJMSExceptionFromMQReturn(mqretError error) jms20subset.JMSException {
447447
errCode := strconv.Itoa(rcInt)
448448
reason := ibmmq.MQItoString("RC", rcInt)
449449

450-
jmsErr := jms20subset.CreateJMSException(reason, errCode, mqretError)
450+
jmsErr := jms20subset.CreateJMSExceptionWithExtraParams(reason, errCode, mqretError, datalen)
451451

452452
return jmsErr
453453

0 commit comments

Comments
 (0)