Using JMS in .Net Applications

Introduction

The WebLogic JMS .NET Client is bundled with WebLogic Server 10g Release 3 and later. When you perform a Complete installation of WebLogic Server on a supported platform, including non-Windows platforms, the WebLogic JMS .NET Client is installed by default. If you choose the Custom installation option, ensure that the WebLogic Server Clients component of WebLogic Server is selected.

Main Article

I was asked recently what we could do about allowing .Net applications to participate in pub/sub environments alongside Java applications.  It turns out that WebLogic Server, since 10.3, has a built-in .Net JMS client, which allows us to easily write JMS programs in C#.  This client supports most of the JMS 1.1 standard (the exceptions being to do with temporary queues and topics) and several of the WebLogic extensions too.  It supports pub/sub and persistent and non-persistent messaging.  Even durable subscriptions!

You can find some documentation on the client at http://download.oracle.com/docs/cd/E15523_01/web.1111/e13746/develop.htm

In this post, I will show you how easy it is to build a simple JMS client in C#.  In the images I am using Visual C# 2008 Express Edition, which is freely available from Microsoft’s web site for anyone who would like to try this out, but does not have access to a full install of Visual Studio.  Everything will work with the Express Edition.

Preparation

First of all, you will need a WebLogic Server (10.3 or later) with JMS set up.  At the very least you need a Connection Factory and a Queue.  If you don’t know how to set it up, take a look at this earlier post.

You will also need the WebLogic JMS .NET client, which is installed in the following directory on the WebLogic Server platform:

MW_HOME/modules/com.bea.weblogic.jms.dotnetclient_x.x.x.x

There is a WebLogic.Messaging.dll in that directory.  Take a copy of that file, you will need it later.  There is also a pdb if you want to be able to debug!

Note that the library will be there regardless of the server platform.  In this post, I am using a WebLogic Server running on Oracle Enterprise Linux.  The server does not need to be running on Windows.

Creating the Client

Now we are ready to create our C# JMS client.  In Visual Studio, create a New Project.  I chose Console Application, you may want to be more creative!

img1

I called my project JMS.  An empty project will be displayed, as shown below.

img2

Now we need to add a reference to our WebLogic.Messaging.dll.  This is done by right clicking on the References in the Solution Explorer and selecting Add Reference…

img3

In the dialog box, switch to the Browse tab, and locate the dll you copied earlier.  Click on OK to add it to your project.

img4

Now you are ready to add the code to your Class.  You can copy my code from below.  The comments explain what is going on.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using WebLogic.Messaging;

namespace JMS
{

    class Program
    {

        private static string host = "ec2-67-202-24-135.compute-1.amazonaws.com";

        private static int port = 8001;

        private static string cfName = "weblogic.jms.ConnectionFactory";

        private static string queueName = "jms/marksQueue";

        private static string username = "weblogic";

        private static string password = "welcome1";

        static void Main(string[] args)
        {

            // create properties dictionary

            IDictionary<string, Object> paramMap = new Dictionary<string, Object>();

            // add necessary properties

            paramMap[Constants.Context.PROVIDER_URL] =

            "t3://" + host + ":" + port;

            paramMap[Constants.Context.SECURITY_PRINCIPAL] = username;

            paramMap[Constants.Context.SECURITY_CREDENTIALS] = password;

            // get the initial context

            IContext context = ContextFactory.CreateContext(paramMap);

            // lookup the connection factory

            IConnectionFactory cf = context.LookupConnectionFactory(cfName);

            // lookup the queue

            IQueue queue = (IQueue)context.LookupDestination(queueName);

            // create a connection

            IConnection connection = cf.CreateConnection();

            // start the connection

            connection.Start();

            // create a session

            ISession session = connection.CreateSession(

            Constants.SessionMode.AUTO_ACKNOWLEDGE);

            // create a message producer

            IMessageProducer producer = session.CreateProducer(queue);

            producer.DeliveryMode = Constants.DeliveryMode.PERSISTENT;

            // create a text message

            ITextMessage textMessage = session.CreateTextMessage("Hello from .Net!!");

            // send the message

            producer.Send(textMessage);

            // CLEAN UP

            connection.Close();

            context.CloseAll();

        }

    }

}

You can now Build (F6) and Run(F5) your code.  If all went well, it should have put a message on the queue containing the text “Hello from .Net!!” which we should be able to see from the WebLogic console.

In the WebLogic console (again, see this earlier post if you are not sure how to get there) navigate to your queue.

img5

Click on the name of the queue to view details, and then open the Monitoring tab.

img6

You should see that you have a message in the queue.  Click on the check box beside the queue and the click on the Show Messages button.  You will now see a list of messages in the queue.

img6

Click on the message ID to view more details of the message, including the content (payload).

img8

There it is!  So you see that it can be very easy to create a C# JMS client.  The documentation that I mentioned at the beginning of this post contains a lot more information and some sample code.  There is also API level documentation there to help you explore further.  Good luck!

Add Your Comment