Skip to main content

Triggers

The ballerinax/java.jms connector supports event-driven message consumption through a jms:Listener. When messages arrive on a configured queue or topic, the listener dispatches them to your service's onMessage callback automatically; no polling loop required.

Three components work together:

ComponentRole
jms:ListenerConnects to the JMS broker and listens for incoming messages on queues or topics.
jms:ServiceDefines the onMessage callback invoked for each received message.
jms:CallerProvides message acknowledgement and transaction control within service callbacks.
jms:MessageThe message payload passed to the onMessage callback.

For action-based record operations, see the Action Reference.


Listener

The java.jms:Listener establishes the connection and manages event subscriptions.

Configuration

The listener supports the following connection strategies:

Config TypeDescription
ConnectionConfigurationConfigures the JMS broker connection for the listener.

ConnectionConfiguration fields:

FieldTypeDefaultDescription
initialContextFactorystringRequiredJNDI initial context factory class (e.g., org.apache.activemq.jndi.ActiveMQInitialContextFactory).
providerUrlstringRequiredJMS provider URL (e.g., tcp://localhost:61616).
connectionFactoryNamestring"ConnectionFactory"JNDI name of the connection factory.
usernamestring()Username for broker authentication.
passwordstring()Password for broker authentication.
propertiesmap<string>{}Additional JNDI properties.

Initializing the listener

Basic listener with ActiveMQ:

import ballerinax/java.jms;

configurable string providerUrl = ?;

listener jms:Listener jmsListener = check new ({
initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
providerUrl: providerUrl
});

Listener with authentication:

import ballerinax/java.jms;

configurable string providerUrl = ?;
configurable string username = ?;
configurable string password = ?;

listener jms:Listener jmsListener = check new ({
initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
providerUrl: providerUrl,
username: username,
password: password
});

Service

A jms:Service is a Ballerina service attached to a jms:Listener. It is annotated with @jms:ServiceConfig to specify the queue or topic to consume from, along with session acknowledgement mode and optional message selectors.

Callback signatures

FunctionSignatureDescription
onMessageremote function onMessage(jms:Message message) returns error?Invoked when a message arrives on the configured queue or topic.
onMessage (with Caller)remote function onMessage(jms:Message message, jms:Caller caller) returns error?Invoked with a Caller for manual acknowledgement or transaction control.
note

The jms:Caller parameter is optional. Include it when you need manual acknowledgement (CLIENT_ACKNOWLEDGE mode) or transaction control (SESSION_TRANSACTED mode).

Full usage example

import ballerina/log;
import ballerinax/java.jms;

configurable string providerUrl = ?;

listener jms:Listener jmsListener = check new ({
initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
providerUrl: providerUrl
});

@jms:ServiceConfig {
queueName: "orders"
}
service "order-processor" on jmsListener {
remote function onMessage(jms:Message message, jms:Caller caller) returns error? {
match message.content {
var content if content is byte[] => {
string textContent = check string:fromBytes(content);
log:printInfo("Received order", content = textContent);
}
var content if content is string => {
log:printInfo("Received text message", content = content);
}
var content if content is map<jms:Value> => {
log:printInfo("Received map message", content = content.toString());
}
}
check caller->acknowledge(message);
}
}
note

Use @jms:ServiceConfig with queueName for queue consumption or topicName for topic subscription. For durable topic subscriptions, set consumerType: jms:DURABLE and provide a subscriberName.


Supporting types

Message

FieldTypeDescription
messageIdstring?Unique identifier assigned by the JMS provider.
timestampint?The time the message was handed off to the provider for sending.
correlationIdstring?Correlation ID for linking messages (e.g., request-reply).
replyToDestination?The destination to which replies should be sent.
destinationDestination?The destination this message was sent to.
deliveryModeint?Delivery mode (1 = non-persistent, 2 = persistent).
redeliveredboolean?Whether this message is being redelivered.
jmsTypestring?The JMS type header value.
expirationint?Expiration time in milliseconds (0 = no expiration).
deliveredTimeint?The earliest time the message can be delivered.
priorityint?Message priority (0-9, default 4).
propertiesmap<Property>?Application-defined message properties.
contentstring|map<Value>|byte[]The message body: text, map, or binary content.

Destination

FieldTypeDescription
typeDestinationTypeDestination type: QUEUE, TEMPORARY_QUEUE, TOPIC, or TEMPORARY_TOPIC.
namestring?The destination name (not required for temporary destinations).

QueueConfig

FieldTypeDescription
sessionAckModeAcknowledgementModeSession acknowledgement mode. Defaults to AUTO_ACKNOWLEDGE.
queueNamestringThe name of the queue to consume from.
messageSelectorstring?Optional JMS message selector expression.

TopicConfig

FieldTypeDescription
sessionAckModeAcknowledgementModeSession acknowledgement mode. Defaults to AUTO_ACKNOWLEDGE.
topicNamestringThe name of the topic to subscribe to.
messageSelectorstring?Optional JMS message selector expression.
noLocalbooleanIf true, excludes messages published by this connection. Defaults to false.
consumerTypeConsumerTypeConsumer type: DEFAULT, DURABLE, SHARED, or SHARED_DURABLE.
subscriberNamestring?Subscription name, required for DURABLE and SHARED_DURABLE types.