JMS
SDK for JMS
JMS support was initially contributed by Jason Koch.
Prerequisites
Dependency
For Java, Kotlin, and Scala, the Gatling JMS SDK is included in the io.gatling.highcharts:gatling-charts-highcharts dependency: there’s nothing to add.
For JavaScript and TypeScript, add the @gatling.io/jms package, available since Gatling JavaScript SDK 3.16.101:
npm install @gatling.io/jms@3.16.102
Imports
The Gatling JMS SDK isn’t imported by default. Add the following imports:
import jakarta.jms.*;
import io.gatling.javaapi.jms.*;
import static io.gatling.javaapi.jms.JmsDsl.*;import { jms, jmsJndiConnectionFactory, jmsProperty, simpleCheck } from "@gatling.io/jms";import jakarta.jms.*
import io.gatling.javaapi.jms.*
import io.gatling.javaapi.jms.JmsDsl.*import jakarta.jms._
import io.gatling.jms.Predef._JMS client implementation
Java Message Service (JMS), now known as Jakarta Messaging, is an API.
In order to use it, you must provide your client vendor-specific implementation, for example org.apache.activemq:activemq-broker if you are using ActiveMQ.
For Java, Kotlin, and Scala, add it as a dependency in the same scope as the Gatling dependencies.
For JavaScript and TypeScript, declare it as a JVM dependency in the gatling section of your package.json.
Gatling downloads it when running or packaging your simulations:
{
"gatling": {
"jvmDependencies": {
"artifacts": ["org.apache.activemq:activemq-client:6.4.0"]
}
}
}
Using legacy javax.jms package
Since 3.14.0, Gatling targets the new jakarta.jms package.
If you’re still using a JMS client that requires the legacy javax.jms package and can’t upgrade to a modern version, you have to use a wrapper library such as com.github.marschall:jakarta-jms-adapter.
Wrap your javax.jms.ConnectionFactory with a com.github.marschall.jakartajmsadapter.JakartaConnectionFactory.
This isn’t available in JavaScript and TypeScript, where the JMS client must implement the jakarta.jms package.
JMS protocol
Use the jms object in order to create a JMS protocol.
connectionFactory
The first mandatory step is to configure the ConnectionFactory.
You can either configure one to be retrieved with a JNDI lookup:
JmsJndiConnectionFactoryBuilder jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
// optional, for performing JNDI lookup
.credentials("username", "password")
// optional, custom JNDI property
.property("foo", "bar")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory");
JmsProtocolBuilder jmsProtocol = jms
.connectionFactory(jndiBasedConnectionFactory);const jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
// optional, for performing JNDI lookup
.credentials("username", "password")
// optional, custom JNDI property
.property("foo", "bar")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory");
const jmsProtocol = jms
.connectionFactory(jndiBasedConnectionFactory);val jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
// optional, for performing JNDI lookup
.credentials("username", "password")
// optional, custom JNDI property
.property("foo", "bar")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory")
val jmsProtocol = jms
.connectionFactory(jndiBasedConnectionFactory)val jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
// optional, for performing JNDI lookup
.credentials("username", "password")
// optional, custom JNDI property
.property("foo", "bar")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory")
val jmsProtocol = jms
.connectionFactory(jndiBasedConnectionFactory)or directly instantiate one with your JMS broker’s Java client library, for example:
ConnectionFactory connectionFactory =
new org.apache.activemq.ActiveMQConnectionFactory("url");
JmsProtocolBuilder jmsProtocol = jms
.connectionFactory(connectionFactory);val connectionFactory: ConnectionFactory =
org.apache.activemq.ActiveMQConnectionFactory("url")
val jmsProtocol = jms
.connectionFactory(connectionFactory)val connectionFactory =
new org.apache.activemq.ActiveMQConnectionFactory("url")
val jmsProtocol = jms
.connectionFactory(connectionFactory)Instantiating a ConnectionFactory isn’t available in JavaScript and TypeScript: use a JNDI lookup.
Other options
The following example shows the other protocol options:
jms
.connectionFactory(connectionFactory)
.credentials("username", "password")
// optional, default to non persistent
.useNonPersistentDeliveryMode()
// optional, default to 1
// listener thread count
// some JMS implementation (like IBM MQ) need more than one MessageListener
// to achieve full readout performance
.listenerThreadCount(5)
// optional, default to `matchByMessageId`
// specify how request and response messages should be matched when using `requestReply`
// Use `matchByCorrelationId` for ActiveMQ.
.matchByCorrelationId()
.matchByMessageId()
// use a custom matching strategy
.messageMatcher((io.gatling.javaapi.jms.JmsMessageMatcher) null)
// optional, default to none
.replyTimeout(1000);jms
.connectionFactory(jndiBasedConnectionFactory)
.credentials("username", "password")
// optional, default to non persistent
.useNonPersistentDeliveryMode()
// optional, default to 1
// listener thread count
// some JMS implementation (like IBM MQ) need more than one MessageListener
// to achieve full readout performance
.listenerThreadCount(5)
// optional, default to `matchByMessageId`
// specify how request and response messages should be matched when using `requestReply`
// Use `matchByCorrelationId` for ActiveMQ.
.matchByCorrelationId()
.matchByMessageId()
// use a custom matching strategy
.messageMatcher({
requestMatchId: (message) => message.correlationId(),
responseMatchId: (message) => message.correlationId()
})
// optional, default to none
.replyTimeout(1000);jms
.connectionFactory(connectionFactory)
.credentials("username", "password")
// optional, default to non persistent
.useNonPersistentDeliveryMode()
// optional, default to 1
// listener thread count
// some JMS implementation (like IBM MQ) need more than one MessageListener
// to achieve full readout performance
.listenerThreadCount(5)
// optional, default to `matchByMessageId`
// specify how request and response messages should be matched when using `requestReply`
// Use `matchByCorrelationId` for ActiveMQ.
.matchByCorrelationId()
.matchByMessageId() // use a custom matching strategy
// see io.gatling.javaapi.jms.JmsMessageMatcher
.messageMatcher(null as io.gatling.javaapi.jms.JmsMessageMatcher)
// optional, default to none
.replyTimeout(1000)jms
.connectionFactory(connectionFactory)
.credentials("username", "password")
// optional, default to non persistent
.useNonPersistentDeliveryMode
// optional, default to 1
// listener thread count
// some JMS implementation (like IBM MQ) need more than one MessageListener
// to achieve full readout performance
.listenerThreadCount(5)
// optional, default to `matchByMessageId`
// specify how request and response messages should be matched
// Use `matchByCorrelationId` for ActiveMQ.
.matchByCorrelationId
.matchByMessageId
// use a custom matching strategy
.messageMatcher(null.asInstanceOf[io.gatling.jms.protocol.JmsMessageMatcher])
// optional, default to none
.replyTimeout(1000)JMS request
Use the jms("requestName") method in order to create a JMS request.
Request type
Currently, requestReply and send (fire and forget) requests are supported.
Destination
Define the target destination with queue("queueName") or alternatively with destination(JmsDestination).
Optionally, you can define a reply destination with replyQueue("responseQueue") or replyDestination(JmsDestination). Otherwise, Gatling will use a dynamic queue.
If you do so, you have the possibility of not setting the JMSReplyTo header with noJmsReplyTo.
Additionally, for the reply destination, you can define a JMS selector with selector
If you have the need to measure the time when a message arrive at a message queue different from the replyDestination(JmsDestination),
you can additionally define a trackerDestination(JmsDestination).
Message
textMessagebytesMessagemapMessageobjectMessageforjava.io.Serializablepayloads
In JavaScript and TypeScript, objectMessage sends strings, numbers, and booleans as is, arrays as java.util.ArrayList, and objects as java.util.HashMap.
A string is a Gatling Expression Language string: a lone "#{attribute}" sends the value of the Session attribute as is, which must be serializable.
To always send a string, pass a function.
See below for a few examples:
// with a static text message
jms("name").send().queue("queueName")
.textMessage("message");
// with a Gatling EL string text message
jms("name").send().queue("queueName")
.textMessage("#{message}");
// with a function text message
jms("name").send().queue("queueName")
.textMessage(session -> session.getString("message"));
// with a ElFileBody template text message
jms("name").send().queue("queueName")
.textMessage(ElFileBody("templatePath"));
// with a static bytes message
jms("name").send().queue("queueName")
.bytesMessage(new byte[] { 0, 1, 2 });
// with a RawFileBody bytes message
jms("name").send().queue("queueName")
.bytesMessage(RawFileBody("templatePath"));// with a static text message
jms("name").send().queue("queueName")
.textMessage("message");
// with a Gatling EL string text message
jms("name").send().queue("queueName")
.textMessage("#{message}");
// with a function text message
jms("name").send().queue("queueName")
.textMessage((session) => session.get("message"));
// with a ElFileBody template text message
jms("name").send().queue("queueName")
.textMessage(ElFileBody("templatePath"));
// with a static bytes message
jms("name").send().queue("queueName")
.bytesMessage([0, 1, 2]);
// with a RawFileBody bytes message
jms("name").send().queue("queueName")
.bytesMessage(RawFileBody("templatePath"));// with a static text message
jms("name").send().queue("queueName")
.textMessage("message")
// with a Gatling EL string text message
jms("name").send().queue("queueName")
.textMessage("#{message}")
// with a function text message
jms("name").send().queue("queueName")
.textMessage { session -> session.getString("message") }
// with a ElFileBody template text message
jms("name").send().queue("queueName")
.textMessage(ElFileBody("templatePath"))
// with a static bytes message
jms("name").send().queue("queueName")
.bytesMessage(byteArrayOf(0, 1, 2))
// with a RawFileBody bytes message
jms("name").send().queue("queueName")
.bytesMessage(RawFileBody("templatePath"))// with a static text message
jms("name").send.queue("queueName")
.textMessage("message")
// with a Gatling EL string text message
jms("name").send.queue("queueName")
.textMessage("#{message}")
// with a function text message
jms("name").send.queue("queueName")
.textMessage(session => session("message").as[String])
// with a ElFileBody template text message
jms("name").send.queue("queueName")
.textMessage(ElFileBody("templatePath"))
// with a static bytes message
jms("name").send.queue("queueName")
.bytesMessage(Array[Byte](0, 1, 2))
// with a RawFileBody bytes message
jms("name").send.queue("queueName")
.bytesMessage(RawFileBody("templatePath"))Extra options
jmsTypepropertypriority: the JMS priority of the message, from 0 (lowest) to 9 (highest). A priority outside this range fails the request. Defaults to the JMS default priority, 4.timeToLive: how long the JMS provider keeps the message before it expires it. Defaults to the JMS default, no expiration.correlationId: theJMSCorrelationIDheader of the message, for example a business key from a feeder.
priority and timeToLive are passed to the JMS producer when it sends the message: setting JMSPriority or JMSExpiration with property has no effect, as the JMS provider overwrites them.
Likewise, property("JMSCorrelationID", ...) sets a regular property, not the header: use correlationId instead.
With matchByCorrelationId, Gatling matches the reply with the correlation id you set, and only generates a random one when you don’t.
Two requests in flight with the same correlation id can’t be told apart: the second one fails.
jms("name").send().queue("queueName")
.textMessage("message")
.jmsType("type")
.property("foo", "bar")
// from 0 (lowest) to 9 (highest)
.priority(9)
// in seconds, or a Duration
.timeToLive(30)
.correlationId("#{orderId}");jms("name").send().queue("queueName")
.textMessage("message")
.jmsType("type")
.property("foo", "bar")
// from 0 (lowest) to 9 (highest)
.priority(9)
// in seconds, or a Duration
.timeToLive(30)
.correlationId("#{orderId}");jms("name").send().queue("queueName")
.textMessage("message")
.jmsType("type")
.property("foo", "bar")
// from 0 (lowest) to 9 (highest)
.priority(9)
// in seconds, or a Duration
.timeToLive(30)
.correlationId("#{orderId}")jms("name").send.queue("queueName")
.textMessage("message")
.jmsType("type")
.property("foo", "bar")
// from 0 (lowest) to 9 (highest)
.priority(9)
.timeToLive(30.seconds)
.correlationId("#{orderId}")These options are available since Gatling 3.16.1.
JMS check
Gatling JMS’s support only current supports the following checks:
It also supports jmsProperty for checking JMS properties on reply messages.
jms("name").requestReply().queue("queueName")
.textMessage("message")
// check a String property
.check(jmsProperty("foo").is("bar"))
// check an int property
.check(jmsProperty("foo").ofInt().is(1))
// save a property
.check(jmsProperty("foo").saveAs("fooProperty"));jms("name").requestReply().queue("queueName")
.textMessage("message")
// check a String property
.check(jmsProperty("foo").is("bar"))
// check an int property
.check(jmsProperty("foo").ofInt().is(1))
// save a property
.check(jmsProperty("foo").saveAs("fooProperty"));jms("name").requestReply().queue("queueName")
.textMessage("message")
// check a String property
.check(jmsProperty("foo").shouldBe("bar"))
// check an int property
.check(jmsProperty("foo").ofInt().shouldBe(1))
// save a property
.check(jmsProperty("foo").saveAs("fooProperty"));jms("name").requestReply.queue("queueName")
.textMessage("message")
// check a String property
.check(jmsProperty("foo").is("bar"))
// check an int property
.check(jmsProperty("foo").ofType[Int].is(1))
// save a property
.check(jmsProperty("foo").saveAs("fooProperty"))In addition, there’s simpleCheck. In JavaScript and TypeScript, its function receives a read-only view of the reply, whose text() returns the body of a text message:
public boolean checkBodyTextCorrect(Message m) {
// this assumes that the service just does an "uppercase" transform on the text
if (m instanceof TextMessage) {
try {
return ((TextMessage) m).getText().equals("HELLO FROM GATLING JMS DSL");
} catch (JMSException e) {
throw new RuntimeException(e);
}
} else {
return false;
}
}
JmsRequestReplyActionBuilder request =
jms("name").requestReply().queue("queueName")
.textMessage("message")
.check(simpleCheck(this::checkBodyTextCorrect));const checkBodyTextCorrect = (m: JmsMessage): boolean =>
// this assumes that the service just does an "uppercase" transform on the text
// text() is null if the message isn't a text message
m.text() === "HELLO FROM GATLING JMS DSL";
const request =
jms("name").requestReply().queue("queueName")
.textMessage("message")
.check(simpleCheck(checkBodyTextCorrect));fun checkBodyTextCorrect(m: Message?): Boolean {
// this assumes that the service just does an "uppercase" transform on the text
return if (m is TextMessage) {
m.text == "HELLO FROM GATLING JMS DSL"
} else {
false
}
}
val request = jms("name").requestReply().queue("queueName")
.textMessage("message")
.check(JmsDsl.simpleCheck { m: Message? -> checkBodyTextCorrect(m) })def checkBodyTextCorrect(m: Message) = {
// this assumes that the service just does an "uppercase" transform on the text
m match {
case tm: TextMessage => tm.getText == "HELLO FROM GATLING JMS DSL"
case _ => false
}
}
val request =
jms("name").requestReply.queue("queueName")
.textMessage("message")
.check(simpleCheck(checkBodyTextCorrect))Example
Short example, assuming ActiveMQ on localhost, using a reqReply query, to the queue named “jmstestq”:
public class TestJmsDsl extends Simulation {
// create a ConnectionFactory for ActiveMQ
// search the documentation of your JMS broker
ConnectionFactory connectionFactory =
new org.apache.activemq.ActiveMQConnectionFactory("tcp://localhost:61616");
// alternatively, you can create a ConnectionFactory from a JNDI lookup
JmsJndiConnectionFactoryBuilder jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
.credentials("user", "secret")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory");
JmsProtocolBuilder jmsProtocol = jms
.connectionFactory(connectionFactory)
.usePersistentDeliveryMode();
ScenarioBuilder scn = scenario("JMS DSL test").repeat(1).on(
exec(jms("req reply testing").requestReply()
.queue("jmstestq")
.textMessage("hello from gatling jms dsl")
.property("test_header", "test_value")
.jmsType("test_jms_type")
.check(xpath("//foo")))
);
{
setUp(scn.injectOpen(rampUsersPerSec(10).to(1000).during(60)))
.protocols(jmsProtocol);
}
}export default simulation((setUp) => {
// look up the ConnectionFactory with JNDI
// search the documentation of your JMS broker
const connectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
.credentials("user", "secret")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory");
const jmsProtocol = jms
.connectionFactory(connectionFactory)
.usePersistentDeliveryMode();
const scn = scenario("JMS DSL test").repeat(1).on(
jms("req reply testing").requestReply()
.queue("jmstestq")
.textMessage("hello from gatling jms dsl")
.property("test_header", "test_value")
.jmsType("test_jms_type")
.check(xpath("//foo"))
);
setUp(scn.injectOpen(rampUsersPerSec(10).to(1000).during(60)))
.protocols(jmsProtocol);
});class TestJmsDsl : Simulation() {
// create a ConnectionFactory for ActiveMQ
// search the documentation of your JMS broker
val connectionFactory: ConnectionFactory =
org.apache.activemq.ActiveMQConnectionFactory("tcp://localhost:61616")
// alternatively, you can create a ConnectionFactory from a JNDI lookup
val jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
.credentials("user", "secret")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory")
val jmsProtocol = jms
.connectionFactory(connectionFactory)
.usePersistentDeliveryMode()
val scn = scenario("JMS DSL test").repeat(1).on(
exec(jms("req reply testing").requestReply()
.queue("jmstestq")
.textMessage("hello from gatling jms dsl")
.property("test_header", "test_value")
.jmsType("test_jms_type")
.check(xpath("//foo")))
)
init {
setUp(scn.injectOpen(rampUsersPerSec(10.0).to(1000.0).during(60)))
.protocols(jmsProtocol)
}
}class TestJmsDsl extends Simulation {
// create a ConnectionFactory for ActiveMQ
// search the documentation of your JMS broker
val connectionFactory =
new org.apache.activemq.ActiveMQConnectionFactory("tcp://localhost:61616")
// alternatively, you can create a ConnectionFactory from a JNDI lookup
val jndiBasedConnectionFactory = jmsJndiConnectionFactory
.connectionFactoryName("ConnectionFactory")
.url("tcp://localhost:61616")
.credentials("user", "secret")
.contextFactory("org.apache.activemq.jndi.ActiveMQInitialContextFactory")
val jmsProtocol = jms
.connectionFactory(connectionFactory)
.usePersistentDeliveryMode
val scn = scenario("JMS DSL test").repeat(1) {
exec(jms("req reply testing").requestReply
.queue("jmstestq")
.textMessage("hello from gatling jms dsl")
.property("test_header", "test_value")
.jmsType("test_jms_type")
.check(xpath("//foo")))
}
setUp(scn.inject(rampUsersPerSec(10).to(1000).during(60)))
.protocols(jmsProtocol)
}