|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2019 |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "testing" |
| 17 | +) |
| 18 | + |
| 19 | +/* |
| 20 | + * Demonstrate a fully worked example of a send/receive application including |
| 21 | + * good practice around error handling. |
| 22 | + */ |
| 23 | +func TestSampleSendReceiveWithErrorHandling(t *testing.T) { |
| 24 | + |
| 25 | + // Create a ConnectionFactory using some property files |
| 26 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 27 | + |
| 28 | + if cfErr != nil { |
| 29 | + fmt.Println("Error during CreateConnecionFactory: ") |
| 30 | + fmt.Println(cfErr) |
| 31 | + } |
| 32 | + |
| 33 | + // Creates a connection to the queue manager. |
| 34 | + // We use a "defer" call to make sure that the connection is closed at the end of the method |
| 35 | + context, errCtx := cf.CreateContext() |
| 36 | + if context != nil { |
| 37 | + defer context.Close() |
| 38 | + } |
| 39 | + |
| 40 | + // Check for errors - note that typically the application would terminate or |
| 41 | + // short cut its execution path once an error like this has been detected. |
| 42 | + if errCtx != nil { |
| 43 | + |
| 44 | + fmt.Println("Error during CreateContext: ") |
| 45 | + fmt.Println(errCtx) |
| 46 | + |
| 47 | + } else { |
| 48 | + |
| 49 | + // Create a Queue object that points at an IBM MQ queue |
| 50 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 51 | + |
| 52 | + // Send a message to the queue that contains the specified text string |
| 53 | + errSend := context.CreateProducer().SendString(queue, "My first message") |
| 54 | + |
| 55 | + // Check for errors. In this case we'll allow the application to continue, |
| 56 | + // but there won't be a message to receive. |
| 57 | + if errSend != nil { |
| 58 | + fmt.Println("Error during SendString: ") |
| 59 | + fmt.Println(errSend) |
| 60 | + } |
| 61 | + |
| 62 | + // Create a consumer, using Defer to make sure it gets closed at the end of the method |
| 63 | + consumer, errCons := context.CreateConsumer(queue) |
| 64 | + if consumer != nil { |
| 65 | + defer consumer.Close() |
| 66 | + } |
| 67 | + |
| 68 | + // Consumer was created successfully |
| 69 | + if errCons == nil { |
| 70 | + |
| 71 | + // Receive a message from the queue and return the string from the message body |
| 72 | + rcvBody, rcvErr := consumer.ReceiveStringBodyNoWait() |
| 73 | + |
| 74 | + if rcvErr != nil { |
| 75 | + fmt.Println("Error received:") |
| 76 | + fmt.Println(rcvErr) |
| 77 | + } else { |
| 78 | + |
| 79 | + // Successful creation of the Consumer |
| 80 | + if rcvBody != nil { |
| 81 | + fmt.Println("Received text string: " + *rcvBody) |
| 82 | + } else { |
| 83 | + fmt.Println("No message received") |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + } else { |
| 89 | + fmt.Println("Error during CreateConsumer: ") |
| 90 | + fmt.Println(errCons) |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * Demonstrate a send/receive application with limited error handling - this code |
| 99 | + * is used as part of the Readme for this repo in order to illustrate the key |
| 100 | + * functional steps using this JMS style library. |
| 101 | + */ |
| 102 | +func TestSampleSendReceiveWithLimitedErrorHandling(t *testing.T) { |
| 103 | + |
| 104 | + // Create a ConnectionFactory using some external property files |
| 105 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 106 | + if cfErr != nil { |
| 107 | + fmt.Println(cfErr) |
| 108 | + } |
| 109 | + |
| 110 | + // Creates a connection to the queue manager. |
| 111 | + // We use a "defer" call to make sure that the connection is closed at the end of the method |
| 112 | + context, ctxErr := cf.CreateContext() |
| 113 | + if ctxErr != nil { |
| 114 | + fmt.Println(ctxErr) |
| 115 | + } |
| 116 | + if context != nil { |
| 117 | + defer context.Close() |
| 118 | + } |
| 119 | + |
| 120 | + // Create a Queue object that points at an IBM MQ queue |
| 121 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 122 | + |
| 123 | + // Send a message to the queue that contains the specified text string |
| 124 | + context.CreateProducer().SendString(queue, "My first message") |
| 125 | + |
| 126 | + // Create a consumer, using Defer to make sure it gets closed at the end of the method |
| 127 | + consumer, conErr := context.CreateConsumer(queue) |
| 128 | + if conErr != nil { |
| 129 | + fmt.Println(conErr) |
| 130 | + } |
| 131 | + if consumer != nil { |
| 132 | + defer consumer.Close() |
| 133 | + } |
| 134 | + |
| 135 | + // Receive a message from the queue and return the string from the message body |
| 136 | + rcvBody, rcvErr := consumer.ReceiveStringBodyNoWait() |
| 137 | + |
| 138 | + if rcvErr != nil { |
| 139 | + fmt.Println("Error received:") |
| 140 | + fmt.Println(rcvErr) |
| 141 | + } |
| 142 | + |
| 143 | + if rcvBody != nil { |
| 144 | + fmt.Println("Received text string: " + *rcvBody) |
| 145 | + } else { |
| 146 | + fmt.Println("No message received") |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +/* |
| 152 | + * Test for receiving a message when there is no message available. |
| 153 | + */ |
| 154 | +func TestReceiveNoWaitWithoutMessage(t *testing.T) { |
| 155 | + |
| 156 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 157 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 158 | + if cfErr != nil { |
| 159 | + fmt.Println(cfErr) |
| 160 | + } |
| 161 | + |
| 162 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 163 | + // at the end of the function (if it was created successfully) |
| 164 | + context, ctxErr := cf.CreateContext() |
| 165 | + if ctxErr != nil { |
| 166 | + fmt.Println(ctxErr) |
| 167 | + } |
| 168 | + if context != nil { |
| 169 | + defer context.Close() |
| 170 | + } |
| 171 | + |
| 172 | + // Equivalent to a JNDI lookup or other declarative definition |
| 173 | + queue := context.CreateQueue("DEV.QUEUE.2") |
| 174 | + |
| 175 | + // Try to receive a message when there isn't one available. |
| 176 | + consumer, conErr := context.CreateConsumer(queue) |
| 177 | + if conErr != nil { |
| 178 | + fmt.Println(conErr) |
| 179 | + } |
| 180 | + if consumer != nil { |
| 181 | + defer consumer.Close() |
| 182 | + } |
| 183 | + |
| 184 | + rcvBody, rcvErr := consumer.ReceiveStringBodyNoWait() |
| 185 | + |
| 186 | + if rcvErr != nil { |
| 187 | + |
| 188 | + fmt.Println("Error received:") |
| 189 | + fmt.Println(rcvErr) |
| 190 | + |
| 191 | + } else { |
| 192 | + |
| 193 | + // No error on the receive, so we can check for the message. |
| 194 | + assert.Nil(t, rcvBody) |
| 195 | + if rcvBody != nil { |
| 196 | + fmt.Println("Received unexpected message: " + *rcvBody) |
| 197 | + } |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments