Пример #1
0
        public CommunicateChannel(IMessageConnection connection, ConsumerContext consumerContext)
        {
            _connection = connection;

            _consumerContext = consumerContext;

            ConsumerBind(consumerContext);
        }
Пример #2
0
 public MessageContext(
     MessageWrapper queueMessage,
     ConsumerContext consuemrContext,
     ulong deliveryTag)
 {
     MessageWrapper = queueMessage;
     ConsumerContext = consuemrContext;
     DeliveryTag = deliveryTag;
 }
        private ICommunicateChannel CreateChannel(ConsumerContext consumerContext)
        {
            if (!_channelPools.Any() || _channelPools.Last().IsFull())
                _channelPools.Add(new CommunicateChannelFactory(_endpoint));

            var channel = _channelPools.Last().GetChannel(consumerContext);

            return channel;
        }
Пример #4
0
        public ICommunicateChannel GetChannel(ConsumerContext consumerContext)
        {
            CheckConnection();

            if (!_channelPools.ContainsKey(consumerContext.GetConsumerKey()))
            {
                var channel = new CommunicateChannel(_connection, consumerContext);

                _channelPools.TryAdd(consumerContext.GetConsumerKey(), channel);

            }
            return _channelPools[consumerContext.GetConsumerKey()];
        }
Пример #5
0
        public PushMessageConsumer(
            CommunicateChannelFactoryPool channelPools,
            IBinarySerializer binarySerializer,
            ConsumerContext consumerContext,
            IQueueMessageHandler messageHandler)
        {
            _channelPools = channelPools;

            _consumerContext = consumerContext;

            _messageHandler = messageHandler;

            _binarySerializer = binarySerializer;

            _cancellation = new CancellationTokenSource();
        }
Пример #6
0
        private void ConsumerBind(ConsumerContext consumerContext)
        {
            _channel = _connection.CreateChannel();

            _channel.ExchangeDeclare(consumerContext.ExChangeName, consumerContext.ExchangeType, true);

            _channel.QueueDeclare(consumerContext.QueueName, consumerContext.Durable,
                !consumerContext.Durable, !consumerContext.Durable, null);

            _channel.QueueBind(_consumerContext.QueueName, _consumerContext.ExChangeName, consumerContext.RoutingKey);

            _basicConsumer = new QueueingBasicConsumer(_channel);

            _channel.BasicQos(0, 100, true);

            _channel.BasicConsume(consumerContext.QueueName, consumerContext.NoAck, _basicConsumer);
        }
        public ICommunicateChannel GetChannel(ConsumerContext consumerContext)
        {
            var communicateChannel = default(ICommunicateChannel);

            for (var i = 0; i < _channelPools.Count; i++)
            {
                if (_channelPools[i].ContainsChannel(consumerContext))
                {
                    communicateChannel = _channelPools[i].GetChannel(consumerContext);
                    break;
                }
            }

            if (communicateChannel != null) return communicateChannel;

            return CreateChannel(consumerContext);
        }
Пример #8
0
        private static void Main(string[] args)
        {
            var containerBuilder = new AutofacContainerBuilder();

            var binarySerializer = new DefaultBinarySerializer();

            var eventHandleInitializer = new EventHandleProvider();
            eventHandleInitializer.Initialize(Assembly.GetExecutingAssembly(), Assembly.Load("Seven.Tests"));

            var commandInitializer = new CommandHandleProvider();
            commandInitializer.Initialize(Assembly.Load("Seven.Tests"));

            var messageTypeProvider = new MessageTypeProvider();
            messageTypeProvider.Initialize(Assembly.GetExecutingAssembly(), Assembly.Load("Seven.Tests"));

            containerBuilder.RegisterInstance(eventHandleInitializer);
            containerBuilder.RegisterInstance(commandInitializer);
            containerBuilder.RegisterInstance(messageTypeProvider);

            var mysqlEventStore = new MySqlEventStore(_mysqlConnection);

            var snapshotStorage = new MysqlSnapshotStorage(_mysqlConnection);

            var aggregateRootStorage = new MysqlAggregateRootStorage(_mysqlConnection);

            var aggregateRootMemory = new AggregateRootMemoryCache();

            var nonEventSouringRepository = new NonEventSouringRepository(aggregateRootStorage, binarySerializer);

            var eventSouringRepository = new EventSouringRepository(mysqlEventStore, snapshotStorage, binarySerializer,
                aggregateRootMemory);

            var endPoint = new RemoteEndpoint("127.0.0.1", "/", "guest", "guest", 5672);

            var exChangeName = typeof(CreateUserCommand).Assembly.GetName().Name; ;

            var responseRoutingKey = MessageUtils.CurrentResponseRoutingKey;

            var channelPools = new CommunicateChannelFactoryPool(endPoint);

            containerBuilder.RegisterInstance(channelPools);
            containerBuilder.RegisterInterface<IBinarySerializer, DefaultBinarySerializer>();

            DependencyResolver.SetResolver(new DefaultDependencyResolver(containerBuilder.Build()));

            var requestChannelPools = new RequestChannelPools();

            var commandTopicProvider = new UserTopicProvider();

            var eventTopicProvider = new UserEEventTopicProvider();

            var eventPublisher = new EventPublisher(requestChannelPools, eventTopicProvider);

            var commandProssor = new DefaultCommandProssor(mysqlEventStore, eventSouringRepository, commandInitializer, eventPublisher, snapshotStorage, binarySerializer);

            var messageHandler = new MessageRequestHandler(commandProssor);

            for (var i = 0; i < 5; i++)
            {
                var routingKey = string.Format("{0}_{1}_{2}", exChangeName, "command", i);

                var consumerContext = new ConsumerContext(exChangeName, routingKey, routingKey, responseRoutingKey, false, true);

                var consumer = new PushMessageConsumer(channelPools, binarySerializer, consumerContext, messageHandler);

                consumer.Start();

                Console.WriteLine("Started.");
            }

            for (var i = 0; i < 5; i++)
            {
                var routingKey = string.Format("{0}_{1}_{2}", exChangeName, "event", i);

                var consumerContext = new ConsumerContext(exChangeName, routingKey, routingKey, responseRoutingKey, false, true);

                var consumer = new PushMessageConsumer(channelPools, binarySerializer, consumerContext, messageHandler);

                consumer.Start();

                Console.WriteLine("Started.");
            }

            Console.WriteLine("begin to consumer the message.");

            Console.ReadLine();
        }
Пример #9
0
        private static void Main(string[] args)
        {
            var binarySerializer = new DefaultBinarySerializer();

            var hostName = "127.0.0.1";
            var port = 5672;
            var userName = "******";
            var password = "******";
            var virtualName = "/";

            var endPoint = new RemoteEndpoint(hostName, virtualName, userName, password, port);

            var exChangeName = typeof(CreateUserCommand).Assembly.GetName().Name;

            var routingKey = MessageUtils.CurrentResponseRoutingKey;

            var responseRoutingKey = MessageUtils.CurrentResponseRoutingKey;

            var consumerContext = new ConsumerContext(exChangeName, responseRoutingKey, responseRoutingKey, routingKey, true);

            var channelPools = new CommunicateChannelFactoryPool(endPoint);

            var consumer = new PushMessageConsumer(channelPools, binarySerializer, consumerContext, new MessageResponseHandler());

            consumer.Start();

            var containerBuilder = new AutofacContainerBuilder();

            containerBuilder.RegisterInstance(channelPools);
            containerBuilder.RegisterInterface<IBinarySerializer, DefaultBinarySerializer>();

            DependencyResolver.SetResolver(new DefaultDependencyResolver(containerBuilder.Build()));

            var requestChannelPools = new RequestChannelPools();

            var commandTopicProvider = new UserTopicProvider();

            var commandService = new CommandService(requestChannelPools, commandTopicProvider);

            Console.WriteLine("begin to receive the result message");

            Stopwatch watch = new Stopwatch();
            watch.Start();

            for (var i = 0; i < 10000; i++)
            {
                var command = new CreateUserCommand(
                 "天涯狼" + DateTime.Now.ToString("yyyyMMddHHmmsss"),
                 DateTime.Now.ToString("yyyyMMddHHmmsss"),
                 true,
                 22);

                var commandResult = commandService.Send(command, 20);

                Console.WriteLine("message:{0} and number is {1}", commandResult.Message, i);
            }

            watch.Stop();

            Console.WriteLine("message:{0} ", watch.ElapsedMilliseconds);
            Console.ReadLine();
        }
Пример #10
0
 public bool ContainsChannel(ConsumerContext consumerContext)
 {
     return _channelPools.ContainsKey(consumerContext.GetConsumerKey());
 }