ballerina/mb package

Package overview

This ballerina/mb package provides an API to connect to Ballerina Message Broker instance.

The package provides consumer and producer endpoint types for queues and topics. Following are the endpoint types supported by this package

  • SimpleQueueReceiver
  • SimpleTopicSubscriber
  • SimpleDurableTopicSubscriber
  • SimpleQueueSender
  • SimpleTopicPublisher

Samples

Simple Queue Receiver

Following is a simple listener program that consumes messages from a Ballerina Message Broker queue named MyQueue.

import ballerina/mb;
import ballerina/log;

// Create a simple queue receiver.
mb:SimpleQueueReceiver receiver {
   host: "localhost",
   port: 5672,
   queueName: "MyQueue"
};

// Bind the receiver to the queue to get the messages.
service<mb:Consumer> mbListener bind receiver {

   // Receive the messages that comes to the queue.
   onMessage(endpoint consumer, mb:Message message) {
       match (message.getTextMessageContent()) {
           string messageText => log:printInfo("Message : " + messageText);
           error e => log:printError("Error occurred while reading message", err=e);
       }
   }
}

Simple Queue Sender

Following is a simple queue sender program that sends messages to a Ballerina Message Broker queue named MyQueue.

import ballerina/mb;
import ballerina/log;

// Create a queue sender.
endpoint mb:SimpleQueueSender queueSender {
   host: "localhost",
   port: 5672,
   queueName: "MyQueue"
};

function main (string... args) {
   // Create a Text message.
   match (queueSender.createTextMessage("Hello from Ballerina")) {
       error e => {
           log:printError("Error occurred while creating message", err = e);
       }

       mb:Message msg => {
           // Send the Ballerina message to the JMS provider.
           queueSender->send(msg) but { error e => log:printError("Error occurred while sending message", err = e) };
       }
   }
}

public type BrokerURLConfig

Field Name Data Type Default Value Description
username string admin
password string admin
host string localhost
port int 5672
clientID string ballerina
virtualHost string default

public type ConsumerEndpointConfiguration

Field Name Data Type Default Value Description
session Session
identifier string

public type SimpleDurableTopicSubscriberEndpointConfiguration

Field Name Data Type Default Value Description
username string admin
password string admin
host string localhost
port int 5672
clientID string ballerina
virtualHost string default
connectionFactoryName string ConnectionFactory
acknowledgementMode string AUTO_ACKNOWLEDGE
identifier string
properties map
messageSelector string
topicPattern string

public type SimpleQueueListenerEndpointConfiguration

Field Name Data Type Default Value Description
username string admin
password string admin
host string localhost
port int 5672
clientID string ballerina
virtualHost string default
connectionFactoryName string ConnectionFactory
acknowledgementMode string AUTO_ACKNOWLEDGE
messageSelector string
properties map
queueName string

public type SimpleQueueSenderEndpointConfiguration

Field Name Data Type Default Value Description
username string admin
password string admin
host string localhost
port int 5672
clientID string ballerina
virtualHost string default
acknowledgementMode string AUTO_ACKNOWLEDGE
properties map
queueName string

public type SimpleTopicPublisherEndpointConfiguration

Field Name Data Type Default Value Description
username string admin
password string admin
host string localhost
port int 5672
clientID string ballerina
virtualHost string default
acknowledgementMode string AUTO_ACKNOWLEDGE
properties map
topicPattern string

public type SimpleTopicSubscriberEndpointConfiguration

Field Name Data Type Default Value Description
username string admin
password string admin
host string localhost
port int 5672
clientID string ballerina
virtualHost string default
connectionFactoryName string ConnectionFactory
acknowledgementMode string AUTO_ACKNOWLEDGE
messageSelector string
properties map
topicPattern string

public type Consumer object

  • <Consumer> getEndpoint() returns (ConsumerTemplate)

    Return Type Description
    ConsumerTemplate

public type ConsumerActions object

  • <ConsumerActions> acknowledge(Message message) returns (error)

    Parameter Name Data Type Default Value Description
    message Message
    Return Type Description
    error

public type ConsumerTemplate object

Field Name Data Type Default Value Description
callerActions ConsumerActions
config ConsumerEndpointConfiguration
  • <ConsumerTemplate> init(ConsumerEndpointConfiguration config)

    Parameter Name Data Type Default Value Description
    config ConsumerEndpointConfiguration
  • <ConsumerTemplate> register(typedesc serviceType)

    Parameter Name Data Type Default Value Description
    serviceType typedesc
  • <ConsumerTemplate> start()

  • <ConsumerTemplate> stop()

  • <ConsumerTemplate> getCallerActions() returns (ConsumerActions)

    Return Type Description
    ConsumerActions

public type DurableTopicSubscriberActions object

Field Name Data Type Default Value Description
helper DurableTopicSubscriberActions
  • <DurableTopicSubscriberActions> acknowledge(Message message) returns (error)

    Parameter Name Data Type Default Value Description
    message Message
    Return Type Description
    error
  • <DurableTopicSubscriberActions> receive() returns (Message | error)

    Return Type Description
    Message | error

public type Message object

  • <Message> getTextMessageContent() returns (string | error)

    Gets text content of the JMS message returns message content as string

    Return Type Description
    string | error
  • <Message> setStringProperty(string key, string value) returns (error)

    Sets a JMS transport string property from the message

    Parameter Name Data Type Default Value Description
    key string

    The string property name

    value string

    The string property value

    Return Type Description
    error
  • <Message> getStringProperty(string key) returns (string | error)

    Gets a JMS transport string property from the message

    Parameter Name Data Type Default Value Description
    key string

    The string property name returns The string property value

    Return Type Description
    string | error
  • <Message> setIntProperty(string key, int value) returns (error)

    Sets a JMS transport integer property from the message

    Parameter Name Data Type Default Value Description
    key string

    The integer property name

    value int

    The integer property value

    Return Type Description
    error
  • <Message> getIntProperty(string key) returns (int | error)

    Gets a JMS transport integer property from the message

    Parameter Name Data Type Default Value Description
    key string

    The integer property name returns The integer property value

    Return Type Description
    int | error
  • <Message> setBooleanProperty(string key, boolean value) returns (error)

    Sets a JMS transport boolean property from the message

    Parameter Name Data Type Default Value Description
    key string

    The boolean property name

    value boolean

    The boolean property value

    Return Type Description
    error
  • <Message> getBooleanProperty(string key) returns (boolean | error)

    Gets a JMS transport boolean property from the message

    Parameter Name Data Type Default Value Description
    key string

    The boolean property name returns The boolean property value

    Return Type Description
    boolean | error
  • <Message> setFloatProperty(string key, float value) returns (error)

    Sets a JMS transport float property from the message

    Parameter Name Data Type Default Value Description
    key string

    The float property name

    value float

    The float property value

    Return Type Description
    error
  • <Message> getFloatProperty(string key) returns (float | error)

    Gets a JMS transport float property from the message

    Parameter Name Data Type Default Value Description
    key string

    The float property name returns The float property value

    Return Type Description
    float | error
  • <Message> getMessageID() returns (string | error)

    Get JMS transport header MessageID from the message returns The header value

    Return Type Description
    string | error
  • <Message> getTimestamp() returns (int | error)

    Get JMS transport header Timestamp from the message returns The header value

    Return Type Description
    int | error
  • <Message> setDeliveryMode(int mode) returns (error)

    Sets DeliveryMode JMS transport header to the message

    Parameter Name Data Type Default Value Description
    mode int

    The header value

    Return Type Description
    error
  • <Message> getDeliveryMode() returns (int | error)

    Get JMS transport header DeliveryMode from the message returns The header value"

    Return Type Description
    int | error
  • <Message> setExpiration(int value) returns (error)

    Sets Expiration JMS transport header to the message

    Parameter Name Data Type Default Value Description
    value int

    The header value

    Return Type Description
    error
  • <Message> getExpiration() returns (int | error)

    Get JMS transport header Expiration from the message returns int: The header value

    Return Type Description
    int | error
  • <Message> clearProperties()

    Clear JMS properties of the message returns error if any JMS provider level internal error occur

  • <Message> clearBody() returns (error)

    Clears body of the JMS message returns error if any JMS provider level internal error occur

    Return Type Description
    error
  • <Message> setPriority(int value) returns (error)

    Sets Priority JMS transport header to the message

    Parameter Name Data Type Default Value Description
    value int

    The header value

    Return Type Description
    error
  • <Message> getPriority() returns (int | error)

    Get JMS transport header Priority from the message returns The header value

    Return Type Description
    int | error
  • <Message> getRedelivered() returns (boolean | error)

    Get JMS transport header Redelivered from the message returns The header value

    Return Type Description
    boolean | error
  • <Message> setCorrelationID(string value) returns (error)

    Sets CorrelationID JMS transport header to the message

    Parameter Name Data Type Default Value Description
    value string

    The header value

    Return Type Description
    error
  • <Message> getCorrelationID() returns (string | error)

    Get JMS transport header CorrelationID from the message returns The header value

    Return Type Description
    string | error

public type QueueReceiverActions object

Field Name Data Type Default Value Description
helper QueueReceiverActions
  • <QueueReceiverActions> acknowledge(Message message) returns (error)

    Parameter Name Data Type Default Value Description
    message Message
    Return Type Description
    error
  • <QueueReceiverActions> receive() returns (Message | error)

    Return Type Description
    Message | error

public type QueueSenderActions object

  • <QueueSenderActions> send(Message m) returns (error)

    Parameter Name Data Type Default Value Description
    m Message
    Return Type Description
    error

public type SimpleDurableTopicSubscriber object

Field Name Data Type Default Value Description
config SimpleDurableTopicSubscriberEndpointConfiguration
  • <SimpleDurableTopicSubscriber> init(SimpleDurableTopicSubscriberEndpointConfiguration config)

    Parameter Name Data Type Default Value Description
    config SimpleDurableTopicSubscriberEndpointConfiguration
  • <SimpleDurableTopicSubscriber> register(typedesc serviceType)

    Parameter Name Data Type Default Value Description
    serviceType typedesc
  • <SimpleDurableTopicSubscriber> start()

  • <SimpleDurableTopicSubscriber> getCallerActions() returns (DurableTopicSubscriberActions)

    Return Type Description
    DurableTopicSubscriberActions
  • <SimpleDurableTopicSubscriber> stop()

  • <SimpleDurableTopicSubscriber> createTextMessage(string message) returns (Message | error)

    Parameter Name Data Type Default Value Description
    message string
    Return Type Description
    Message | error

public type SimpleQueueReceiver object

Field Name Data Type Default Value Description
config SimpleQueueListenerEndpointConfiguration
  • <SimpleQueueReceiver> init(SimpleQueueListenerEndpointConfiguration config)

    Parameter Name Data Type Default Value Description
    config SimpleQueueListenerEndpointConfiguration
  • <SimpleQueueReceiver> register(typedesc serviceType)

    Parameter Name Data Type Default Value Description
    serviceType typedesc
  • <SimpleQueueReceiver> start()

  • <SimpleQueueReceiver> getCallerActions() returns (QueueReceiverActions)

    Return Type Description
    QueueReceiverActions
  • <SimpleQueueReceiver> stop()

  • <SimpleQueueReceiver> createTextMessage(string message) returns (Message | error)

    Parameter Name Data Type Default Value Description
    message string
    Return Type Description
    Message | error

public type SimpleQueueSender object

Field Name Data Type Default Value Description
config SimpleQueueSenderEndpointConfiguration
  • <SimpleQueueSender> init(SimpleQueueSenderEndpointConfiguration config)

    Parameter Name Data Type Default Value Description
    config SimpleQueueSenderEndpointConfiguration
  • <SimpleQueueSender> register(typedesc serviceType)

    Parameter Name Data Type Default Value Description
    serviceType typedesc
  • <SimpleQueueSender> start()

  • <SimpleQueueSender> getCallerActions() returns (QueueSenderActions)

    Return Type Description
    QueueSenderActions
  • <SimpleQueueSender> stop()

  • <SimpleQueueSender> createTextMessage(string message) returns (Message | error)

    Parameter Name Data Type Default Value Description
    message string
    Return Type Description
    Message | error

public type SimpleTopicPublisher object

Field Name Data Type Default Value Description
config SimpleTopicPublisherEndpointConfiguration
  • <SimpleTopicPublisher> init(SimpleTopicPublisherEndpointConfiguration config)

    Parameter Name Data Type Default Value Description
    config SimpleTopicPublisherEndpointConfiguration
  • <SimpleTopicPublisher> register(typedesc serviceType)

    Parameter Name Data Type Default Value Description
    serviceType typedesc
  • <SimpleTopicPublisher> start()

  • <SimpleTopicPublisher> getCallerActions() returns (TopicPublisherActions)

    Return Type Description
    TopicPublisherActions
  • <SimpleTopicPublisher> stop()

  • <SimpleTopicPublisher> createTextMessage(string message) returns (Message | error)

    Parameter Name Data Type Default Value Description
    message string
    Return Type Description
    Message | error

public type SimpleTopicSubscriber object

Field Name Data Type Default Value Description
config SimpleTopicSubscriberEndpointConfiguration
  • <SimpleTopicSubscriber> init(SimpleTopicSubscriberEndpointConfiguration config)

    Parameter Name Data Type Default Value Description
    config SimpleTopicSubscriberEndpointConfiguration
  • <SimpleTopicSubscriber> register(typedesc serviceType)

    Parameter Name Data Type Default Value Description
    serviceType typedesc
  • <SimpleTopicSubscriber> start()

  • <SimpleTopicSubscriber> getCallerActions() returns (TopicSubscriberActions)

    Return Type Description
    TopicSubscriberActions
  • <SimpleTopicSubscriber> stop()

  • <SimpleTopicSubscriber> createTextMessage(string message) returns (Message | error)

    Parameter Name Data Type Default Value Description
    message string
    Return Type Description
    Message | error

public type TopicPublisherActions object

  • <TopicPublisherActions> send(Message m) returns (error)

    Parameter Name Data Type Default Value Description
    m Message
    Return Type Description
    error

public type TopicSubscriberActions object

Field Name Data Type Default Value Description
helper TopicSubscriberActions
  • <TopicSubscriberActions> acknowledge(Message message) returns (error)

    Parameter Name Data Type Default Value Description
    message Message
    Return Type Description
    error
  • <TopicSubscriberActions> receive() returns (Message | error)

    Return Type Description
    Message | error