I am trying to produce messages to and consume messages from ActiveMQ Artemis queues for the first time. I am able to connect and produce messages via Apache.NMS.ActiveMQ as well as Apache.NMS.AMQP when I use the code in a C# console application. However, when I put that same code in a C# Windows forms application the CreateSession
method call runs forever.
IConnectionFactory factory = new ConnectionFactory(brokerUri);
using (IConnection connection = factory.CreateConnection(username, password))
{
using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
{
IDestination destination = session.GetQueue(queueName);
IMessageProducer producer = session.CreateProducer(destination);
IMessage textMessage = session.CreateTextMessage(text);
producer.Send(textMessage);
}
}
My broker is using SSL, and I use the Apache.NMS.ActiveMQ library with the brokerUri="ssl://mybroker:443"
and I use Apache.NMS.AMQP with the brokerUri="amqps://mybroker:443"
. Again, each of these work fine when run in a Console app, but not a Windows Forms app.
For the tl;dr crowd:
We've encountered this issue in AMQ6 and AMQ7. The workaround mentioned in the jira issues and the comments above is to wrap any AMQ connection code in a Task.Run, like so: