Friday, May 3, 2024

Setting Up IBM MQ with JMeter for JMS Testing | Testing IBM MQ with JMeter

Here is a Groovy script demonstrates how to establish a secure connection to an IBM MQ queue and send a text message using JMS (Java Message Service). It configures a JMS connection factory with connection details such as the hostname, port, channel, and queue manager name, and sets up SSL/TLS security using a keystore and trust store. Once the secure connection is established, the script creates a JMS session and message producer to send a text message to a specified MQ queue. This script is commonly used in enterprise environments where secure communication with message queues is necessary for reliable, asynchronous messaging between different systems or applications. By ensuring data confidentiality and integrity through SSL/TLS, the script facilitates secure message exchange between client and server applications.

import com.ibm.msg.client.jms.JmsConnectionFactory
import com.ibm.msg.client.jms.JmsFactoryFactory
import com.ibm.msg.client.wmq.WMQConstants
import javax.jms.Session
import javax.jms.TextMessage
import javax.jms.MessageProducer
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.*
import java.security.KeyStore
import java.io.FileInputStream

// Start of script execution
println("### Script execution started")

// Define connection details
def hostName = "ABC-XYZ88.ind.thefirm.com" // Hostname of the MQ server
def hostPort = 1414 // Port number for the MQ server
def channelName = "IND.QWD.RWDWWE" // MQ channel name
def queueManagerName = "INDSER34" // Queue manager name
def queueName = "IND.RED.REQUEST.FRER" // Queue name
def cipherSuite = "ECDHE_RSA_AES_256_GCM_SHA384" // SSL/TLS cipher suite

// Create a JMS connection factory
def factoryFactoryInstance = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER)
def connectionFactory = factoryFactoryInstance.createConnectionFactory()

try {
    // Set connection properties for the factory
    connectionFactory.setStringProperty(WMQConstants.WMQ_HOST_NAME, hostName)
    connectionFactory.setIntProperty(WMQConstants.WMQ_PORT, hostPort)
    connectionFactory.setStringProperty(WMQConstants.WMQ_CHANNEL, channelName)
    connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT)
    connectionFactory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName)
    
    // Load the keystore from a file
    String keystoreType = "PKCS12" // Keystore format (e.g., PKCS12)
    String keystorePath = "mq-keystore.12" // Path to the keystore file
    String keystorePassword = "keystorePassword" // Keystore password
    
    // Load the keystore
    KeyStore keyStore = KeyStore.getInstance(keystoreType)
    FileInputStream fis = new FileInputStream(keystorePath)
    keyStore.load(fis, keystorePassword.toCharArray())
    
    // Initialize the KeyManagerFactory with the keystore
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
    keyManagerFactory.init(keyStore, keystorePassword.toCharArray())
    
    // Initialize the TrustManagerFactory with the keystore
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
    trustManagerFactory.init(keyStore)
    
    // Initialize the SSLContext with key and trust managers
    SSLContext sslContext = SSLContext.getInstance("TLS")
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new java.security.SecureRandom())
    
    // Get the SSLSocketFactory and set it in the connection factory
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory()
    connectionFactory.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, cipherSuite)
    connectionFactory.setObjectProperty(WMQConstants.WMQ_SSL_SOCKET_FACTORY, sslSocketFactory)
    
    // Create a JMS connection and session
    def jmsConnection = connectionFactory.createConnection()
    def jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)
    
    // Create a destination queue
    def destinationQueue = jmsSession.createQueue(queueName)
    
    // Log that the setup is complete
    println("### MQ setup completed")
    
    // Create a message producer and send a text message
    def messageProducer = jmsSession.createProducer(destinationQueue)
    def textMessage = jmsSession.createTextMessage("Your message content goes here")
    messageProducer.send(textMessage)
    
    // Log that the message was sent successfully
    println("### Message sent successfully")

} catch (Exception e) {
    // Handle any exceptions that occur
    println("Exception: " + e.toString())
    e.printStackTrace()
} finally {
    // Ensure resources are closed
    try {
        messageProducer?.close()
        jmsSession?.close()
        jmsConnection?.close()
    } catch (Exception e) {
        // Handle exceptions during resource closure
        println("Exception during resource closure: " + e.toString())
        e.printStackTrace()
    }
}

Thursday, May 2, 2024

Calling an HTTP Sampler from a JSR223 Sampler in JMeter: A Step-by-Step Guide

 We can call one sampler (HTTP) from another sampler (JSR223) in JMeter. JMeter provides flexibility to customize and control the flow of your test plan using various components, including JSR223 samplers.

To call an HTTP sampler from a JSR223 sampler, you can use the JMeter API within the JSR223 sampler code. Here's an example of how you can achieve this: 1. Add a JSR223 Sampler to your test plan. 2. Choose the appropriate language (e.g., Groovy) for the JSR223 sampler. 3. Write your custom code in the script area of the JSR223 sampler to call the HTTP sampler using the JMeter API. Here's an example: ```groovy script import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; // Get the HTTP sampler by its name def httpSampler = ctx.getCurrentSampler().getThreadContext().getVariables().getObject("HTTPSamplerProxy") // Make sure the HTTP sampler is not null if (httpSampler != null) { // Execute the HTTP sampler def httpResult = httpSampler.sample() // You can access the response code, response message, and other details from the HTTP result int responseCode = httpResult.getResponseCode() String responseMessage = httpResult.getResponseMessage() // Process the HTTP result as needed // ... } ``` In the above example, we obtain the HTTP sampler using the `getObject()` method from the JMeter `ThreadContext` and then execute the HTTP sampler using the `sample()` method. You can access and process the response details according to your requirements. Note: Make sure you have properly configured and added the HTTP sampler to your test plan before using it within the JSR223 sampler. Remember to replace the language-specific code (`groovy` in this example) if you choose a different language for your JSR223 sampler. It's worth noting that calling one sampler from another can affect the concurrency and throughput of your test. Ensure you understand the implications and adjust your test plan accordingly. import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; // Get the HTTP sampler by its name def httpSampler = ctx.getCurrentSampler().getThreadContext().getVariables().getObject("HTTPSamplerProxy") // Make sure the HTTP sampler is not null if (httpSampler != null) { // Execute the HTTP sampler def httpResult = httpSampler.sample() // You can access the response code, response message, and other details from the HTTP result int responseCode = httpResult.getResponseCode() String responseMessage = httpResult.getResponseMessage() // Process the HTTP result as needed // ... }