Author: jstrachan
Date: Thu Aug 17 13:20:42 2006
New Revision: 432377
URL: http://svn.apache.org/viewvc?rev=432377&view=rev
Log:
added fix for AMQ-883 and AMQ-865 along with test cases from Denis Abramov and
Bryan Schmidt. In partcular many thanks to Bryan Schmidt who figured out what
the problem was and helped me do wacky things with the event delegate thingy :)
Added:
incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/DurableTest.cs
Modified:
incubator/activemq/trunk/activemq-dotnet/src/main/csharp/ActiveMQ/MessageConsumer.cs
incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/AsyncConsumeTest.cs
Modified:
incubator/activemq/trunk/activemq-dotnet/src/main/csharp/ActiveMQ/MessageConsumer.cs
URL:
http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-dotnet/src/main/csharp/ActiveMQ/MessageConsumer.cs?rev=432377&r1=432376&r2=432377&view=diff
==============================================================================
---
incubator/activemq/trunk/activemq-dotnet/src/main/csharp/ActiveMQ/MessageConsumer.cs
(original)
+++
incubator/activemq/trunk/activemq-dotnet/src/main/csharp/ActiveMQ/MessageConsumer.cs
Thu Aug 17 13:20:42 2006
@@ -41,8 +41,18 @@
private Dispatcher dispatcher = new Dispatcher();
private int maximumRedeliveryCount = 10;
private int redeliveryTimeout = 500;
+ private event MessageListener listener;
- public event MessageListener Listener;
+ public event MessageListener Listener
+ {
+ add {
+ listener += value;
+ FireAsyncDispatchOfMessages();
+ }
+ remove {
+ listener -= value;
+ }
+ }
public MessageConsumer(Session session, ConsumerInfo info,
AcknowledgementMode acknowledgementMode)
@@ -84,11 +94,16 @@
{
dispatcher.Enqueue(message);
- if (Listener != null)
+ if (listener != null)
{
+ FireAsyncDispatchOfMessages();
+ }
+ }
+
+ protected void FireAsyncDispatchOfMessages()
+ {
// lets dispatch to the thread pool for this connection for
messages to be processed
ThreadPool.QueueUserWorkItem(new
WaitCallback(session.DispatchAsyncMessages));
- }
}
public IMessage Receive()
@@ -122,14 +137,14 @@
/// </summary>
public void DispatchAsyncMessages()
{
- while (Listener != null)
+ while (listener != null)
{
IMessage message = dispatcher.DequeueNoWait();
if (message != null)
{
//here we add the code that if do acknowledge action.
message = AutoAcknowledge(message);
- Listener(message);
+ listener(message);
}
else
{
@@ -218,7 +233,7 @@
{
dispatcher.Redeliver(message);
- if (Listener != null)
+ if (listener != null)
{
// lets re-dispatch the message at some point in the future
Thread.Sleep(RedeliveryTimeout);
Modified:
incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/AsyncConsumeTest.cs
URL:
http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/AsyncConsumeTest.cs?rev=432377&r1=432376&r2=432377&view=diff
==============================================================================
---
incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/AsyncConsumeTest.cs
(original)
+++
incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/AsyncConsumeTest.cs
Thu Aug 17 13:20:42 2006
@@ -44,7 +44,6 @@
public void TestAsynchronousConsume()
{
- // lets create an async consumer
// START SNIPPET: demo
IMessageConsumer consumer =
Session.CreateConsumer(this.Destination);
consumer.Listener += new MessageListener(OnMessage);
@@ -61,7 +60,43 @@
}
[ Test ]
- public void textMessageSRExample()
+ public void TestCreateConsumerAfterSend()
+ {
+ // now lets send a message
+ IMessageProducer producer = CreateProducer();
+ IMessage request = CreateMessage();
+ request.NMSCorrelationID = "abc";
+ request.NMSType = "Test";
+ producer.Send(request);
+
+ // lets create an async consumer
+ IMessageConsumer consumer =
Session.CreateConsumer(this.Destination);
+ consumer.Listener += new MessageListener(OnMessage);
+
+ WaitForMessageToArrive();
+ }
+
+ [ Test ]
+ public void
TestCreateConsumerBeforeSendButAddListenerAfterSend()
+ {
+ // lets create an async consumer
+ IMessageConsumer consumer =
Session.CreateConsumer(this.Destination);
+
+ // now lets send a message
+ IMessageProducer producer = CreateProducer();
+ IMessage request = CreateMessage();
+ request.NMSCorrelationID = "abc";
+ request.NMSType = "Test";
+ producer.Send(request);
+
+ // now lets add the listener
+ consumer.Listener += new MessageListener(OnMessage);
+
+ WaitForMessageToArrive();
+ }
+
+ [ Test ]
+ public void TextMessageSRExample()
{
using (IConnection connection =
Factory.CreateConnection())
{
Added:
incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/DurableTest.cs
URL:
http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/DurableTest.cs?rev=432377&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/DurableTest.cs
(added)
+++ incubator/activemq/trunk/activemq-dotnet/src/test/csharp/NMS/DurableTest.cs
Thu Aug 17 13:20:42 2006
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Text;
+
+using NUnit.Framework;
+using ActiveMQ;
+using NMS;
+using ActiveMQ.Commands;
+using System.Threading;
+
+namespace ActiveMQDurableTest {
+ [ TestFixture ]
+ public class DurableTest
+ {
+ private static string TOPIC = "TestTopic";
+
+ private static String URI = "tcp://localhost:61616";
+
+ private static String CLIENT_ID = "DurableClientId";
+
+ private static String CONSUMER_ID = "ConsumerId";
+
+ private static ConnectionFactory FACTORY = new
ConnectionFactory(new Uri(URI));
+
+ private int count = 0;
+
+ public void RegisterDurableConsumer()
+ {
+ using (IConnection connection =
FACTORY.CreateConnection())
+ {
+ connection.ClientId = CLIENT_ID;
+ connection.Start();
+
+ using (ISession session =
connection.CreateSession(
+ AcknowledgementMode.DupsOkAcknowledge))
+ {
+ ITopic topic = session.GetTopic(TOPIC);
+ IMessageConsumer consumer =
session.CreateDurableConsumer(
+ topic, CONSUMER_ID, "2 > 1",
false);
+ consumer.Dispose();
+ }
+
+ connection.Stop();
+ }
+ }
+
+ public void SendPersistentMessage()
+ {
+ using (IConnection connection =
FACTORY.CreateConnection())
+ {
+ connection.Start();
+ using (ISession session =
connection.CreateSession(
+ AcknowledgementMode.DupsOkAcknowledge))
+ {
+ ITopic topic = session.GetTopic(TOPIC);
+ ActiveMQTextMessage message = new
ActiveMQTextMessage("Hello");
+ message.NMSPersistent = true;
+ message.Persistent = true;
+ IMessageProducer producer =
session.CreateProducer();
+ producer.Send(topic, message);
+ producer.Dispose();
+ }
+
+ connection.Stop();
+ }
+ }
+
+ [ Test ]
+ public void TestMe()
+ {
+ count = 0;
+
+ RegisterDurableConsumer();
+ SendPersistentMessage();
+
+ using (IConnection connection =
FACTORY.CreateConnection())
+ {
+ connection.ClientId = CLIENT_ID;
+ connection.Start();
+
+ using (ISession session =
connection.CreateSession(
+ AcknowledgementMode.DupsOkAcknowledge))
+ {
+ ITopic topic = session.GetTopic(TOPIC);
+ IMessageConsumer consumer =
session.CreateDurableConsumer(
+ topic, CONSUMER_ID, "2 > 1",
false);
+ consumer.Listener += new
MessageListener(consumer_Listener); /// Don't know how else to give the system
enough time. /// Thread.Sleep(5000); Assert.AreEqual(0, count);
Console.WriteLine("Count = " + count); SendPersistentMessage();
Thread.Sleep(5000); Assert.AreEqual(2, count); Console.WriteLine("Count = " +
count); consumer.Dispose(); }
+
+ connection.Stop();
+ }
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="message"></param>
+ private void consumer_Listener(IMessage message)
+ {
+ ++count;
+ }
+ }
+}