예제 #1
0
    static void Main(string[] args)
    {
        Console.WriteLine("Remote host: ");
        string             host = Console.ReadLine();
        CConnectionContext cc   = new CConnectionContext(host, 20901, "async_queue_client", "pwd_for_async_queue");

        using (CSocketPool <CAsyncQueue> spAq = new CSocketPool <CAsyncQueue>())
        {
            if (!spAq.StartSocketPool(cc, 1))
            {
                Console.WriteLine("Failed in connecting to remote async queue server");
                Console.WriteLine("Press any key to close the application ......");
                Console.Read();
                return;
            }
            CAsyncQueue aq = spAq.Seek();
            try
            {
                //Optionally, you can enqueue messages with transaction style by calling the methods StartQueueTrans and EndQueueTrans in pair
                var fsqt = aq.startQueueTrans(TEST_QUEUE_KEY);
                TestEnqueue(aq);
                //test message batching
                using (CScopeUQueue sb = new CScopeUQueue())
                {
                    CUQueue q = sb.UQueue;
                    CAsyncQueue.BatchMessage(idMessage3, "Hello", "World", q);
                    CAsyncQueue.BatchMessage(idMessage4, true, 234.456, "MyTestWhatever", q);
                    if (!aq.EnqueueBatch(TEST_QUEUE_KEY, q))
                    {
                        throw new CSocketError(CAsyncQueue.SESSION_CLOSED_BEFORE, "Socket already closed before sending the request EnqueueBatch", CAsyncQueue.idEnqueueBatch, true);
                    }
                }
                var feqt = aq.endQueueTrans(false);
                TestDequeue(aq);
                aq.WaitAll();
                //get a queue key two parameters, message count and queue file size by default option oMemoryCached
                var ffq = aq.flushQueue(TEST_QUEUE_KEY);
                var fgk = aq.getKeys();
                var fcq = aq.closeQueue(TEST_QUEUE_KEY);
                Console.WriteLine("StartQueueTrans/res: " + fsqt.Result);
                Console.WriteLine("EndQueueTrans/res: " + feqt.Result);
                Console.WriteLine(ffq.Result);
                int index = 0;
                Console.Write("[");
                string[] keys = fgk.Result;
                foreach (string k in keys)
                {
                    if (index != 0)
                    {
                        Console.Write(",");
                    }
                    Console.Write(k);
                }
                Console.WriteLine("]");
                Console.WriteLine("CloseQueue/res: " + fcq.Result);
            }
            catch (AggregateException ex)
            {
                foreach (Exception e in ex.InnerExceptions)
                {
                    //An exception from server (CServerError), Socket closed after sending a request (CSocketError) or request canceled (CSocketError),
                    Console.WriteLine(e);
                }
            }
            catch (CSocketError ex)
            {
                //Socket is already closed before sending a request
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                //bad operations such as invalid arguments, bad operations and de-serialization errors, and so on
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to close the application ......");
            Console.Read();
        }
    }