Skip to main content

Triggers

The ballerinax/java.jms connector supports event-driven message consumption through a jms:Listener. When messages arrive on the queue or topic configured for the listener, 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 a single queue or topic.
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 is initialized with MessageListenerConfigurations, passed as named arguments to new:

FieldTypeDefaultDescription
connectionConfigConnectionConfigurationRequiredConfigures the JMS broker connection for the listener.
acknowledgementModeAcknowledgementModeAUTO_ACKNOWLEDGESession acknowledgement mode: AUTO_ACKNOWLEDGE, CLIENT_ACKNOWLEDGE, SESSION_TRANSACTED, or DUPS_OK_ACKNOWLEDGE.
consumerOptionsConsumerOptionsRequiredThe queue or topic to consume from, plus the underlying consumer options.

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.

ConsumerOptions fields:

FieldTypeDefaultDescription
destinationDestinationRequiredThe queue or topic to consume from.
typeConsumerTypeDEFAULTConsumer type: DEFAULT, DURABLE, SHARED, or SHARED_DURABLE. Non-default types require a TOPIC destination.
messageSelectorstring""JMS message selector expression to filter messages.
noLocalbooleanfalseIf true, excludes messages published by this connection.
subscriberNamestring()Subscription name, required for DURABLE, SHARED, and SHARED_DURABLE types.

Initializing the listener​

Basic listener with ActiveMQ:

import ballerinax/java.jms;

configurable string providerUrl = ?;

listener jms:Listener jmsListener = check new (
connectionConfig = {
initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
providerUrl: providerUrl
},
consumerOptions = {
destination: {
'type: jms:QUEUE,
name: "orders"
}
}
);

Listener with authentication:

import ballerinax/java.jms;

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

listener jms:Listener jmsListener = check new (
connectionConfig = {
initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
providerUrl: providerUrl,
username: username,
password: password
},
consumerOptions = {
destination: {
'type: jms:QUEUE,
name: "orders"
}
}
);

Service​

A jms:Service is a Ballerina service attached to a jms:Listener. The queue or topic to consume from, the session acknowledgement mode, and optional message selectors are configured on the listener (consumerOptions and acknowledgementMode), so each listener consumes from a single destination.

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 (
connectionConfig = {
initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
providerUrl: providerUrl
},
acknowledgementMode = jms:CLIENT_ACKNOWLEDGE,
consumerOptions = {
destination: {
'type: jms:QUEUE,
name: "orders"
}
}
);

service "order-processor" on jmsListener {
remote function onMessage(jms:Message message, jms:Caller caller) returns error? {
if message is jms:BytesMessage {
string textContent = check string:fromBytes(message.content);
log:printInfo("Received order", content = textContent);
} else if message is jms:TextMessage {
log:printInfo("Received text message", content = message.content);
} else if message is jms:MapMessage {
log:printInfo("Received map message", content = message.content.toString());
}
check caller->acknowledge(message);
}
}
note

Set consumerOptions.destination to {'type: jms:QUEUE, name: ...} for queue consumption or {'type: jms:TOPIC, name: ...} for topic subscription. For durable topic subscriptions, also set 'type: jms:DURABLE in consumerOptions 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<anydata>?Application-defined message properties.

Message has no body field. Received messages are one of the following subtypes, each of which includes all Message fields plus a content field:

Typecontent typeDescription
TextMessagestringText message body.
MapMessagemap<anydata>Map message body.
BytesMessagebyte[]Binary message body.

Destination​

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