|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.ibm.mq.integration.tests; |
| 7 | + |
| 8 | +import com.ibm.mq.MQException; |
| 9 | +import com.ibm.mq.MQQueueManager; |
| 10 | +import com.ibm.mq.constants.CMQC; |
| 11 | +import com.ibm.mq.constants.CMQCFC; |
| 12 | +import com.ibm.mq.headers.pcf.PCFException; |
| 13 | +import com.ibm.mq.headers.pcf.PCFMessage; |
| 14 | +import com.ibm.mq.headers.pcf.PCFMessageAgent; |
| 15 | +import com.ibm.msg.client.jakarta.jms.JmsConnectionFactory; |
| 16 | +import com.ibm.msg.client.jakarta.jms.JmsFactoryFactory; |
| 17 | +import com.ibm.msg.client.jakarta.wmq.WMQConstants; |
| 18 | +import io.opentelemetry.ibm.mq.config.QueueManager; |
| 19 | +import io.opentelemetry.ibm.mq.util.WmqUtil; |
| 20 | +import jakarta.jms.Destination; |
| 21 | +import jakarta.jms.JMSConsumer; |
| 22 | +import jakarta.jms.JMSContext; |
| 23 | +import jakarta.jms.JMSException; |
| 24 | +import jakarta.jms.JMSProducer; |
| 25 | +import jakarta.jms.JMSRuntimeException; |
| 26 | +import jakarta.jms.TextMessage; |
| 27 | + |
| 28 | +/** |
| 29 | + * This code was adapted from https://github.com/ibm-messaging/mq-dev-samples/. |
| 30 | + * |
| 31 | + * <p>A minimal and simple application for Point-to-point messaging. |
| 32 | + * |
| 33 | + * <p>Application makes use of fixed literals, any customisations will require re-compilation of |
| 34 | + * this source file. Application assumes that the named queue is empty prior to a run. |
| 35 | + * |
| 36 | + * <p>Notes: |
| 37 | + * |
| 38 | + * <p>API type: Jakarta API (JMS v3.0, simplified domain) |
| 39 | + * |
| 40 | + * <p>Messaging domain: Point-to-point |
| 41 | + * |
| 42 | + * <p>Provider type: IBM MQ |
| 43 | + * |
| 44 | + * <p>Connection mode: Client connection |
| 45 | + * |
| 46 | + * <p>JNDI in use: No |
| 47 | + */ |
| 48 | +public final class JakartaPutGet { |
| 49 | + |
| 50 | + private JakartaPutGet(){ |
| 51 | + } |
| 52 | + |
| 53 | + public static void createQueue(QueueManager manager, String name, int maxDepth) { |
| 54 | + MQQueueManager ibmQueueManager = WmqUtil.connectToQueueManager(manager); |
| 55 | + PCFMessageAgent agent = WmqUtil.initPcfMessageAgent(manager, ibmQueueManager); |
| 56 | + PCFMessage request = new PCFMessage(CMQCFC.MQCMD_CREATE_Q); |
| 57 | + request.addParameter(com.ibm.mq.constants.CMQC.MQCA_Q_NAME, name); |
| 58 | + request.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL); |
| 59 | + |
| 60 | + request.addParameter(CMQC.MQIA_MAX_Q_DEPTH, maxDepth); |
| 61 | + // these parameters are indicated in percentage of max depth. |
| 62 | + request.addParameter(CMQC.MQIA_Q_DEPTH_HIGH_LIMIT, 75); |
| 63 | + request.addParameter(CMQC.MQIA_Q_DEPTH_LOW_LIMIT, 20); |
| 64 | + request.addParameter(CMQC.MQIA_Q_DEPTH_HIGH_EVENT, CMQCFC.MQEVR_ENABLED); |
| 65 | + request.addParameter(CMQC.MQIA_Q_DEPTH_LOW_EVENT, CMQCFC.MQEVR_ENABLED); |
| 66 | + request.addParameter(CMQC.MQIA_Q_DEPTH_MAX_EVENT, CMQCFC.MQEVR_ENABLED); |
| 67 | + try { |
| 68 | + agent.send(request); |
| 69 | + } catch (PCFException e) { |
| 70 | + if (e.reasonCode == CMQCFC.MQRCCF_OBJECT_ALREADY_EXISTS) { |
| 71 | + return; |
| 72 | + } |
| 73 | + throw new RuntimeException(e); |
| 74 | + } catch (Exception e) { |
| 75 | + throw new RuntimeException(e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param manager Queue manager configuration |
| 81 | + * @param queueName Queue that the application uses to put and get messages to and from |
| 82 | + * @param numberOfMessages Number of messages to send |
| 83 | + * @param sleepIntervalMs Sleep interval in ms |
| 84 | + */ |
| 85 | + public static void runPutGet( |
| 86 | + QueueManager manager, String queueName, int numberOfMessages, int sleepIntervalMs) { |
| 87 | + |
| 88 | + createQueue(manager, queueName, 100000); |
| 89 | + JMSContext context = null; |
| 90 | + JMSContext senderContext = null; |
| 91 | + try { |
| 92 | + // Create a connection factory |
| 93 | + JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.JAKARTA_WMQ_PROVIDER); |
| 94 | + JmsConnectionFactory cf = ff.createConnectionFactory(); |
| 95 | + |
| 96 | + // Set the properties |
| 97 | + cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, manager.getHost()); |
| 98 | + cf.setIntProperty(WMQConstants.WMQ_PORT, manager.getPort()); |
| 99 | + cf.setStringProperty(WMQConstants.WMQ_CHANNEL, manager.getChannelName()); |
| 100 | + cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); |
| 101 | + cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, manager.getName()); |
| 102 | + cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JakartaPutGet (Jakarta)"); |
| 103 | + cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true); |
| 104 | + cf.setStringProperty(WMQConstants.USERID, manager.getUsername()); |
| 105 | + cf.setStringProperty(WMQConstants.PASSWORD, manager.getPassword()); |
| 106 | + // cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "*TLS12ORHIGHER"); |
| 107 | + // cf.setIntProperty(MQConstants.CERTIFICATE_VALIDATION_POLICY, |
| 108 | + // MQConstants.MQ_CERT_VAL_POLICY_NONE); |
| 109 | + |
| 110 | + // Create Jakarta objects |
| 111 | + context = cf.createContext(); |
| 112 | + Destination destination = context.createQueue("queue:///" + queueName); |
| 113 | + |
| 114 | + JMSConsumer consumer = context.createConsumer(destination); |
| 115 | + consumer.setMessageListener(message -> {}); |
| 116 | + |
| 117 | + senderContext = cf.createContext(); |
| 118 | + Destination senderDestination = senderContext.createQueue("queue:///" + queueName); |
| 119 | + |
| 120 | + for (int i = 0; i < numberOfMessages; i++) { |
| 121 | + long uniqueNumber = System.currentTimeMillis() % 1000; |
| 122 | + TextMessage message = |
| 123 | + senderContext.createTextMessage("Your lucky number today is " + uniqueNumber); |
| 124 | + message.setIntProperty(WMQConstants.JMS_IBM_CHARACTER_SET, 37); |
| 125 | + JMSProducer producer = senderContext.createProducer(); |
| 126 | + producer.send(senderDestination, message); |
| 127 | + |
| 128 | + Thread.sleep(sleepIntervalMs); |
| 129 | + } |
| 130 | + |
| 131 | + } catch (JMSException | InterruptedException jmsex) { |
| 132 | + throw new RuntimeException(jmsex); |
| 133 | + } finally { |
| 134 | + if (context != null) { |
| 135 | + context.close(); |
| 136 | + } |
| 137 | + if (senderContext != null) { |
| 138 | + senderContext.close(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Send a number of messages to the queue. |
| 145 | + * |
| 146 | + * @param manager Queue manager configuration |
| 147 | + * @param queueName Queue that the application uses to put and get messages to and from |
| 148 | + * @param numberOfMessages Number of messages to send |
| 149 | + */ |
| 150 | + public static void sendMessages(QueueManager manager, String queueName, int numberOfMessages) { |
| 151 | + |
| 152 | + createQueue(manager, queueName, 1000); |
| 153 | + JMSContext context = null; |
| 154 | + try { |
| 155 | + // Create a connection factory |
| 156 | + JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.JAKARTA_WMQ_PROVIDER); |
| 157 | + JmsConnectionFactory cf = ff.createConnectionFactory(); |
| 158 | + |
| 159 | + // Set the properties |
| 160 | + cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, manager.getHost()); |
| 161 | + cf.setIntProperty(WMQConstants.WMQ_PORT, manager.getPort()); |
| 162 | + cf.setStringProperty(WMQConstants.WMQ_CHANNEL, manager.getChannelName()); |
| 163 | + cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); |
| 164 | + cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, manager.getName()); |
| 165 | + cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "Message Sender"); |
| 166 | + cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true); |
| 167 | + cf.setStringProperty(WMQConstants.USERID, manager.getUsername()); |
| 168 | + cf.setStringProperty(WMQConstants.PASSWORD, manager.getPassword()); |
| 169 | + // cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "*TLS12ORHIGHER"); |
| 170 | + // cf.setIntProperty(MQConstants.CERTIFICATE_VALIDATION_POLICY, |
| 171 | + // MQConstants.MQ_CERT_VAL_POLICY_NONE); |
| 172 | + |
| 173 | + // Create Jakarta objects |
| 174 | + context = cf.createContext(); |
| 175 | + Destination destination = context.createQueue("queue:///" + queueName); |
| 176 | + |
| 177 | + for (int i = 0; i < numberOfMessages; i++) { |
| 178 | + long uniqueNumber = System.currentTimeMillis() % 1000; |
| 179 | + TextMessage message = |
| 180 | + context.createTextMessage("Your lucky number today is " + uniqueNumber); |
| 181 | + message.setIntProperty(WMQConstants.JMS_IBM_CHARACTER_SET, 37); |
| 182 | + JMSProducer producer = context.createProducer(); |
| 183 | + producer.send(destination, message); |
| 184 | + } |
| 185 | + |
| 186 | + } catch (JMSException e) { |
| 187 | + throw new RuntimeException(e); |
| 188 | + } catch (JMSRuntimeException e) { |
| 189 | + if (e.getCause() instanceof MQException) { |
| 190 | + MQException mqe = (MQException) e.getCause(); |
| 191 | + if (mqe.getReason() == 2053) { // queue is full |
| 192 | + return; |
| 193 | + } |
| 194 | + } |
| 195 | + throw new RuntimeException(e); |
| 196 | + } finally { |
| 197 | + if (context != null) { |
| 198 | + context.close(); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Reads all the messages of the queue. |
| 205 | + * |
| 206 | + * @param manager Queue manager configuration |
| 207 | + * @param queueName Queue that the application uses to put and get messages to and from |
| 208 | + */ |
| 209 | + public static void readMessages(QueueManager manager, String queueName) { |
| 210 | + JMSContext context = null; |
| 211 | + try { |
| 212 | + // Create a connection factory |
| 213 | + JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.JAKARTA_WMQ_PROVIDER); |
| 214 | + JmsConnectionFactory cf = ff.createConnectionFactory(); |
| 215 | + |
| 216 | + // Set the properties |
| 217 | + cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, manager.getHost()); |
| 218 | + cf.setIntProperty(WMQConstants.WMQ_PORT, manager.getPort()); |
| 219 | + cf.setStringProperty(WMQConstants.WMQ_CHANNEL, manager.getChannelName()); |
| 220 | + cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); |
| 221 | + cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, manager.getName()); |
| 222 | + cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "Message Receiver"); |
| 223 | + cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true); |
| 224 | + cf.setStringProperty(WMQConstants.USERID, manager.getUsername()); |
| 225 | + cf.setStringProperty(WMQConstants.PASSWORD, manager.getPassword()); |
| 226 | + // cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "*TLS12ORHIGHER"); |
| 227 | + // cf.setIntProperty(MQConstants.CERTIFICATE_VALIDATION_POLICY, |
| 228 | + // MQConstants.MQ_CERT_VAL_POLICY_NONE); |
| 229 | + |
| 230 | + // Create Jakarta objects |
| 231 | + context = cf.createContext(); |
| 232 | + Destination destination = context.createQueue("queue:///" + queueName); |
| 233 | + |
| 234 | + JMSConsumer consumer = context.createConsumer(destination); // autoclosable |
| 235 | + while (consumer.receiveBody(String.class, 100) != null) {} |
| 236 | + |
| 237 | + } catch (JMSException e) { |
| 238 | + throw new RuntimeException(e); |
| 239 | + } catch (JMSRuntimeException e) { |
| 240 | + if (e.getCause() instanceof MQException) { |
| 241 | + MQException mqe = (MQException) e.getCause(); |
| 242 | + if (mqe.getReason() == CMQC.MQRC_NO_MSG_AVAILABLE) { // out of messages, we read them all. |
| 243 | + return; |
| 244 | + } |
| 245 | + } |
| 246 | + throw new RuntimeException(e); |
| 247 | + } finally { |
| 248 | + if (context != null) { |
| 249 | + context.close(); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + public static void tryLoginWithBadPassword(QueueManager manager) { |
| 255 | + |
| 256 | + JMSContext context = null; |
| 257 | + try { |
| 258 | + // Create a connection factory |
| 259 | + JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.JAKARTA_WMQ_PROVIDER); |
| 260 | + JmsConnectionFactory cf = ff.createConnectionFactory(); |
| 261 | + |
| 262 | + // Set the properties |
| 263 | + cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, manager.getHost()); |
| 264 | + cf.setIntProperty(WMQConstants.WMQ_PORT, manager.getPort()); |
| 265 | + cf.setStringProperty(WMQConstants.WMQ_CHANNEL, manager.getChannelName()); |
| 266 | + cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); |
| 267 | + cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, manager.getName()); |
| 268 | + cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "Bad Password"); |
| 269 | + cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true); |
| 270 | + cf.setStringProperty(WMQConstants.USERID, manager.getUsername()); |
| 271 | + cf.setStringProperty(WMQConstants.PASSWORD, "badpassword"); |
| 272 | + // cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "*TLS12ORHIGHER"); |
| 273 | + // cf.setIntProperty(MQConstants.CERTIFICATE_VALIDATION_POLICY, |
| 274 | + // MQConstants.MQ_CERT_VAL_POLICY_NONE); |
| 275 | + |
| 276 | + // Create Jakarta objects |
| 277 | + context = cf.createContext(); |
| 278 | + } catch (JMSException e) { |
| 279 | + throw new RuntimeException(e); |
| 280 | + } catch (JMSRuntimeException e) { |
| 281 | + if (e.getCause() instanceof MQException) { |
| 282 | + MQException mqe = (MQException) e.getCause(); |
| 283 | + if (mqe.getReason() == 2035) { // bad password |
| 284 | + return; |
| 285 | + } |
| 286 | + } |
| 287 | + throw new RuntimeException(e); |
| 288 | + } finally { |
| 289 | + if (context != null) { |
| 290 | + context.close(); |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | +} |
0 commit comments