We are evaluating the NMS-API to connect a C# app to our ActiveMQ
broker. For this we wrote a simple client which sends a request and
waits for a reply (Client --> Broker --> Server --> Broker --> Client).
The client/server C#-app runs in a single process with two different
connections to the broker which resides on a different pc on the network.
This scenario takes about 200ms for each message transfered by the
C#-API and less than 20ms by the Java-API although both do the same thing.
Does anybody have an idea what is going wrong or why there is such a big
time differences?
Thank you for helping
Stefan
Code for the C# test app follows:
==========================================
using System;
using Apache.NMS;
namespace Test
{
class SimpleTest
{
private static readonly String URI = "tcp://broker:61616";
private static readonly String REQUEST_QUEUE = "test.request";
private static DateTime startOffset;
public static void Main()
{
IConnectionFactory factory = new
NMSConnectionFactory(URI);
SetUpReceiver(factory);
SetUpSender(factory);
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
private static void SetUpReceiver(IConnectionFactory factory)
{
// set up receiver
IConnection rConnection = factory.CreateConnection();
ISession rSession = rConnection.CreateSession();
IMessageConsumer rConsumer =
rSession.CreateConsumer(rSession.GetQueue(REQUEST_QUEUE));
IMessageProducer rProducer = rSession.CreateProducer();
rConsumer.Listener += delegate(IMessage message)
{
OnMessage(rSession, rProducer, message);
};
rConnection.Start();
}
private static void SetUpSender(IConnectionFactory factory)
{
IConnection sConnection = factory.CreateConnection();
ISession sSession = sConnection.CreateSession();
IMessageProducer sProducer =
sSession.CreateProducer(sSession.GetQueue(REQUEST_QUEUE));
IDestination replyDestination =
sSession.CreateTemporaryQueue();
IMessageConsumer sConsumer =
sSession.CreateConsumer(replyDestination);
sConnection.Start();
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Test " + i);
// send message and wait for reply
IMessage requestMsg =
sSession.CreateTextMessage("Request" + i);
requestMsg.NMSReplyTo = replyDestination;
startOffset = DateTime.Now;
sProducer.Send(requestMsg, false, NMSConstants.defaultPriority,
NMSConstants.defaultTimeToLive);
WriteTimedMessage("Request message sent");
IMessage replyMsg = sConsumer.Receive();
WriteTimedMessage("Reply message received");
}
}
private static void OnMessage(ISession session, IMessageProducer
producer, IMessage message)
{
WriteTimedMessage("Request message received");
IMessage replyMsg = session.CreateTextMessage("Reply");
producer.Send(message.NMSReplyTo, replyMsg, false,
NMSConstants.defaultPriority, NMSConstants.defaultTimeToLive);
WriteTimedMessage("Reply message sent");
}
private static void WriteTimedMessage(String message)
{
lock (typeof(SimpleTest))
{
TimeSpan diff = DateTime.Now - startOffset;
Console.WriteLine("{0} ms: {1}",
diff.TotalMilliseconds, message);
}
}
}
}