使用 Fiorano JMS 的 C# 应用程序在退出时挂起

C# Application using Fiorano JMS hangs on exit

我正在使用 Fiorano 的 C# 对 JMS 的支持来发布关于某个主题的消息。 一切顺利,直到应用程序退出。然后它实际上并没有退出。 我假设 Fiorano 是 运行 一个前台线程(但这是我的猜测。)

这是一个说明问题的最小但完整的示例:

    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Linq;
    using fiorano.csharp.naming;
       // Note: Reference to 
       //   Assembly: fmq-csharp-native 
       //   Version: v2.0.50727
       //   Runtime Version: 10.2.0.10533

    namespace NotificationClientTest
    {
        /// <summary>
        /// Main program
        /// </summary>
        public static class Program
        {
            /// <summary>
            /// Main method
            /// </summary>
            /// <param name="args">The arguments.  If any exist we hang.</param>
            public static void Main(string[] args)
            {
                Report("Enter Main"); 
                TheFioranoHangOnExit(args.Any());
                Report("Leave Main");
            }

            /// <summary>
            /// Trigger the problem.
            /// </summary>
            /// <param name="problem"> If true, trigger the problem  </param>
            private static void TheFioranoHangOnExit(bool problem)
            {
                // Initialize queue
                var contextProperties = new Hashtable
                    {
                        { FioranoContext.SECURITY_PRINCIPAL, "user" },
                        { FioranoContext.SECURITY_CREDENTIALS, "secretPassword" },
                        { FioranoContext.PROVIDER_URL, "http://192.168.5.1:1956" },
                        { FioranoContext.BACKUP_URLS, "http://192.168.5.2:1956" },
                        { FioranoContext.INITIAL_CONTEXT_FACTORY, "fiorano.jms.runtime.naming.FioranoInitialContextFactory" },
                    };

                var namingContext = new FioranoNamingContext(contextProperties);
                var topicFactory = namingContext.lookupTCF("PRIMARYTCF");

                if (problem)
                {
                    Report("Creating a connection");
                    var connection = topicFactory.createTopicConnection();
                    connection.stop(); // I've tried swapping the order of stop and close just in case...
                    connection.close();
                }
                else
                {
                    Report("No Connection");
                }
                namingContext.close();
            }

            /// <summary>
            /// Write to console, write to debug window
            /// </summary>
            /// <param name="message">What to say</param>
            private static void Report(string message)
            {
                Console.WriteLine(message);
                Debug.WriteLine(message);
            }
        }
    }

运行这个应用

    C:\Playground\FioranoHangTest\bin\Debug>NotificationClientTest.exe
    Enter Main
    No Connection
    Leave Main

    C:\Playground\FioranoHangTest\bin\Debug>NotificationClientTest.exe oops
    Enter Main
    Creating a connection
    Leave Main
    [At this point the program hangs.
     ^C or Task Manager can kill it.]

This question 描述了在 Java 中使用 GlassFish 的 JMS 遇到的类似问题。 Fiorano/C#有没有同样的问题?

否则,我错过了什么?

我发现了一个丑陋的解决方法:

        public static void Main(string[] args)
        {
            Report("Enter Main"); 
            TheFioranoHangOnExit(args.Any());
            Report("Leave Main");
            // Evict Fiorano Foreground threads.
            Environment.Exit(0); 
        }

打扰一下,打完那个我得去用碱液洗手指了。

我仍然希望得到更好的答案。