private Boolean createTopicListener(string topicName) { if (connected) { String localTopicName = "position." + topicName; IDestination topic = session.GetTopic(localTopicName); // create a consumer IMessageConsumer consumer = session.CreateConsumer(topic); consumer.Listener += new MessageListener(this.OnlineMessage); // connect to the 'command' queue and issue a request for batch positions // need to send a conduit so that we can receive the data IQueue commandQueue = session.GetQueue("position.Command"); IMessageProducer commandProducer = session.CreateProducer(commandQueue); ITemporaryQueue responseQueue = session.CreateTemporaryQueue(); batchConsumer = session.CreateConsumer(responseQueue); batchConsumer.Listener += this.StartupMessageLoad; ITextMessage textCommandMessage = session.CreateTextMessage("SendBatchAndOnline account=" + topicName); textCommandMessage.NMSReplyTo = responseQueue; commandProducer.Send(textCommandMessage); return(true); } else { return(false); } }
// [RowTest] // [Row(AcknowledgementMode.AutoAcknowledge)] // [Row(AcknowledgementMode.ClientAcknowledge)] // [Row(AcknowledgementMode.DupsOkAcknowledge)] // [Row(AcknowledgementMode.Transactional)] public void TestConsumerReceiveBeforeMessageDispatched(AcknowledgementMode ackMode) { // Launch a thread to perform a delayed send. Thread sendThread = new Thread(DelayedProducerThreadProc); using (IConnection connection = CreateConnection(TEST_CLIENT_ID + ":" + new Random().Next())) { connection.Start(); using (ISession session = connection.CreateSession(ackMode)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); using (IMessageConsumer consumer = session.CreateConsumer(queue)) { ThreadArg arg = new ThreadArg(); arg.connection = connection; arg.session = session; arg.destination = queue; sendThread.Start(arg); IMessage message = consumer.Receive(TimeSpan.FromMinutes(1)); Assert.IsNotNull(message); } } } }
//[Test] public virtual void TestTempDestOnlyConsumedByLocalConn() { connection.Start(); ISession tempSession = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = tempSession.CreateTemporaryQueue(); IMessageProducer producer = tempSession.CreateProducer(queue); producer.DeliveryMode = (MsgDeliveryMode.NonPersistent); ITextMessage message = tempSession.CreateTextMessage("First"); producer.Send(message); // temp destination should not be consume when using another connection IConnection otherConnection = CreateConnection(); connections.Add(otherConnection); ISession otherSession = otherConnection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue otherQueue = otherSession.CreateTemporaryQueue(); IMessageConsumer consumer = otherSession.CreateConsumer(otherQueue); IMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(3000)); Assert.IsNull(msg); // should be able to consume temp destination from the same connection consumer = tempSession.CreateConsumer(queue); msg = consumer.Receive(TimeSpan.FromMilliseconds(3000)); Assert.IsNotNull(msg); }
//[Test] public virtual void TestTemporaryQueueAsynchronousConsume( //[Values(MsgDeliveryMode.Persistent, MsgDeliveryMode.NonPersistent)] MsgDeliveryMode deliveryMode, string testDestRef) { using (IConnection connection = CreateConnectionAndStart(GetTestClientId())) using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) using (ITemporaryQueue tempReplyDestination = session.CreateTemporaryQueue()) using (IDestination destination = GetClearDestinationByNodeReference(session, testDestRef)) using (IMessageConsumer consumer = session.CreateConsumer(destination)) using (IMessageConsumer tempConsumer = session.CreateConsumer(tempReplyDestination)) using (IMessageProducer producer = session.CreateProducer(destination)) { producer.DeliveryMode = deliveryMode; tempConsumer.Listener += new MessageListener(OnMessage); consumer.Listener += new MessageListener(OnQueueMessage); IMessage request = session.CreateMessage(); request.NMSCorrelationID = "TemqQueueAsyncConsume"; request.NMSType = "Test"; request.NMSReplyTo = tempReplyDestination; producer.Send(request); WaitForMessageToArrive(); Assert.AreEqual("TempQueueAsyncResponse", receivedMsg.NMSCorrelationID, "Invalid correlation ID."); } }
public void TestUnAckedMessageAreNotConsumedOnSessionClose() { ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); producer.Send(session.CreateTextMessage("Hello")); // Consume the message... IMessageConsumer consumer = session.CreateConsumer(queue); IMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(1000)); Assert.IsNotNull(msg); // Don't ack the message. // Reset the session. This should cause the unacknowledged message to be re-delivered. session.Close(); session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); // Attempt to Consume the message... consumer = session.CreateConsumer(queue); msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsNotNull(msg); msg.Acknowledge(); session.Close(); }
public void TestManyMessageAckedAfterMessageConsumption() { int messageCount = 20; IMessage msg; ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); for(int i = 0; i < messageCount; i++) { msg = session.CreateTextMessage("msg" + i); producer.Send(msg); } // Consume the message... IMessageConsumer consumer = session.CreateConsumer(queue); for(int i = 0; i < messageCount; i++) { msg = consumer.Receive(TimeSpan.FromMilliseconds(1000)); Assert.IsNotNull(msg); msg.Acknowledge(); } msg = consumer.Receive(TimeSpan.FromMilliseconds(1000)); Assert.IsNull(msg); // Reset the session. session.Close(); session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); // Attempt to Consume the message... consumer = session.CreateConsumer(queue); msg = consumer.Receive(TimeSpan.FromMilliseconds(1000)); Assert.IsNull(msg); session.Close(); }
/// <summary> /// Connect to a server and start sending Keepalive messages /// on an internally used connection event topic. /// </summary> /// <param name="url">The server adress</param> public static void Connect(string url) { if (IsConnected) { return; } IConnectionFactory connectionFactory = new ConnectionFactory(url); connection = connectionFactory.CreateConnection(); connection.Start(); mainSession = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); localTempQueue = mainSession.CreateTemporaryQueue(); serverMessageConsumer = mainSession.CreateConsumer(localTempQueue); serverMessageConsumer.Listener += DispatchMessageToChannel; ClientID = localTempQueue.ToString(); connectionTopic = new ActiveMQTopic(CONNECTION_TOPIC); connectionMessageProducer = mainSession.CreateProducer(connectionTopic); allComChannels = new Dictionary <string, ComChannel>(); SendConnectionEventMessage(CONNECT); StartKeepaliveMessageThread(); if (!IsConnected) { Console.Error.WriteLine("[NetworkService] failed to connect."); Close(); } }
public void TestTmpQueueWorksUnderLoad() { int count = 500; int dataSize = 1024; ArrayList list = new ArrayList(count); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; byte[] data = new byte[dataSize]; for (int i = 0; i < count; i++) { IBytesMessage message = session.CreateBytesMessage(); message.WriteBytes(data); message.Properties.SetInt("c", i); producer.Send(message); list.Add(message); } connection.Start(); IMessageConsumer consumer = session.CreateConsumer(queue); for (int i = 0; i < count; i++) { IMessage message2 = consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsTrue(message2 != null); Assert.AreEqual(i, message2.Properties.GetInt("c")); Assert.IsTrue(message2.Equals(list[i])); } }
public void TestNoTimeoutConsumer( [Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge, AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)] AcknowledgementMode ackMode) { // Launch a thread to perform IMessageConsumer.Receive(). // If it doesn't fail in less than three seconds, no exception was thrown. Thread receiveThread = new Thread(new ThreadStart(TimeoutConsumerThreadProc)); using (IConnection connection = CreateConnection()) { connection.Start(); using (ISession session = connection.CreateSession(ackMode)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); using (this.timeoutConsumer = session.CreateConsumer(queue)) { receiveThread.Start(); if (receiveThread.Join(3000)) { Assert.Fail("IMessageConsumer.Receive() returned without blocking. Test failed."); } else { // Kill the thread - otherwise it'll sit in Receive() until a message arrives. receiveThread.Interrupt(); } } } } }
public void TestConsumerReceiveBeforeMessageDispatched( [Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge, AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)] AcknowledgementMode ackMode) { // Launch a thread to perform a delayed send. Thread sendThread = new Thread(DelayedProducerThreadProc); using (IConnection connection = CreateConnection()) { connection.Start(); using (ISession session = connection.CreateSession(ackMode)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); using (IMessageConsumer consumer = session.CreateConsumer(queue)) { ThreadArg arg = new ThreadArg(); arg.connection = connection; arg.session = session; arg.destination = queue; sendThread.Start(arg); IMessage message = consumer.Receive(TimeSpan.FromMinutes(1)); Assert.IsNotNull(message); } } } }
public async Task TestCantDeleteTemporaryQueueWithConsumers() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); string dynamicAddress = "myTempQueueAddress"; testPeer.ExpectTempQueueCreationAttach(dynamicAddress); ITemporaryQueue temporaryQueue = await session.CreateTemporaryQueueAsync(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlow(); IMessageConsumer consumer = await session.CreateConsumerAsync(temporaryQueue); Assert.CatchAsync <IllegalStateException>(async() => await temporaryQueue.DeleteAsync(), "should not be able to delete temporary queue with active consumers"); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); await consumer.CloseAsync(); // Now it should be allowed testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); await temporaryQueue.DeleteAsync(); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
protected override IMessage CreateMessage() { ITextMessage message = Session.CreateTextMessage(expectedText); replyTo = Session.CreateTemporaryQueue(); // lets set the headers message.NMSCorrelationID = correlationID; message.NMSReplyTo = replyTo; message.NMSPersistent = persistent; message.NMSPriority = priority; message.NMSType = type; message.Properties["NMSXGroupID"] = groupID; message.Properties["NMSXGroupSeq"] = groupSeq; // lets set the custom headers message.Properties["customText"] = customText; message.Properties["custom1"] = custom1; message.Properties["custom2"] = custom2; message.Properties["custom3"] = custom3; message.Properties["custom4"] = custom4; message.Properties["custom5"] = custom5; message.Properties["custom6"] = custom6; message.Properties["custom7"] = custom7; message.Properties["custom8"] = custom8; return message; }
public void TestTemporaryQueueAsynchronousConsume(MsgDeliveryMode deliveryMode) { using (IConnection connection = CreateConnection(TEST_CLIENT_ID)) { connection.Start(); using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { IDestination destination = SessionUtil.GetDestination(session, DESTINATION_NAME); ITemporaryQueue tempReplyDestination = session.CreateTemporaryQueue(); using (IMessageConsumer consumer = session.CreateConsumer(destination)) using (IMessageConsumer tempConsumer = session.CreateConsumer(tempReplyDestination)) using (IMessageProducer producer = session.CreateProducer(destination)) { producer.DeliveryMode = deliveryMode; producer.RequestTimeout = receiveTimeout; tempConsumer.Listener += new MessageListener(OnMessage); consumer.Listener += new MessageListener(OnQueueMessage); IMessage request = session.CreateMessage(); request.NMSCorrelationID = "TemqQueueAsyncConsume"; request.NMSType = "Test"; request.NMSReplyTo = tempReplyDestination; producer.Send(request); WaitForMessageToArrive(); Assert.AreEqual("TempQueueAsyncResponse", receivedMsg.NMSCorrelationID, "Invalid correlation ID."); } } } }
public void TestSyncReceiveConsumerClose(AcknowledgementMode ackMode) { // Launch a thread to perform IMessageConsumer.Receive(). // If it doesn't fail in less than three seconds, no exception was thrown. Thread receiveThread = new Thread(new ThreadStart(TimeoutConsumerThreadProc)); using (IConnection connection = CreateConnection(TEST_CLIENT_ID)) { connection.Start(); using (ISession session = connection.CreateSession(ackMode)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); using (this.timeoutConsumer = session.CreateConsumer(queue)) { receiveThread.Start(); if (receiveThread.Join(3000)) { Assert.Fail("IMessageConsumer.Receive() returned without blocking. Test failed."); } else { // Kill the thread - otherwise it'll sit in Receive() until a message arrives. this.timeoutConsumer.Close(); receiveThread.Join(10000); if (receiveThread.IsAlive) { // Kill the thread - otherwise it'll sit in Receive() until a message arrives. receiveThread.Interrupt(); Assert.Fail("IMessageConsumer.Receive() thread is still alive, Close should have killed it."); } } } } } }
//[Test] public virtual void TestDoChangeSentMessage( //[Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge, // AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)] AcknowledgementMode ackMode, //[Values(true, false)] bool doClear) { using (IConnection connection = CreateConnection()) { connection.Start(); using (ISession session = connection.CreateSession(ackMode)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); using (IMessageConsumer consumer = session.CreateConsumer(queue)) { IMessageProducer producer = session.CreateProducer(queue); ITextMessage message = session.CreateTextMessage(); string prefix = "ConsumerTest - TestDoChangeSentMessage: "; for (int i = 0; i < COUNT; i++) { message.Properties[VALUE_NAME] = i; message.Text = prefix + Convert.ToString(i); producer.Send(message); if (doClear) { message.ClearBody(); message.ClearProperties(); } } if (ackMode == AcknowledgementMode.Transactional) { session.Commit(); } for (int i = 0; i < COUNT; i++) { ITextMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage; Assert.AreEqual(msg.Text, prefix + Convert.ToString(i)); Assert.AreEqual(msg.Properties.GetInt(VALUE_NAME), i); } if (ackMode == AcknowledgementMode.Transactional) { session.Commit(); } } } } }
public ClientResultStream(OpenGammaFudgeContext fudgeContext, MQTemplate mqTemplate, bool checkSeqNumber) { _mqTemplate = mqTemplate; _fudgeMessageDecoder = new FudgeMessageDecoder(fudgeContext, checkSeqNumber); _connection = _mqTemplate.CreateConnection(); _session = _connection.CreateSession(); _destination = _session.CreateTemporaryQueue(); _consumer = _session.CreateConsumer(_destination); _consumer.Listener += RawMessageReceived; _connection.Start(); }
public void TestQueueRollbackConsumerListener() { connection.Start(); this.session = connection.CreateSession(AcknowledgementMode.Transactional); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); IMessage message = session.CreateTextMessage("Test Message"); producer.Send(message); session.Commit(); IMessageConsumer consumer = session.CreateConsumer(queue); consumer.Listener += new MessageListener(OnMessageListener); Thread.Sleep(500); // first try.. should get 2 since there is no delay on the // first redeliver.. Assert.AreEqual(2, counter); Thread.Sleep(1000); // 2nd redeliver (redelivery after 1 sec) Assert.AreEqual(3, counter); Thread.Sleep(2000); // 3rd redeliver (redelivery after 2 seconds) - it should give up after // that Assert.AreEqual(4, counter); // create new message producer.Send(session.CreateTextMessage("Test Message Again")); session.Commit(); Thread.Sleep(500); // it should be committed, so no redelivery Assert.AreEqual(5, counter); Thread.Sleep(1500); // no redelivery, counter should still be 5 Assert.AreEqual(5, counter); session.Close(); }
public void ActiveMQInvernadero() { String user = env("ACTIVEMQ_USER", "admin"); String password = env("ACTIVEMQ_PASSWORD", "password"); String host = env("ACTIVEMQ_HOST", "localhost"); int port = Int32.Parse(env("ACTIVEMQ_PORT", "61616")); String destinationQueueTemp = "Temperatura"; String destinationQueueHum = "Humedad"; String destinationQueueConf = "Configuracion"; this.responseBuffer = new Dictionary <string, AsyncMessageHelper>(); String brokerUri = "activemq:tcp://" + host + ":" + port; var connectionFactory = new ConnectionFactory(brokerUri); try { connection = connectionFactory.CreateConnection(); connection.Start(); this.session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); var destinationTemp = session.GetDestination(destinationQueueTemp); var destinationHum = session.GetDestination(destinationQueueHum); var destinationConf = session.GetDestination(destinationQueueConf); this.producerTemp = session.CreateProducer(destinationTemp); this.producerTemp.DeliveryMode = MsgDeliveryMode.NonPersistent; this.producerHum = session.CreateProducer(destinationHum); this.producerHum.DeliveryMode = MsgDeliveryMode.NonPersistent; this.producerConf = session.CreateProducer(destinationConf); this.producerConf.DeliveryMode = MsgDeliveryMode.NonPersistent; this.temporaryQueueHum = session.CreateTemporaryQueue(); this.temporaryQueueTemp = session.CreateTemporaryQueue(); this.temporaryQueueConf = session.CreateTemporaryQueue(); var responseConsumerTemp = session.CreateConsumer(temporaryQueueTemp); var responseConsumerHum = session.CreateConsumer(temporaryQueueHum); var responseConsumerConf = session.CreateConsumer(temporaryQueueConf); responseConsumerTemp.Listener += new MessageListener(responseConsumer_ListenerTemp); responseConsumerHum.Listener += new MessageListener(responseConsumer_ListenerHum); responseConsumerConf.Listener += new MessageListener(responseConsumer_ListenerConf); } catch (Exception ex) { } }
public void TestTmpQueueWorksUnderLoad() { int count = 500; int dataSize = 1024; ArrayList list = new ArrayList(count); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); IBytesMessage message; IBytesMessage message2; IMessageProducer producer = session.CreateProducer(queue); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; byte[] srcdata = new byte[dataSize]; srcdata[0] = (byte)'B'; srcdata[1] = (byte)'A'; srcdata[2] = (byte)'D'; srcdata[3] = (byte)'W'; srcdata[4] = (byte)'O'; srcdata[5] = (byte)'L'; srcdata[6] = (byte)'F'; for (int i = 0; i < count; i++) { message = session.CreateBytesMessage(); message.WriteBytes(srcdata); message.Properties.SetInt("c", i); producer.Send(message); list.Add(message); } connection.Start(); byte[] data = new byte[dataSize]; byte[] data2 = new byte[dataSize]; IMessageConsumer consumer = session.CreateConsumer(queue); for (int i = 0; i < count; i++) { message2 = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as IBytesMessage; Assert.IsNotNull(message2); Assert.AreEqual(i, message2.Properties.GetInt("c")); message = list[i] as IBytesMessage; Assert.IsNotNull(message); message.Reset(); message.ReadBytes(data); message2.ReadBytes(data2); Assert.AreEqual(data, data2); } }
/// <summary> /// Disconnect and close all network resources without sending a disconnect event message. /// </summary> public static void Close() { //lock (allComChannels) { // if (allComChannels != null) { // foreach (ComChannel channel in allComChannels.Values) { // Console.WriteLine(channel.channelID); // channel._CloseInternal(); // } // allComChannels = null; // } //} if (connectionMessageProducer != null) { connectionMessageProducer.Close(); connectionMessageProducer = null; } if (serverMessageConsumer != null) { serverMessageConsumer.Close(); serverMessageConsumer = null; } if (localTempQueue != null) { localTempQueue.Dispose(); localTempQueue = null; } if (connectionTopic != null) { connectionTopic.Dispose(); connectionTopic = null; } if (mainSession != null) { mainSession.Close(); mainSession = null; } if (connection != null) { connection.Close(); connection = null; } }
public void InvalidSelectorExceptionTest() { using (IConnection connection = CreateConnection()) using (ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); try { session.CreateConsumer(queue, "3+5"); Assert.Fail("Should throw an InvalidSelectorException"); } catch (InvalidSelectorException) { } } }
public void TestDoChangeSentMessage(AcknowledgementMode ackMode) { using (IConnection connection = CreateConnection(TEST_CLIENT_ID)) { connection.Start(); using (ISession session = connection.CreateSession(ackMode)) { ITemporaryQueue queue = session.CreateTemporaryQueue(); using (IMessageConsumer consumer = session.CreateConsumer(queue)) { IMessageProducer producer = session.CreateProducer(queue); ITextMessage message = session.CreateTextMessage(); string prefix = "ConsumerTest - TestDoChangeSentMessage: "; for (int i = 0; i < COUNT; i++) { message.Properties[VALUE_NAME] = i; message.Text = prefix + Convert.ToString(i); producer.Send(message); message.ClearBody(); message.ClearProperties(); } if (ackMode == AcknowledgementMode.Transactional) { session.Commit(); } for (int i = 0; i < COUNT; i++) { ITextMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage; Assert.AreEqual(msg.Text, prefix + Convert.ToString(i)); Assert.AreEqual(msg.Properties[VALUE_NAME], i); } if (ackMode == AcknowledgementMode.Transactional) { session.Commit(); } } } } }
public void TestIndividualAcknowledgeMultiMessages_AcknowledgeFirstTest() { ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); // Push 2 messages to queue ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); ITextMessage msg = session.CreateTextMessage("test 1"); producer.Send(msg, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue); msg = session.CreateTextMessage("test 2"); producer.Send(msg, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue); producer.Close(); IMessageConsumer consumer = session.CreateConsumer(queue); // Read the first message ITextMessage fetchedMessage1 = (ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsNotNull(fetchedMessage1); Assert.AreEqual("test 1", fetchedMessage1.Text); // Read the second message ITextMessage fetchedMessage2 = (ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsNotNull(fetchedMessage2); Assert.AreEqual("test 2", fetchedMessage2.Text); // Acknowledge first message fetchedMessage1.Acknowledge(); consumer.Close(); // Read first message a second time consumer = session.CreateConsumer(queue); fetchedMessage1 = (ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsNotNull(fetchedMessage1); Assert.AreEqual("test 2", fetchedMessage1.Text); // Try to read second message a second time fetchedMessage2 = (ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsNull(fetchedMessage2); consumer.Close(); }
public void TestCreateTemporaryQueue() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.RegisterLinkProcessor(new TestLinkProcessor()); testAmqpPeer.Open(); IConnection connection = EstablishConnection(); connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); session.CreateConsumer(queue); connection.Close(); } }
public void TestLastMessageAcked() { ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); ITextMessage msg1 = session.CreateTextMessage("msg1" + Guid.NewGuid().ToString()); ITextMessage msg2 = session.CreateTextMessage("msg2" + Guid.NewGuid().ToString()); ITextMessage msg3 = session.CreateTextMessage("msg3" + Guid.NewGuid().ToString()); producer.Send(msg1); producer.Send(msg2); producer.Send(msg3); // Consume the message... IMessageConsumer consumer = session.CreateConsumer(queue); ITextMessage ackmsg = consumer.Receive(TimeSpan.FromMilliseconds(1000)) as ITextMessage; Assert.IsNotNull(ackmsg); Assert.AreEqual(msg1.Text, ackmsg.Text); ackmsg = consumer.Receive(TimeSpan.FromMilliseconds(1000)) as ITextMessage; Assert.IsNotNull(ackmsg); Assert.AreEqual(msg2.Text, ackmsg.Text); ackmsg = consumer.Receive(TimeSpan.FromMilliseconds(1000)) as ITextMessage; Assert.IsNotNull(ackmsg); Assert.AreEqual(msg3.Text, ackmsg.Text); ackmsg.Acknowledge(); // Reset the session. session.Close(); session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); // Attempt to Consume the message... consumer = session.CreateConsumer(queue); ackmsg = consumer.Receive(TimeSpan.FromMilliseconds(1000)) as ITextMessage; Assert.IsNotNull(ackmsg); Assert.AreEqual(msg1.Text, ackmsg.Text); ackmsg = consumer.Receive(TimeSpan.FromMilliseconds(1000)) as ITextMessage; Assert.IsNotNull(ackmsg); Assert.AreEqual(msg2.Text, ackmsg.Text); IMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(1000)); Assert.IsNull(msg); session.Close(); }
public void TestTempQueueHoldsMessagesWithoutConsumers() { ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; ITextMessage message = session.CreateTextMessage("Hello"); producer.Send(message); connection.Start(); IMessageConsumer consumer = session.CreateConsumer(queue); IMessage message2 = consumer.Receive(TimeSpan.FromMilliseconds(3000)); Assert.IsNotNull(message2); Assert.IsTrue(message2 is ITextMessage, "Expected message to be a ITextMessage"); Assert.IsTrue(((ITextMessage)message2).Text == message.Text, "Expected message to be a '" + message.Text + "'"); }
public void TestDeleteDestinationWithSubscribersFails() { Connection connection = GetNewConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = session.CreateTemporaryQueue(); connection.Start(); session.CreateConsumer(queue); try { queue.Delete(); Assert.Fail("Should fail as Subscribers are active"); } catch (NMSException) { } }
public void SendReceiveNMSProperties(MsgDeliveryMode deliveryMode) { using (IConnection connection = CreateConnection(TEST_CLIENT_ID)) { connection.Start(); using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { IDestination destination = SessionUtil.GetDestination(session, DESTINATION_NAME); using (IMessageConsumer consumer = session.CreateConsumer(destination)) using (IMessageProducer producer = session.CreateProducer(destination)) { producer.Priority = priority; producer.DeliveryMode = deliveryMode; producer.RequestTimeout = receiveTimeout; ITextMessage request = session.CreateTextMessage(expectedText); ITemporaryQueue replyTo = session.CreateTemporaryQueue(); // Set the headers request.NMSCorrelationID = correlationID; request.NMSReplyTo = replyTo; request.NMSType = type; request.Properties["NMSXGroupID"] = groupID; request.Properties["NMSXGroupSeq"] = groupSeq; producer.Send(request); ITextMessage message = consumer.Receive(receiveTimeout) as ITextMessage; Assert.IsNotNull(message, "Did not receive an ITextMessage!"); Assert.AreEqual(expectedText, message.Text, "Message text does not match."); // compare standard NMS headers Assert.AreEqual(correlationID, message.NMSCorrelationID, "NMSCorrelationID does not match"); Assert.AreEqual(deliveryMode, message.NMSDeliveryMode, "NMSDeliveryMode does not match"); Assert.AreEqual(priority, message.NMSPriority, "NMSPriority does not match"); Assert.AreEqual(type, message.NMSType, "NMSType does not match"); Assert.AreEqual(groupID, message.Properties["NMSXGroupID"], "NMSXGroupID does not match"); Assert.AreEqual(groupSeq, message.Properties["NMSXGroupSeq"], "NMSXGroupSeq does not match"); Assert.AreEqual(replyTo, message.NMSReplyTo, "NMSReplyTo does not match"); } } } }
public void TestTempQueueHoldsMessagesWithConsumers() { using (IConnection connection = CreateConnection()) { using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { connection.Start(); ITemporaryQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); producer.DeliveryMode = (MsgDeliveryMode.NonPersistent); for (int correlationCount = 0; correlationCount < 100; correlationCount++) { string correlationId = string.Format("CID_{0}", correlationCount); IMessageConsumer consumer = null; try { ITextMessage message = session.CreateTextMessage("Hello"); consumer = session.CreateConsumer(queue, string.Format("JMSCorrelationID = '{0}'", correlationId)); message.NMSCorrelationID = correlationId; producer.Send(message); IMessage message2 = consumer.Receive(TimeSpan.FromMilliseconds(1000)); Assert.IsNotNull(message2); Assert.AreEqual(correlationId, message2.NMSCorrelationID, "Received the wrong correlation ID message."); Assert.IsInstanceOf(typeof(ITextMessage), message2); Assert.AreEqual(((ITextMessage)message2).Text, message.Text); } finally { if (null != consumer) { consumer.Close(); } } } } } }
public void TestPublishFailsForClosedConnection() { Connection connection = GetNewConnection(); Connection tempConnection = GetNewConnection(); ISession tempSession = tempConnection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = tempSession.CreateTemporaryQueue(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); connection.Start(); IMessageConsumer advisoryConsumer = session.CreateConsumer(AdvisorySupport.TEMP_DESTINATION_COMPOSITE_ADVISORY_TOPIC); advisoryConsumer.Listener += OnAdvisoryMessage; // This message delivery should work since the temp connection is still // open. IMessageProducer producer = session.CreateProducer(queue); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; ITextMessage message = session.CreateTextMessage("First"); producer.Send(message); Thread.Sleep(1000); // Closing the connection should destroy the temp queue that was // created. tempConnection.Close(); WaitForTempDestinationDelete(queue); // This message delivery should NOT work since the temp connection is now closed. try { message = session.CreateTextMessage("Hello"); producer.Send(message); Assert.Fail("Send should fail since temp destination should not exist anymore."); } catch (NMSException e) { Tracer.Debug("Test threw expected exception: " + e.Message); } }
public void TestTempDestOnlyConsumedByLocalConn() { connection.Start(); ISession tempSession = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue queue = tempSession.CreateTemporaryQueue(); IMessageProducer producer = tempSession.CreateProducer(queue); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; ITextMessage message = tempSession.CreateTextMessage("First"); producer.Send(message); // temp destination should not be consume when using another connection Connection otherConnection = CreateConnection() as Connection; connections.Add(otherConnection); ISession otherSession = otherConnection.CreateSession(AcknowledgementMode.AutoAcknowledge); ITemporaryQueue otherQueue = otherSession.CreateTemporaryQueue(); IMessageConsumer consumer = otherSession.CreateConsumer(otherQueue); IMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(3000)); Assert.IsNull(msg); // should throw InvalidDestinationException when consuming a temp // destination from another connection try { consumer = otherSession.CreateConsumer(queue); Assert.Fail("Send should fail since temp destination should be used from another connection"); } catch (InvalidDestinationException) { Assert.IsTrue(true, "failed to throw an exception"); } // should be able to consume temp destination from the same connection consumer = tempSession.CreateConsumer(queue); msg = consumer.Receive(TimeSpan.FromMilliseconds(3000)); Assert.NotNull(msg); }
public ITemporaryQueue GetTemporaryQueue() { if (this.temporaryQueue != null) return this.temporaryQueue; this.temporaryQueue = this.session.CreateTemporaryQueue(); return this.temporaryQueue; }
/// <summary> /// Sets up the response buffer, temporary queue, response consumer and listener delegates for sending and receiving synchronous messages. /// </summary> private void InitializeForSynchronous() { lock (this) { if (this.isInitializedForSynchronous) return; this.responseBuffer = new Dictionary<string, AsyncMessageHelper>(); this.temporaryQueue = this.session.GetTemporaryQueue(); this.responseConsumer = this.session.CreateConsumer(this.temporaryQueue); this.responseConsumer.Listener += new MessageListener(responseConsumer_Listener); this.isInitializedForSynchronous = true; this.isSynchronous = true; } }
public virtual void Destroy() { if (this.session == null) return; if (this.temporaryQueue != null) this.temporaryQueue.Delete(); this.temporaryQueue = null; this.session.Dispose(); this.session = null; this.connection = null; }
public ITemporaryQueue CreateTemporaryQueue(bool useCachedQueue = true) { if (useCachedQueue && this.temporaryQueue != null) return this.temporaryQueue; this.temporaryQueue = this.session.CreateTemporaryQueue(); return this.temporaryQueue; }