import ballerina/jms;
import ballerina/mb;
import ballerina/log;
jms:Connection conn = new ({
initialContextFactory: "wso2mbInitialContextFactory",
providerUrl: "amqp://admin:admin@carbon/carbon?brokerlist='tcp://localhost:5672'"
});
jms:Session jmsSession = new (conn, {
acknowledgementMode: "AUTO_ACKNOWLEDGE"
});
endpoint jms:QueueReceiver consumer {
session: jmsSession,
queueName: "MyQueue"
};
service<jms:Consumer> jmsListener bind consumer {
onMessage(endpoint consumer, jms:Message message) {
string messageText = check message.getTextMessageContent();
log:printInfo("Message : " + messageText);
}
}
JMS Queue Message ReceiverBallerina natively supports JMS. Here is an example where we create a simple JMS queue receiver. |
|
import ballerina/jms;
import ballerina/mb;
import ballerina/log;
|
|
jms:Connection conn = new ({
initialContextFactory: "wso2mbInitialContextFactory",
providerUrl: "amqp://admin:admin@carbon/carbon?brokerlist='tcp://localhost:5672'"
});
|
Initialize a JMS connection with the provider. |
jms:Session jmsSession = new (conn, {
|
Initialize a JMS session on top of the created connection. |
acknowledgementMode: "AUTO_ACKNOWLEDGE"
});
|
Optional property. Defaults to AUTO_ACKNOWLEDGE |
endpoint jms:QueueReceiver consumer {
session: jmsSession,
queueName: "MyQueue"
};
|
Initialize a queue receiver using the created session. |
service<jms:Consumer> jmsListener bind consumer {
|
Bind the created consumer to the listener service. |
onMessage(endpoint consumer, jms:Message message) {
string messageText = check message.getTextMessageContent();
log:printInfo("Message : " + messageText);
}
}
|
OnMessage resource get invoked when a message is received. |
$ ballerina run jms-queue-message-receiver.bal
|
To run the program, put the code in |
JMS subscriber run as a Ballerina service listening on the subscribed queue or topic |