@@ -89,3 +89,74 @@ func TestGetByMsgID(t *testing.T) {
8989 }
9090
9191}
92+
93+ /*
94+ * Test receiving a specific message from a queue using its JMSMessageID
95+ */
96+ func TestGetByMsgIDWithPrefix (t * testing.T ) {
97+
98+ // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
99+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
100+ assert .Nil (t , cfErr )
101+
102+ // Creates a connection to the queue manager, using defer to close it automatically
103+ // at the end of the function (if it was created successfully)
104+ context , ctxErr := cf .CreateContext ()
105+ assert .Nil (t , ctxErr )
106+ if context != nil {
107+ defer context .Close ()
108+ }
109+
110+ // First, check the queue is empty
111+ queue := context .CreateQueue ("DEV.QUEUE.1" )
112+ consumer , conErr := context .CreateConsumer (queue )
113+ assert .Nil (t , conErr )
114+ if consumer != nil {
115+ defer consumer .Close ()
116+ }
117+ reqMsgTest , err := consumer .ReceiveNoWait ()
118+ assert .Nil (t , err )
119+ assert .Nil (t , reqMsgTest )
120+
121+ // Put a couple of messages before the one we're aiming to get back
122+ producer := context .CreateProducer ().SetTimeToLive (10000 )
123+ producer .SendString (queue , "One-prefix" )
124+ producer .SendString (queue , "Two-prefix" )
125+ producer .SendString (queue , "Three-prefix" )
126+
127+ myMsgFourStr := "Four-prefix"
128+ sentMsg := context .CreateTextMessageWithString (myMsgFourStr )
129+ err = producer .Send (queue , sentMsg )
130+ assert .Nil (t , err )
131+ sentMsgID := sentMsg .GetJMSMessageID ()
132+
133+ // Put a couple of messages after the one we're aiming to get back
134+ producer .SendString (queue , "Five-prefix" )
135+
136+ // Create the consumer to read by CorrelID
137+ correlIDConsumer , correlErr := context .CreateConsumerWithSelector (queue , "JMSMessageID = 'ID:" + sentMsgID + "'" )
138+ assert .Nil (t , correlErr )
139+ gotCorrelMsg , correlGetErr := correlIDConsumer .ReceiveNoWait ()
140+
141+ // Clean up the remaining messages from the queue before we start checking if
142+ // we got the right one back.
143+ var cleanupMsg jms20subset.Message
144+ for ok := true ; ok ; ok = (cleanupMsg != nil ) {
145+ cleanupMsg , err = consumer .ReceiveNoWait ()
146+ }
147+
148+ // Now do the comparisons
149+ assert .Nil (t , correlGetErr )
150+ assert .NotNil (t , gotCorrelMsg )
151+ gotMsgID := gotCorrelMsg .GetJMSMessageID ()
152+ assert .Equal (t , sentMsgID , gotMsgID )
153+
154+ switch msg := gotCorrelMsg .(type ) {
155+ case jms20subset.TextMessage :
156+ assert .Equal (t , myMsgFourStr , * msg .GetText ())
157+ default :
158+ fmt .Println (reflect .TypeOf (gotCorrelMsg ))
159+ assert .Fail (t , "Got something other than a text message" )
160+ }
161+
162+ }
0 commit comments