Example #1
0
        public IMessageBroker Create(ConnectionConfig config)
        {
            string databaseType = config.BrokerType.ToString();
            var    kernel       = CommonConnection.Load();

            return(kernel.Get <IMessageBroker>(databaseType).Setup(config));
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("[{0}] Start Application", DateTime.Now.ToString("hh.mm.ss.ffffff"));

            MessageFactory messageFactory = CommonConnection.Load().Get <MessageFactory>();

            /** connection example **/

            var channel = "secret_channel"; //channel for rabbitmq or topic for kafka

            //subscribe message rabbit
            ConnectionConfig connection            = new ConnectionConfig(MessageBrokerEnum.RabbitMQ, "127.0.0.1", null);
            IMessageBroker   messageBrokerRabbitMq = messageFactory.Create(connection);

            ((ICommonConsumer)messageBrokerRabbitMq.GetConsumer()).OnMessageReceivedEventHandler += OnMessageReceivedEventHandler;

            //subscribe message redis
            ConnectionConfig connectionRedis    = new ConnectionConfig(MessageBrokerEnum.Redis, "127.0.0.1", 6379);
            IMessageBroker   messageBrokerRedis = messageFactory.Create(connectionRedis);

            ((ICommonConsumer)messageBrokerRedis.GetConsumer()).OnMessageReceivedEventHandler += OnMessageReceivedEventHandler;

            //subscribe message kafka
            ConnectionConfig connectionKafka    = new ConnectionConfig(MessageBrokerEnum.Kafka, "127.0.0.1", 9092);
            IMessageBroker   messageBrokerKafka = messageFactory.Create(connectionKafka);

            ((ICommonConsumer)messageBrokerKafka.GetConsumer()).OnMessageReceivedEventHandler += OnMessageReceivedEventHandler;

            messageBrokerKafka.Start(channel);
            messageBrokerRedis.Start(channel);
            messageBrokerRabbitMq.Start(channel);

            /** end of connection example **/


            //publish Message
            RequestMessageData <SomeMessage> objectToSend = new RequestMessageData <SomeMessage>(new SomeMessage(1, "message dikirim", DateTime.Now, new List <string> {
                "ratna", "santi"
            }));                                                                                                                                                                            //some class or object
            bool isPublish = messageBrokerRedis.PublishRequestMessage(channel, objectToSend);

            isPublish &= messageBrokerKafka.PublishRequestMessage <SomeMessage>(channel, objectToSend); //could adding additional key for kafka on third parameter (optional)
            isPublish &= messageBrokerRabbitMq.PublishRequestMessage(channel, objectToSend);

            // example of publish bulk
            //publishBulk(messageBrokerRabbitMq, channel, objectToSend);
            //publishBulk(messageBrokerRedis, channel, objectToSend);
            //publishBulk(messageBrokerKafka, channel, objectToSend);

            Console.WriteLine("after task start");

            Thread.Sleep(10000);
            Console.WriteLine("cancel start");

            messageBrokerRabbitMq.Unsubscribe(channel);
            messageBrokerKafka.Unsubscribe(channel);
            messageBrokerRedis.Unsubscribe(channel);

            Console.WriteLine("cancel finished");
        }