MessageUtils.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Messaging » Nms » Support » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Messaging » Nms » Support » MessageUtils.cs
#region License

/*
 * Copyright  2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

using System;
using Common.Logging;
using Spring.Util;
using Apache.NMS;

namespace Spring.Messaging.Nms.Support{
    /// <summary>
    /// Generic utility methods for working with NMS. Mainly for internal use
    /// within the framework, but also useful for custom NMS access code.
    /// </summary>
    public abstract class NmsUtils
    {
        #region Logging

        private static readonly ILog logger = LogManager.GetLogger(typeof(NmsUtils));

        #endregion

        /// <summary> Close the given NMS Connection and ignore any thrown exception.
        /// This is useful for typical <code>finally</code> blocks in manual NMS code.
        /// </summary>
        /// <param name="con">the NMS Connection to close (may be <code>null</code>)
        /// </param>
        public static void CloseConnection(IConnection con)
        {
            CloseConnection(con, false);
        }

        /// <summary> Close the given NMS Connection and ignore any thrown exception.
        /// This is useful for typical <code>finally</code> blocks in manual NMS code.
        /// </summary>
        /// <param name="con">the NMS Connection to close (may be <code>null</code>)
        /// </param>
        /// <param name="stop">whether to call <code>stop()</code> before closing
        /// </param>
        public static void CloseConnection(IConnection con, bool stop)
        {
            if (con != null)
            {
                try
                {
                    if (stop)
                    {
                        try
                        {
                            con.Stop();
                        }
                        finally
                        {
                            con.Close();
                        }
                    }
                    else
                    {
                        con.Close();
                    }
                }
                catch (NMSException ex)
                {
                    logger.Debug("Could not close NMS Connection", ex);
                }
                catch (Exception ex)
                {
                    // We don't trust the NMS provider: It might throw another exception.
                    logger.Debug("Unexpected exception on closing NMS Connection", ex);
                }
            }
        }
        
        /// <summary> Close the given NMS Session and ignore any thrown exception.
    /// This is useful for typical <code>finally</code> blocks in manual NMS code.
    /// </summary>
    /// <param name="session">the NMS Session to close (may be <code>null</code>)
    /// </param>
    public static void CloseSession(ISession session)
        {
            if (session != null)
            {
                try
                {
                    session.Close();
                }
                catch (NMSException ex)
                {
                    logger.Debug("Could not close NMS ISession", ex);
                }
                catch (Exception ex)
                {
                    // We don't trust the NMS provider: It might throw RuntimeException or Error.
                    logger.Debug("Unexpected exception on closing NMS ISession", ex);
                }
            }
        }
        
        /// <summary> Close the given NMS MessageProducer and ignore any thrown exception.
    /// This is useful for typical <code>finally</code> blocks in manual NMS code.
    /// </summary>
    /// <param name="producer">the NMS MessageProducer to close (may be <code>null</code>)
    /// </param>
        public static void CloseMessageProducer(IMessageProducer producer)
        {
            if (producer != null)
            {
                try
                {
                    producer.Close();
                }
                catch (NMSException ex)
                {
                    logger.Debug("Could not close NMS MessageProducer", ex);
                }
                catch (Exception ex)
                {
                    // We don't trust the NMS provider: It might throw RuntimeException or Error.
                    logger.Debug("Unexpected exception on closing NMS MessageProducer", ex);
                }
            }
        }

        /// <summary> Close the given NMS MessageConsumer and ignore any thrown exception.
        /// This is useful for typical <code>finally</code> blocks in manual NMS code.
        /// </summary>
        /// <param name="consumer">the NMS MessageConsumer to close (may be <code>null</code>)
        /// </param>
        public static void CloseMessageConsumer(IMessageConsumer consumer)
        {
            if (consumer != null)
            {
                try
                {
                    consumer.Close();
                }
                catch (NMSException ex)
                {
                    logger.Debug("Could not close NMS MessageConsumer", ex);
                }
                catch (Exception ex)
                {
                    // We don't trust the NMS provider: It might throw RuntimeException or Error.
                    logger.Debug("Unexpected exception on closing NMS MessageConsumer", ex);
                }
            }
        }
/*
        /// <summary> Close the given NMS QueueRequestor and ignore any thrown exception.
        /// This is useful for typical <code>finally</code> blocks in manual NMS code.
        /// </summary>
        /// <param name="requestor">the NMS QueueRequestor to close (may be <code>null</code>)
        /// </param>
 */
//        public static void CloseQueueRequestor(QueueRequestor requestor)
//        {
//            if (requestor != null)
//            {
//                try
//                {
//                    requestor.Close();
//                }
//                catch (NMSException ex)
//                {
//                    logger.Debug("Could not close NMS QueueRequestor", ex);
//                }
//                catch (Exception ex)
//                {
//                    // We don't trust the NMS provider: It might throw RuntimeException or Error.
//                    logger.Debug("Unexpected exception on closing NMS QueueRequestor", ex);
//                }
//            }
//        }


        /// <summary> Commit the Session if not within a distributed transaction.</summary>
        /// <remarks>Needs investigation - no distributed tx in .NET messaging providers</remarks>
        /// <param name="session">the NMS Session to commit
        /// </param>
        /// <throws>NMSException if committing failed </throws>
        public static void CommitIfNecessary(ISession session)
        {
        AssertUtils.ArgumentNotNull(session, "ISession must not be null");
      
      session.Commit();

      // TODO Investigate
      
//        try {
//          session.Commit();
//        }
//        catch (TransactionInProgressException ex) {
//            // TODO Investigate
//          // Ignore -> can only happen in case of a JTA transaction.
//        }
//        catch (IllegalStateException ex) {
//            // TODO Investigate
//          // Ignore -> can only happen in case of a JTA transaction.
//        }
      }

        /// <summary> Rollback the Session if not within a distributed transaction.</summary>
        /// <remarks>Needs investigation - no distributed tx in EMS</remarks>
        /// <param name="session">the NMS Session to rollback
        /// </param>
        /// <throws>  NMSException if committing failed </throws>
      public static void RollbackIfNecessary(ISession session)
        {
        AssertUtils.ArgumentNotNull(session, "ISession must not be null");
      session.Rollback();

      // TODO Investigate

//        try {
//          session.Rollback();
//        }
//        catch (TransactionInProgressException ex) {
//          // Ignore -> can only happen in case of a JTA transaction.
//        }
//        catch (IllegalStateException ex) {
//          // Ignore -> can only happen in case of a JTA transaction.
//        }
      }
        
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.