Back to Examples

JMS message producer - Manual acknowledgment

The jms:MessageConsumer allows manual acknowledgment for received messages. A jms:MessageConsumer can be initialized by using a jms:Session object. To configure manual acknowledgment mode, create a jms:Session object with the acknowledge mode set to jms:CLIENT_ACKNOWLEDGE.

import ballerina/io;
import ballerinax/java.jms;
import ballerinax/activemq.driver as _;

public function main() returns error? {
    jms:Connection connection = check new (
        initialContextFactory = "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
        providerUrl = "tcp://localhost:61616"
    );
    jms:Session session = check connection->createSession(jms:CLIENT_ACKNOWLEDGE);
    jms:MessageConsumer consumer = check session.createConsumer(destination = {
        'type: jms:QUEUE,
        name: "order-queue"
    });
    jms:Message? message = check consumer->receive(5000);
    if message is jms:MapMessage {
        map<anydata> payload = message.content;
        int|error orderId = payload["orderId"].ensureType();
        if orderId is int {
            io:println("Received message from the JMS provider", orderId);
            check consumer->acknowledge(message);
        }
    }
}

Prerequisites

Start a ActiveMQ broker instance.

Run the program by executing the following command.

$ bal run jms_consumer_acknowledgement.bal

Tip: Run the JMS message producer given in the JMS message producer - Produce message example to produce a few sample messages to the queue.

Related links

PreviousConsume message
NextSend/Receive bytes