Skip to content

Commit a7141af

Browse files
committed
Support for message int properties - #39
1 parent 7dbcecf commit a7141af

File tree

3 files changed

+189
-4
lines changed

3 files changed

+189
-4
lines changed

jms20subset/Message.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ type Message interface {
5858
// Returns nil if the named property is not set.
5959
GetStringProperty(name string) (*string, JMSException)
6060

61+
// SetIntProperty enables an application to set a int-type message property.
62+
SetIntProperty(name string, value int) JMSException
63+
64+
// GetIntProperty returns the int value of a named message property.
65+
// Returns 0 if the named property is not set.
66+
GetIntProperty(name string) (int, JMSException)
67+
6168
// PropertyExists returns true if the named message property exists on this message.
6269
PropertyExists(name string) (bool, JMSException)
6370

messageproperties_test.go

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ import (
2727
* JMS: SetStringProperty, GetStringProperty,
2828
* https://github.com/eclipse-ee4j/messaging/blob/master/api/src/main/java/jakarta/jms/Message.java#L1119
2929
*
30+
* Double
31+
* Boolean
32+
*
33+
* BytesMessage
34+
*
3035
*/
3136

3237
/*
@@ -337,19 +342,19 @@ func TestPropertyClearProperties(t *testing.T) {
337342
assert.Equal(t, 0, len(allPropNames))
338343

339344
propName2 := "myPropertyTwo"
340-
propValue2 := "myValueTwo"
345+
propValue2 := 246811
341346

342347
// Set multiple properties
343348
retErr = txtMsg.SetStringProperty(propName, &propValue)
344349
assert.Nil(t, retErr)
345350
gotPropValue, propErr = txtMsg.GetStringProperty(propName)
346351
assert.Nil(t, propErr)
347352
assert.Equal(t, propValue, *gotPropValue)
348-
retErr = txtMsg.SetStringProperty(propName2, &propValue2)
353+
retErr = txtMsg.SetIntProperty(propName2, propValue2)
349354
assert.Nil(t, retErr)
350-
gotPropValue, propErr = txtMsg.GetStringProperty(propName2)
355+
gotPropValue2, propErr := txtMsg.GetIntProperty(propName2)
351356
assert.Nil(t, propErr)
352-
assert.Equal(t, propValue2, *gotPropValue)
357+
assert.Equal(t, propValue2, gotPropValue2)
353358
propExists, propErr = txtMsg.PropertyExists(propName2)
354359
assert.Nil(t, propErr)
355360
assert.True(t, propExists) // now exists
@@ -593,3 +598,117 @@ func TestStringPropertyTextMessageEmptyBody(t *testing.T) {
593598
assert.Equal(t, propBValue, *gotPropValue)
594599

595600
}
601+
602+
/*
603+
* Test the creation of a text message with an int property.
604+
*/
605+
func TestIntProperty(t *testing.T) {
606+
607+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
608+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
609+
assert.Nil(t, cfErr)
610+
611+
// Creates a connection to the queue manager, using defer to close it automatically
612+
// at the end of the function (if it was created successfully)
613+
context, ctxErr := cf.CreateContext()
614+
assert.Nil(t, ctxErr)
615+
if context != nil {
616+
defer context.Close()
617+
}
618+
619+
// Create a TextMessage and check that we can populate it
620+
msgBody := "IntPropertyRequestMsg"
621+
txtMsg := context.CreateTextMessage()
622+
txtMsg.SetText(msgBody)
623+
624+
propName := "myProperty"
625+
propValue := 6
626+
627+
// Test the empty value before the property is set.
628+
gotPropValue, propErr := txtMsg.GetIntProperty(propName)
629+
assert.Nil(t, propErr)
630+
assert.Equal(t, 0, gotPropValue)
631+
propExists, propErr := txtMsg.PropertyExists(propName)
632+
assert.Nil(t, propErr)
633+
assert.False(t, propExists)
634+
635+
// Test the ability to set properties before the message is sent.
636+
retErr := txtMsg.SetIntProperty(propName, propValue)
637+
assert.Nil(t, retErr)
638+
gotPropValue, propErr = txtMsg.GetIntProperty(propName)
639+
assert.Nil(t, propErr)
640+
assert.Equal(t, propValue, gotPropValue)
641+
assert.Equal(t, msgBody, *txtMsg.GetText())
642+
propExists, propErr = txtMsg.PropertyExists(propName)
643+
assert.Nil(t, propErr)
644+
assert.True(t, propExists) // now exists
645+
646+
propName2 := "myProperty2"
647+
propValue2 := 246810
648+
retErr = txtMsg.SetIntProperty(propName2, propValue2)
649+
assert.Nil(t, retErr)
650+
gotPropValue, propErr = txtMsg.GetIntProperty(propName2)
651+
assert.Nil(t, propErr)
652+
assert.Equal(t, propValue2, gotPropValue)
653+
654+
// Set a property then try to "unset" it by setting to 0
655+
unsetPropName := "mySendThenRemovedString"
656+
unsetPropValue := 12345
657+
retErr = txtMsg.SetIntProperty(unsetPropName, unsetPropValue)
658+
assert.Nil(t, retErr)
659+
gotPropValue, propErr = txtMsg.GetIntProperty(unsetPropName)
660+
assert.Nil(t, propErr)
661+
assert.Equal(t, unsetPropValue, gotPropValue)
662+
retErr = txtMsg.SetIntProperty(unsetPropName, 0)
663+
assert.Nil(t, retErr)
664+
gotPropValue, propErr = txtMsg.GetIntProperty(unsetPropName)
665+
assert.Nil(t, propErr)
666+
assert.Equal(t, 0, gotPropValue)
667+
668+
// Set up objects for send/receive
669+
queue := context.CreateQueue("DEV.QUEUE.1")
670+
consumer, errCons := context.CreateConsumer(queue)
671+
if consumer != nil {
672+
defer consumer.Close()
673+
}
674+
assert.Nil(t, errCons)
675+
676+
// Now send the message and get it back again, to check that it roundtripped.
677+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, txtMsg)
678+
assert.Nil(t, errSend)
679+
680+
rcvMsg, errRvc := consumer.ReceiveNoWait()
681+
assert.Nil(t, errRvc)
682+
assert.NotNil(t, rcvMsg)
683+
684+
switch msg := rcvMsg.(type) {
685+
case jms20subset.TextMessage:
686+
assert.Equal(t, msgBody, *msg.GetText())
687+
default:
688+
assert.Fail(t, "Got something other than a text message")
689+
}
690+
691+
// Check property is available on received message.
692+
gotPropValue, propErr = rcvMsg.GetIntProperty(propName)
693+
assert.Nil(t, propErr)
694+
assert.Equal(t, propValue, gotPropValue)
695+
propExists, propErr = txtMsg.PropertyExists(propName)
696+
assert.Nil(t, propErr)
697+
assert.True(t, propExists) // now exists
698+
699+
gotPropValue, propErr = rcvMsg.GetIntProperty(propName2)
700+
assert.Nil(t, propErr)
701+
assert.Equal(t, propValue2, gotPropValue)
702+
703+
// Properties that are not set should return nil
704+
gotPropValue, propErr = rcvMsg.GetIntProperty("nonExistentProperty")
705+
assert.Nil(t, propErr)
706+
assert.Equal(t, 0, gotPropValue)
707+
gotPropValue, propErr = rcvMsg.GetIntProperty(unsetPropName)
708+
assert.Nil(t, propErr)
709+
assert.Equal(t, 0, gotPropValue)
710+
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
711+
assert.Nil(t, propErr)
712+
assert.True(t, propExists) // exists, even though it is set to zero
713+
714+
}

mqjms/MessageImpl.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,65 @@ func (msg *MessageImpl) GetStringProperty(name string) (*string, jms20subset.JMS
346346
return &valueStr, retErr
347347
}
348348

349+
// SetIntProperty enables an application to set a int-type message property.
350+
func (msg *MessageImpl) SetIntProperty(name string, value int) jms20subset.JMSException {
351+
var retErr jms20subset.JMSException
352+
353+
var linkedErr error
354+
355+
smpo := ibmmq.NewMQSMPO()
356+
pd := ibmmq.NewMQPD()
357+
358+
linkedErr = msg.msgHandle.SetMP(smpo, name, pd, value)
359+
360+
if linkedErr != nil {
361+
rcInt := int(linkedErr.(*ibmmq.MQReturn).MQRC)
362+
errCode := strconv.Itoa(rcInt)
363+
reason := ibmmq.MQItoString("RC", rcInt)
364+
retErr = jms20subset.CreateJMSException(reason, errCode, linkedErr)
365+
}
366+
367+
return retErr
368+
}
369+
370+
// GetIntProperty returns the int value of a named message property.
371+
// Returns 0 if the named property is not set.
372+
func (msg *MessageImpl) GetIntProperty(name string) (int, jms20subset.JMSException) {
373+
374+
var valueRet int
375+
var retErr jms20subset.JMSException
376+
377+
impo := ibmmq.NewMQIMPO()
378+
pd := ibmmq.NewMQPD()
379+
380+
_, value, err := msg.msgHandle.InqMP(impo, pd, name)
381+
382+
if err == nil {
383+
switch valueTyped := value.(type) {
384+
case int64:
385+
valueRet = int(valueTyped)
386+
default:
387+
// TODO - other conversions
388+
//fmt.Println("Other type", value, reflect.TypeOf(value))
389+
}
390+
} else {
391+
392+
mqret := err.(*ibmmq.MQReturn)
393+
if mqret.MQRC == ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
394+
// This indicates that the requested property does not exist.
395+
// valueRet will remain with its default value of nil
396+
return 0, nil
397+
} else {
398+
// Err was not nil
399+
rcInt := int(mqret.MQRC)
400+
errCode := strconv.Itoa(rcInt)
401+
reason := ibmmq.MQItoString("RC", rcInt)
402+
retErr = jms20subset.CreateJMSException(reason, errCode, mqret)
403+
}
404+
}
405+
return valueRet, retErr
406+
}
407+
349408
// PropertyExists returns true if the named message property exists on this message.
350409
func (msg *MessageImpl) PropertyExists(name string) (bool, jms20subset.JMSException) {
351410

0 commit comments

Comments
 (0)