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

Introduction

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.

Main Article

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

# Copyright 2012 Oracle Corporation. 
# All Rights Reserved. 
# 
# Provided on an 'as is' basis, without warranties or conditions of any kind, 
# either express or implied, including, without limitation, any warranties or 
# conditions of title, non-infringement, merchantability, or fitness for a 
# particular purpose. You are solely responsible for determining the 
# appropriateness of using and assume any risks. You may not redistribute. 

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();
    }

  }

}

Add Your Comment