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 an Architect ("IC6") in the Platform Architecture Team in Oracle Development. Mark's focus area is continuous delivery, configuration management and provisioning - making it simple to manage the configuration of complex environments and applications built with Oracle Database, Fusion Middleware and Fusion Applications, on-premise and in the cloud. Before joining the Platform Architecture team, Mark was a senior member of the A-Team since 2010, and worked in Sales Consulting at Oracle since 2006 and various roles at IBM since 1994.
This entry was posted in 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 )

Google+ photo

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

Connecting to %s