An updated simple WebLogic JMS client in .Net (C#)

In previous posts, we presented a simple WebLogic JMS client in Java here, and an updated one here.  We also presented a simple C# JMS program here.

In this post, we present a more or less equivalent C#/.Net implementation of the updated simple JMS client.  Please refer to the referenced posts for details of how to set up the necessary queues, etc. on WebLogic, and how to build and use this code.

Update: You can grab this code from our Subversion repository:
svn checkout https://www.samplecode.oracle.com/svn/jmsclients/trunk

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLogic.Messaging;

namespace SimpleJMSClient
{

  class Program
  {

    private static string DEFAULT_URL = "t3://localhost:7101";
    private static string cfName = "weblogic.jms.ConnectionFactory";
    private static string DEFAULT_QUEUE = "jms/MarksQueue";
    private static string DEFAULT_USER = "weblogic";
    private static string DEFAULT_PASSWORD = "weblogic1";

    static void Main(string[] args)
    {
      sendMessage("hello from .net with 1 arg");
      sendMessage("t3://localhost:7101",
                  "weblogic",
                  "weblogic1",
                  "jms/MarksQueue",
                  "hello from .net with 5 args");
    }

    static void sendMessage(string messageText)
    {
      sendMessage(DEFAULT_URL,
                  DEFAULT_USER,
                  DEFAULT_PASSWORD,
                  DEFAULT_QUEUE,
                  messageText);
    }

    static void sendMessage(string url, string user, string password, string queueName, string messageText)
    {
      // create properties dictionary
      IDictionary<string, Object> paramMap = new Dictionary<string, Object>();

      // add necessary properties
      paramMap[Constants.Context.PROVIDER_URL] = url;
      paramMap[Constants.Context.SECURITY_PRINCIPAL] = user;
      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(messageText);

      // send the message
      producer.Send(textMessage);

      // CLEAN UP
      connection.Close();
      context.CloseAll();
    }

  }

}

About Mark Nelson

Mark Nelson is a Consulting Solution Architect in the Fusion Middleware Architects Team (known as ”The A-Team”) in Oracle Development. Their mission is to supply deep technical expertise to support customers deploying Oracle Fusion Middleware, and to collect real world feedback to continuously improve the product set. Mark spends most of his time working on development lifecycle, SOA and BPM. Before joining Oracle Development in 2010, Mark worked in Sales Consulting at Oracle since 2006 and various roles at IBM since 1994, including several in Software Group and System/390 Group across Asia Pacific.
This entry was posted in group4, Uncategorized and tagged , , , . Bookmark the permalink.

2 Responses to An updated simple WebLogic JMS client in .Net (C#)

  1. Pingback: Simple JMS client in Scala | RedStack

  2. Pingback: An updated simple WebLogic JMS client | RedStack

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s