I am using Artemis with .NET Core and NMS in 2 different projects. One as a consumer and one as a listener and I am following the code example provided in the docs. When I follow the documentation, I can get both async and sync versions to work. However, the listener only works when the queue already previously has a message on it. If i start the listener first with no message and use the async version, after execution from another project, and sending the message from the producer, nothing is happening. So basically I cannot continous listening for the queue on the listener part. anyone knows what im doing wrong?
public void Receive(string queue)
{
IConnectionFactory factory = new NMSConnectionFactory(ServerUri);
using (IConnection connection = factory.CreateConnection(Username, Password))
using (Apache.NMS.ISession session = connection.CreateSession())
{
IDestination destination = SessionUtil.GetDestination(session, queue);
using (IMessageConsumer consumer = session.CreateConsumer(destination))
{
// Start the connection so that messages will be processed.
connection.Start();
consumer.Listener += new MessageListener(message =>
{
Console.WriteLine(message);
});
//semaphore.WaitOne((int)receiveTimeout.TotalMilliseconds, true);
if (message == null)
{
Console.WriteLine("No message received!");
}
else
{
Console.WriteLine("Received message with ID: " + message.NMSMessageId);
Console.WriteLine("Received message with ID: " + message.Text);
}
/* // Consume a message
IObjectMessage message2 = consumer.Receive() as IObjectMessage;
if (message2 == null)
{
Console.WriteLine("No message received!");
}
else
{
Console.WriteLine("Received message with ID: " + message2.NMSMessageId);
Console.WriteLine("Received message with text: " + message2.Body);
}*/
}
}
}
and for the producer in another project
public void Send(object message, string queue)
{
IConnectionFactory factory = new NMSConnectionFactory(ServerUri);
using (IConnection connection = factory.CreateConnection(Username, Password))
using (ISession session = connection.CreateSession())
{
IDestination destination = SessionUtil.GetDestination(session, queue);
using (IMessageProducer producer = session.CreateProducer(destination))
{
connection.Start();
producer.DeliveryMode = MsgDeliveryMode.Persistent;
ITextMessage request = session.CreateTextMessage("Hello World!");
request.NMSCorrelationID = "abc";
request.Properties["NMSXGroupID"] = "cheese";
request.Properties["myHeader"] = "Cheddar";
/* IObjectMessage request = session.CreateObjectMessage(message);*/
producer.Send(request);
}
}
}
i expected it to work.
From a quick glance over the listener code I don't see any reason why it would hang around listening for a message if one wasn't on the queue since you are using an asynchronous consumer and not blocking or waiting any length of time inside that receive call. It isn't really even clear why you would be using an asynchronous listener in an API call that appears to be appears to be an imperative receive call which would more closely map to the NMS IMessageConsumer Receive API than the asynchronous receive callback mechanism.
From the code given you are also performing a messaging anti-pattern which is to create a new connection, session and consumer for each attempt to receive a message.