mirror of https://github.com/apache/activemq.git
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 :)
git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@432377 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d0da99a08c
commit
73e5a3024c
|
@ -41,8 +41,18 @@ namespace ActiveMQ
|
|||
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 @@ namespace ActiveMQ
|
|||
{
|
||||
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 @@ namespace ActiveMQ
|
|||
/// </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 @@ namespace ActiveMQ
|
|||
{
|
||||
dispatcher.Redeliver(message);
|
||||
|
||||
if (Listener != null)
|
||||
if (listener != null)
|
||||
{
|
||||
// lets re-dispatch the message at some point in the future
|
||||
Thread.Sleep(RedeliveryTimeout);
|
||||
|
|
|
@ -44,7 +44,6 @@ namespace NMS {
|
|||
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 @@ namespace NMS {
|
|||
}
|
||||
|
||||
[ 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())
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue