Example #1
0
        public void testPersistence()
        {
            IMessagingBus bus = MQFactory.Instance.createMessagingBus();

            IDictionary <string, object> storageProps = new Dictionary <string, object>();

            // For InMemoryDB (It's not HSQLDB!)
            //storageProps.Add("storageName", "MyMemoryStorage");
            //IPersistenceStorage<String> persistStorage =  MQFactory.Instance.createPersistenceStorage<String>("InMemory",storageProps);

            // For SQLite
            //Object obj = Activator.CreateInstance("System.Data.SQLite.SQLiteConnection", "SQLiteConnection");
            //System.Data.SQLite.SQLiteConnection connection = null;
            storageProps.Add("dbAssemblyName", "System.Data.SQLite");
            storageProps.Add("dbConnectionClass", "System.Data.SQLite.SQLiteConnection");
            storageProps.Add("dbConnectionString", "Data Source=test.db3");
            IPersistenceStorage <String> persistStorage = MQFactory.Instance.createPersistenceStorage <String>("SQL", storageProps);

            IMQConnection                     serverConnection = null;
            IMQConnection                     clientConnection = null;
            IMessageQueue <String>            queue            = null;
            IPersistenceQueueStorage <String> queueStorage     = null;

            try {
                serverConnection = bus.create(new Uri("bnmq://127.0.0.1:3333"));
                ISupplier supplier = serverConnection.createSupplier("TestSupplier");
                queueStorage = persistStorage.createQueueStorage("MyQueue");
                queue        = supplier.createQueue <String>("myqueues/queue", queueStorage);
                serverConnection.addListener(new TestMQConnectionListener());
                serverConnection.start();

                clientConnection = bus.connect(new Uri("bnmq://127.0.0.1:3333"));
                clientConnection.addListener(new TestMQConnectionListener());
                clientConnection.start();
                IRemoteSupplier remSupplier           = clientConnection.lookup("TestSupplier");
                IRemoteMessageQueue <String> remQueue = remSupplier.lookupQueue <String>("myqueues/queue");
                remQueue.addConsumer(new TestPersistenceConsumer(), true);
                clientConnection.close();
                clientConnection = null;

                for (int i = 0; i < 10; i++)
                {
                    IMessage <String> mandatoryMessage = queue.createMessage("Mandatory message " + i);
                    mandatoryMessage.Mandatory = true;
                    queue.sendMessage(mandatoryMessage);
                }

                clientConnection = bus.connect(new Uri("bnmq://127.0.0.1:3333"));
                clientConnection.addListener(new TestMQConnectionListener());
                clientConnection.start();
                remSupplier = clientConnection.lookup("TestSupplier");
                remQueue    = remSupplier.lookupQueue <String>("myqueues/queue");
                remQueue.addConsumer(new TestPersistenceConsumer(), true);
                Thread.Sleep(2000);
                //clientConnection.close();
            }
            finally {
                if (queue != null)
                {
                    queue.stop();
                }
                if (queueStorage != null)
                {
                    queueStorage.close();
                }
                if (clientConnection != null)
                {
                    clientConnection.close();
                }
                if (serverConnection != null)
                {
                    serverConnection.close();
                }
                if (bus != null)
                {
                    bus.close();
                }
            }
        }
Example #2
0
        public void start()
        {
            IMessagingBus bus = MQFactory.Instance.createMessagingBus();


            IMQConnection serverConnection = null;
            IMessageQueue <ExampleMessage>            queue        = null;
            IPersistenceQueueStorage <ExampleMessage> queueStorage = null;
            QueueDispatcher dispatcher = null;

            try {
                serverConnection = bus.create(new Uri("bnmq://127.0.0.1:3333"));
                serverConnection.addListener(new MQConnectionListener());
                ISupplier supplier = serverConnection.createSupplier("ExampleSupplier");
                Console.WriteLine("Supplier created successfully");

                IDictionary <String, Object> storProps = new Dictionary <String, Object>();
                storProps.Add("storageName", "MyMemoryStorage");
                // For InMemoryDB (It's not HSQLDB!)
                IPersistenceStorage <ExampleMessage> persistStorage = MQFactory.Instance.createPersistenceStorage <ExampleMessage>("InMemory", storProps);

                queueStorage = persistStorage.createQueueStorage("MyQueue");
                Console.WriteLine("QueueStorage created successfully");

                queue = supplier.createQueue <ExampleMessage>("myqueues/queue", queueStorage);
                Console.WriteLine("MessageQueue created successfully");

                serverConnection.start();
                Console.WriteLine("Listener created successfully");

                dispatcher = new QueueDispatcher(queue);
                dispatcher.start();
                Console.WriteLine("Example queue dispatcher started successfully");

                Console.WriteLine("Please enter to exit");
                Console.ReadKey();
                Console.WriteLine("Trying to stop example queue dispatcher");
                dispatcher.stop();
            }
            catch (Exception e) {
                Console.WriteLine(e.ToString());
            }
            finally {
                if (serverConnection != null)
                {
                    Console.WriteLine("Trying to close listener");
                    serverConnection.close();
                }

                if (queue != null)
                {
                    Console.WriteLine("Trying to stop queue");
                    queue.stop();
                }

                if (queueStorage != null)
                {
                    Console.WriteLine("Trying to close queue storage");
                    queueStorage.close();
                }

                if (bus != null)
                {
                    Console.WriteLine("Trying to finallize messaging bus");
                    bus.close();
                }
            }
        }