public void TestWithinInvoke() { var connectionFactory = new Mock <Connection.IConnectionFactory>(); var connection = new Mock <Connection.IConnection>(); connectionFactory.Setup((f) => f.CreateConnection()).Returns(connection.Object); var channel1 = new Mock <RC.IModel>(); var channel2 = new Mock <RC.IModel>(); connection.SetupSequence((c) => c.CreateChannel(false)).Returns(channel1.Object).Returns(channel2.Object); var declareOk = new RC.QueueDeclareOk("foo", 0, 0); channel1.Setup((c) => c.QueueDeclare(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >())).Returns(declareOk); var template = new RabbitTemplate(connectionFactory.Object); var admin = new RabbitAdmin(template); template.Invoke <object>((o) => { admin.DeclareQueue(); admin.DeclareQueue(); admin.DeclareQueue(); admin.DeclareQueue(); return(null); }); connection.Verify((c) => c.CreateChannel(false), Times.Once); channel1.Verify((c) => c.QueueDeclare(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >()), Times.Exactly(4)); channel1.Verify((c) => c.Close(), Times.Once); channel2.VerifyNoOtherCalls(); }
public RabbitQueue(IModel model, IRabbitSerializer serializer, QueueDeclareOk result, QueueOptions queueOptions) { _model = model; _defaultSerializer = serializer ?? queueOptions.Serializer; _queueOptions = queueOptions; this.Name = result.QueueName; this.ConsumerCount = result.ConsumerCount; this.MessageCount = result.MessageCount; }
public LauncherServerSendRequestToTShock() { channel = OverallInformations.conn.CreateModel(); channel.ExchangeDeclare(exchange: OverallInformations.LauncherServerTShockExchange, type: "direct", durable: false, autoDelete: true, arguments: null); replyQueueOk = channel.QueueDeclare(); channel.QueueBind(queue: replyQueueOk.QueueName, exchange: OverallInformations.LauncherServerTShockExchange, routingKey: replyQueueOk.QueueName, arguments: null); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); consumer = new EventingBasicConsumer(channel); consumer.Received += (model, _ea) => { ea = _ea; LauncherReadyEvent.Set(); }; channel.BasicConsume(queue: replyQueueOk.QueueName, noAck: true, consumer: consumer); }
public void Start(Action<object> onMessage) { Stop(); _connection = _factory.CreateConnection(); _channel = _connection.CreateModel(); DeclareExchangeIfRequired(); var consumer = CreateConsumer(onMessage); _queueName = _channel.QueueDeclare(); _channel.QueueBind(_queueName, _options.ExchangeName, _options.RoutingKey); _channel.BasicConsume(_queueName, false, consumer); _hook(); }
private void ReceiveChat() { ChannelToReceive = OverallInformations.conn.CreateModel(); ChannelToReceive.ExchangeDeclare(exchange: OverallInformations.TShockChatBroadcastExchange, type: "fanout"); ToReceiveQueue = ChannelToReceive.QueueDeclare(); ChannelToReceive.QueueBind(queue: ToReceiveQueue.QueueName, exchange: OverallInformations.TShockChatBroadcastExchange, routingKey: "", arguments: null); ChannelToReceive.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); EventingBasicConsumer ReceiveConsumer = new EventingBasicConsumer(ChannelToReceive); ReceiveConsumer.Received += Consumer_Received; ChannelToReceive.BasicConsume(queue: ToReceiveQueue.QueueName, noAck: true, consumer: ReceiveConsumer); ChannelToSend = OverallInformations.conn.CreateModel(); ChannelToSend.ExchangeDeclare(exchange: OverallInformations.LauncherServerClientExchange, type: "direct", durable: false, autoDelete: true, arguments: null); ToSendQueue = ChannelToSend.QueueDeclare(); ChannelToSend.QueueBind(queue: ToSendQueue.QueueName, exchange: OverallInformations.LauncherServerClientExchange, routingKey: ToSendQueue.QueueName, arguments: null); ChannelToSend.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); EventingBasicConsumer SendComsumer = new EventingBasicConsumer(ChannelToSend); SendComsumer.Received += SendComsumer_Received; ChannelToSend.BasicConsume(queue: ToSendQueue.QueueName, noAck: true, consumer: SendComsumer); }
public void Listen() { ConnectionFactory factory = new ConnectionFactory(); factory.HostName = _exchange.Hostname; factory.VirtualHost = _exchange.VirtualHost; factory.Port = AmqpTcpEndpoint.UseDefaultPort; _connection = factory.CreateConnection(); _channel = _connection.CreateModel(); // declare the exchange _channel.ExchangeDeclare(_exchange.Name, ExchangeType.Topic, true); // declare the queue _queue = _channel.QueueDeclare(_queueName, false, true, false, null); // create a basic queueing consumer QueueingBasicConsumer consumer = new QueueingBasicConsumer(_channel); String consumerTag = _channel.BasicConsume(_queue.QueueName, false, consumer); // let anyone interested know that we've started this.MarkAsStarted(); // and consume until we've been disposed while (!_isDisposing) { while (_bindings.Count > 0) { try { BasicDeliverEventArgs e = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); IBasicProperties props = e.BasicProperties; // create an amqp envelope out of the byte array AmqpEnvelope env = new AmqpEnvelope(e.Body); // do some receipt-specific stuff this.LogAndSetReceiptHeaders(env, props); // raise the envelope to interested clients this.Raise_EnvelopeReceived_Event(env); // wait for the envelope to be marked as processed before getting the next message env.WaitToBeProcessed(); // we may want the WaitToBeProcessed method to return a completion type // one day so that we can do smarter things than just "ack", for example, // "nack'ing" events we failed to process. _channel.BasicAck(e.DeliveryTag, false); } catch (EndOfStreamException) { // this happens when we get disconnected somehow. Typically, this // happens when the process is shutting down. Let's log this and // not freak out about it. LOG.InfoFormat("Rabbit Queue {0} is no longer listening for new events", _queueName); break; } catch (Exception ex) { // The consumer was removed, either through // channel or connection closure, or through the // action of IModel.BasicCancel(). LOG.Error(string.Format( "The Rabbit queue {0} encountered an exception attempting to consume a message.", _queueName ), ex); break; } } if (_bindings.Count == 0) { // avoid a hard-loop Thread.Sleep(100); } } }
public void ShouldCallQueueDeclareWithParametersWhenPubSubTypeIsSubscribeAndRoutingKeyIsSet() { const string queueName = "abc"; var expectedResult = new QueueDeclareOk(queueName, 1, 1); _endpoint = new QueueEndpoint() { Name = "testendpoint", Exchange = new ExchangeConfiguration() { Name = "ex1" }, PubSubType = PubSubType.Subscribe, RoutingKey = queueName, Subscription = new SubscriptionConfiguration() { QueueName = "abckduf", NoAck = false } }; _endpoint.Subscription.ExchangeBindings.Add(new ExchangeBinding() { Name = "e1" }); _channel .Stub(x => x.QueueDeclare()) .Return(new QueueDeclareOk(queueName, 1, 1)); _channel .Stub(x => x.QueueDeclare(_endpoint.Subscription.QueueName, _endpoint.Subscription.NoAck, false, false, null)) .Return(expectedResult); var configurator = new ChannelConfigurator(); configurator.ConfigureQueue(_endpoint, _channel); _channel.AssertWasCalled(x => x.QueueDeclare(_endpoint.Subscription.QueueName, _endpoint.Subscription.Durable, false, false, null)); _channel.AssertWasCalled(x => x.QueueBind(expectedResult, _endpoint.Subscription.ExchangeBindings[0].Name, "")); }
public void ShouldThrowExceptionIfPubSubTypeIsSubscribeAndNoSubscriptionConfigExists() { const string queueName = "abc"; var expectedResult = new QueueDeclareOk(queueName, 1, 1); _endpoint = new QueueEndpoint() { Name = "testendpoint", Exchange = new ExchangeConfiguration() { Name = "ex1" }, PubSubType = PubSubType.Subscribe, RoutingKey = queueName, Subscription = null }; var configurator = new ChannelConfigurator(); configurator.ConfigureQueue(_endpoint, _channel); }
static void Main(string[] args) { if (!File.Exists("config.json")) { Console.WriteLine("Error: config.json not found!"); return; } config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json")); amqpFactory = new ConnectionFactory { Uri = config.AmqpURI, AutomaticRecoveryEnabled = true, TopologyRecoveryEnabled = true }; using (amqpConnection = amqpFactory.CreateConnection()) { using (amqpChannel = amqpConnection.CreateModel()) { amqpMsgProps = amqpChannel.CreateBasicProperties(); amqpMsgProps.Persistent = true; amqpQueue = amqpChannel.QueueDeclare(); amqpChannel.QueueBind(amqpQueue.QueueName, config.AmqpExchange, "*.message"); var consumer = new EventingBasicConsumer(amqpChannel); consumer.Received += (_, amqpMessage) => { if (inChat && amqpMessage.RoutingKey != "steam.message") { var jsonStr = Encoding.UTF8.GetString(amqpMessage.Body); var message = JsonConvert.DeserializeObject<BridgeMessage>(jsonStr); Console.WriteLine( "[>] {0}: [{1}] {2}", config.SteamName, message.Sender, message.Content ); steamFriends.SendChatRoomMessage( config.SteamChat, EChatEntryType.ChatMsg, string.Format("[{0}] {1}", message.Sender, message.Content) ); } }; amqpChannel.BasicConsume(amqpQueue.QueueName, true, consumer); steamClient = new SteamClient(); steamUser = steamClient.GetHandler<SteamUser>(); steamFriends = steamClient.GetHandler<SteamFriends>(); var manager = new CallbackManager(steamClient); manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected); manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected); manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn); manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff); manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnUpdateMachineAuth); manager.Subscribe<SteamFriends.ChatEnterCallback>(OnChatEnter); manager.Subscribe<SteamFriends.ChatMsgCallback>(OnChatMsg); manager.Subscribe<SteamFriends.ChatMemberInfoCallback>(OnChatMemberInfo); steamClient.Connect(); while (true) { manager.RunWaitCallbacks(); } } } }