server is the class that users instantiate to connect a SessionReceiver callback to the stream of received messages received on a Session.
示例#1
0
        public void ConnectAndReceive()
        {
            try
            {
                _sessionReceiver = new TradeConfirmationReceiver();
                /*
                   * Step 1: Preparing the connection and session
                   */
                _connection = new Connection(_brokerAddress);
                _connection.SetOption("reconnect", true);
                _connection.SetOption("reconnect_limit", 2);
                _connection.SetOption("reconnect_urls", _failBrokerAddress);

                //must set the username, a little different with Eurex's Demo code
                //the username is case sensitive
                //be carful, the .cert's frendly is empty in LCMLO_LIQSPALBB.crt
                _connection.SetOption("username", _memberName);
                _connection.SetOption("transport", "ssl");
                _connection.SetOption("sasl_mechanisms", "EXTERNAL");
                _connection.SetOption("heartbeat", 60);//unit:seconds

                _connection.Open();

                _session = _connection.CreateSession();
                /*
                 * Step 2: Create callback server and implicitly start it
                 */
                // The callback server is running and executing callbacks on a separate thread.
                _callbackServer = new CallbackServer(_session, _sessionReceiver);
                /*
                 * Step 3: Creating message consumer
                 */
                _receiver = _session.CreateReceiver(_broadcastAddress);
                _receiver.Capacity = 100;
            }
            catch (QpidException ex)
            {
                _Log.Error("Make connection to AMQP broker failed!", ex);
                throw;
            }
        }
        /// <summary>
        /// A function to illustrate how to open a Session callback and
        /// receive messages.
        /// </summary>
        /// <param name="args">Main program arguments</param>
        public int TestProgram(string[] args)
        {
            string url = "amqp:tcp:localhost:5672";
            string addr = "amq.direct/map_example";
            int nSec = 30;
            string connectionOptions = "";

            if (1 == args.Length)
            {
                if (args[0].Equals("-h") || args[0].Equals("-H") || args[0].Equals("/?"))
                {
                    usage(url, addr, nSec);
                    return 1;
                }
            }

            if (args.Length > 0)
                url = args[0];
            if (args.Length > 1)
                addr = args[1];
            if (args.Length > 2)
                nSec = System.Convert.ToInt32(args[2]);
            if (args.Length > 3)
                connectionOptions = args[3];

            //
            // Create and open an AMQP connection to the broker URL
            //
            Connection connection = new Connection(url, connectionOptions);
            connection.Open();

            //
            // Create a session.
            //
            Session session = connection.CreateSession();

            //
            // Receive through callback
            //
            // Create callback server and implicitly start it
            //
            SessionReceiver.CallbackServer cbServer =
                new SessionReceiver.CallbackServer(session, this);

            //
            // The callback server is running and executing callbacks on a
            // separate thread.
            //

            //
            // Create a receiver for the direct exchange using the
            // routing key "map_example".
            //
            Receiver receiver = session.CreateReceiver(addr);

            //
            // Establish a capacity
            //
            receiver.Capacity = 100;

            //
            // Wait so many seconds for messages to arrive.
            //
            System.Threading.Thread.Sleep(nSec * 1000);   // in mS

            //
            // Stop the callback server.
            //
            cbServer.Close();

            //
            // Close the receiver and the connection.
            //
            try
            {
                receiver.Close();
                connection.Close();
            }
            catch (Exception exception)
            {
                // receiver or connection may throw if they closed in error.
                // A typical application will take more action here.
                Console.WriteLine("{0} Closing exception caught.", exception.ToString());
            }
            return 0;
        }
示例#3
0
        static void Main(string[] args)
        {

            //RequestResponse();
            //return;

            //[[ The following two types of EnviromentVariable must set by manual, not here.
            //1)
            //System.Environment.SetEnvironmentVariable("QPID_SSL_CERT_STORE", "Personal", EnvironmentVariableTarget.Process);
            //System.Environment.SetEnvironmentVariable("QPID_SSL_CERT_NAME", "abcfr_abcfralmmacc1", EnvironmentVariableTarget.Process);


            //2)
            //System.Environment.SetEnvironmentVariable("QPID_LOG_ENABLE", "trace+", EnvironmentVariableTarget.Process);
            //System.Environment.SetEnvironmentVariable("QPID_SSL_CERT_FILENAME", @".\ABCFR_ABCFRALMMACC1.p12", EnvironmentVariableTarget.Process);
            //System.Environment.SetEnvironmentVariable("QPID_SSL_CERT_PASSWORD_FILE", @".\ABCFR_ABCFRALMMACC1.pwd", EnvironmentVariableTarget.Process);
            //System.Environment.SetEnvironmentVariable("QPID_SSL_CERT_NAME", "abcfr_abcfralmmacc1", EnvironmentVariableTarget.Process);
            //]]

            //string brokerAddr = args.Length > 0 ? args[0] : "amqp:ssl:192.168.34.11:11234";
            //shit, seems must use the host name
            string brokerAddr = args.Length > 0 ? args[0] : "amqp:ssl:chengdudev6:11234";
            string failBrokerAddr = args.Length > 1 ? args[1] : "amqp:ssl:chengdudev6:11234";

            string memberName = "ABCFR_ABCFRALMMACC1";
            string broadcastAddress = "broadcast." + memberName + ".TradeConfirmation; { node: { type: queue }, create: never, mode: consume, assert: always }";

            Connection connection = null;
            Session session;

            try
            {
                /*
                 * Step 1: Preparing the connection and session
                 */
                connection = new Connection(brokerAddr);
                connection.SetOption("reconnect", true);
                connection.SetOption("reconnect_limit", 2);
                connection.SetOption("reconnect_urls", failBrokerAddr);

                //shit, must set the username, a little different with Eurex's Demo code
                //shit, the username is case sensitive
                connection.SetOption("username", "ABCFR_ABCFRALMMACC1");

                connection.SetOption("transport", "ssl");
                connection.SetOption("sasl_mechanisms", "EXTERNAL");

                //connection.SetOption("heartbeat", 120);

                connection.Open();
                session = connection.CreateSession();
                /*
                 * Step 2: Create callback server and implicitly start it
                 */
                CallbackServer cbServer = new CallbackServer(session, new Listener());
                // The callback server is running and executing callbacks on a separate thread.
                /*
                 * Step 3: Creating message consumer
                 */
                Receiver receiver = session.CreateReceiver(broadcastAddress);
                receiver.Capacity = 100;

                Console.ReadLine();

                //System.Threading.Thread.Sleep(20 * 1000);   // in mS
                ///*
                // * Step 4: Stop the callback server and close receiver
                // */
                //cbServer.Close();
                //receiver.Close();
                //session.Sync();
            }
            catch (QpidException ex)
            {
                Console.WriteLine("QpidException caught: {0}", ex.Message);
                Console.WriteLine();
                Console.WriteLine("Press any key to continue!");
                Console.ReadLine();
            }
            finally
            {
                if (connection != null && connection.IsOpen)
                {
                    Console.WriteLine("Closing the connection.");
                    connection.Close();
                }
            }
        }