Code Monkey home page Code Monkey logo

module-jms's Introduction

Build Status

Module overview

The wso2/jms module provides an API to connect to an external JMS provider like ActiveMQ from Ballerina.

This module is created with minimal deviation from the JMS API to make it easy for the developers who are used to working with the JMS API. This module is written to support both JMS 2.0 and JMS 1.0 API.

Currently, the following JMS API Classes are supported through this module.

  • Connection
  • Session
  • Destination (Queue, Topic, TemporaryQueue, TemporaryTopic)
  • Message (TextMessage, MapMessage, BytesMessage, StreamMessage)
  • MessageConsumer
  • MessageProducer

The following sections provide details on how to use the JMS connector.

Compatibility

Version
Ballerina Language 1.0.1

Samples

JMS Message Producer and Consumer example

Following is a simple Ballerina program that sends and receives a message from a queue named MyQueue.

import ballerina/log;
import wso2/jms;

public function main() returns error? {

    jms:Connection connection = check jms:createConnection({
                      initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
                      providerUrl: "tcp://localhost:61616"
                    });
    jms:Session session = check connection->createSession({acknowledgementMode: "AUTO_ACKNOWLEDGE"});
    jms:Destination queue = check session->createQueue("MyQueue");
    jms:MessageProducer producer = check session.createProducer(queue);
    jms:MessageConsumer consumer = check session->createConsumer(queue);

    jms:TextMessage msg = check session.createTextMessage("Hello Ballerina!");

    check producer->send(msg);

    jms:Message? response = check consumer->receive(3000);
    if (response is jms:TextMessage) {
        var val = response.getText();
        if (val is string) {
            log:printInfo("Message received: " + val);
        } else {
            log:printInfo("Message received without text");
        }
    } else {
        log:printInfo("Message received.");
    }
}

Asynchronous message consumer

One of the key deviations from the JMS API was the asynchronous message consumption using message listeners. In Ballerina transport listener, the concept is covered with the service type, hence we have used the Ballerina service to implement the message listener. Following is a message listener example listening on a topic named MyTopic.

import ballerina/log;
import wso2/jms;

jms:Connection connection = check jms:createConnection({
                   initialContextFactory: "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
                   providerUrl: "tcp://localhost:61616"
              });
jms:Session session = check connection->createSession({acknowledgementMode: "AUTO_ACKNOWLEDGE"});
jms:Destination topic = check session->createTopic("MyTopic");

listener jms:MessageConsumer jmsConsumer = check session->createDurableSubscriber(topic, "sub-1");

service messageListener on jmsConsumer {

   resource function onMessage(jms:Message message) {
       if (message is jms:TextMessage) {
           var val = message.getText();
           if (val is string) {
               log:printInfo("Message received: " + val );
           } else {
               log:printInfo("Message received without text");
           }
       } else {
           log:printInfo("Message received.");
       }
   }
}

module-jms's People

Contributors

indikasampath2000 avatar aashikam avatar asitha avatar ballerina-bot avatar maryamzi avatar vinok88 avatar thisaruguruge avatar riyafa avatar hasithaa avatar supuns avatar vijithaekanayake avatar lankavitharana avatar dinuish94 avatar rdhananjaya avatar pubudu91 avatar manuranga avatar anoukh avatar wggihan avatar pamod avatar anupama-pathirage avatar chamil321 avatar keizer619 avatar kishanthan avatar hasuniea avatar manuri avatar daneshk avatar hevayo avatar gimantha avatar mohanvive avatar arukshani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.