Ejemplo n.º 1
0
        public void Send(Envelope env)
        {
            Log.Debug("Enter Send");

            // first, get the topology based on the headers
            RoutingInfo routing = this.GetRoutingFromCacheOrService(_routingInfoCache, _topoSvc, env.Headers);

            // next, pull out all the producer exchanges
            IEnumerable <Exchange> exchanges =
                from route in routing.Routes
                select route.ProducerExchange;

            // for each exchange, send the envelope
            foreach (Exchange ex in exchanges)
            {
                Log.Debug("Sending to exchange: " + ex.ToString());
                IConnection conn = _connFactory.ConnectTo(ex);

                using (IModel channel = conn.CreateModel())
                {
                    IBasicProperties props = channel.CreateBasicProperties();
                    props.Headers = env.Headers as IDictionary;

                    channel.ExchangeDeclare(ex.Name, ex.ExchangeType, ex.IsDurable, ex.IsAutoDelete, ex.Arguments);
                    channel.BasicPublish(ex.Name, ex.RoutingKey, props, env.Payload);

                    // close the channel, but not the connection.  Channels are cheap.
                    channel.Close();
                }
            }

            Log.Debug("Leave Send");
        }
Ejemplo n.º 2
0
        protected RabbitListener createListener(IRegistration registration, Exchange exchange)
        {
            // create a channel
            var connection = _connFactory.ConnectTo(exchange);

            // create a listener
            RabbitListener listener = new RabbitListener(registration, exchange, connection);

            listener.OnEnvelopeReceived += dispatcher =>
            {
                Log.Debug("Got an envelope from the RabbitListener: dispatching.");
                // the dispatcher encapsulates the logic of giving the envelope to handlers
                dispatcher.Dispatch();
            };
            listener.OnClose += _listeners.Remove;

            //TODO: Resolve that RabbitListener does not implement OnConnectionError
            //listener.OnConnectionError(new ReconnectOnConnectionErrorCallback(_channelFactory));

            // store the listener
            _listeners.Add(registration, listener);

            // put it on another thread so as not to block this one
            // don't continue on this thread until we've started listening
            ManualResetEvent startEvent     = new ManualResetEvent(false);
            Thread           listenerThread = new Thread(listener.Start);

            listenerThread.Name = string.Format("{0} on {1}:{2}{3}", exchange.QueueName, exchange.HostName, exchange.Port, exchange.VirtualHost);
            listenerThread.Start(startEvent);

            return(listener);
        }