PDA

View Full Version : Spring.Messaging.Nms - Setting up Listener


dmar
05-02-2007, 02:06 PM
I have downloaded Spring.Messaging.Nms (Spring.Messaging.Nms-20070320-1632.zip) and am attempting to use it for messaging between a .NET Windows Forms app and a Java app. I am totally new to JMS and messaging concepts...so I apologize for what are probably newbie questions that I am posting.

I have been successful at sending a message to a topic using NmsTemplate. However, I am not able to set up a listener. I am trying to piece together the code using the ListenerTests.cs contents I found under Spring.Messaging.Nms.Integration.Tests. The relevant code from my form is posted below. Using jconsole, I can see that my listener form makes a connection to ActiveMQ, but when I send messages to my test topic, the listener is not picking them up -- the OnMessage event is not even firing.

One other thing -- the connections that result from me sending messages with NmsTemplate are not being closed - even after I close my sending application. I can see that in jconsole.

I appreciate any thoughts/sugggestions. Also would like to know if there are more complete samples/documentation somewhere. I'm very new to this, and not finding much to go on so far.

Thanks in adavance. Code snippet is below.

private void btnListen_Click(object sender, EventArgs e)
{
txtMessages.AppendText("Setting up Listener...\r\n");
EstablishListenerContainer();
btnListen.Enabled = false;
}

private void EstablishListenerContainer()
{

string serverUrl = "tcp://localhost:61616";
IConnectionFactory factory = new ConnectionFactory(serverUrl);

SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer();
container.ConnectionFactory = factory;

container.DestinationName = "TEST.Test1";
container.ConcurrentConsumers = 10;

container.MessageListener = new SimpleListener();

txtMessages.AppendText("Listening to messages...\r\n");

container.AfterPropertiesSet();

//Thread.Sleep(5000);
}

}

internal class SimpleListener : IMessageListener
{
public void OnMessage(IMessage message)
{
String threadId = "[" + Thread.CurrentThread.Name + "]";

ITextMessage tm = message as ITextMessage;

if (tm != null)
{
Console.WriteLine(threadId + ": Received message = " + tm.Text);
}

//Thread.Sleep(100);
}
}

Mark Pollack
05-02-2007, 02:28 PM
Hi,

I've ran the tests for NMS and they worked but I didn't recreate your exact scenario, i.e sending from Java. Your listener code looks good.

As far as docs, there is alot of similarity with those you can find in the Spring Java framework. Even better for your case, is an article I wrote for InfoQ that explains in detail JMS communication between a .NET frontend and Java backend using JMS and Spring/Spring.NET. There is a good amount of code that should get you started, however, it is using the TIBCO EMS implementation. You can download a TIBCO EMS dameon just to give it a whirl and then translate that to NMS (a find/replease of Ems to Nms should get you far). The NMS code was contributed by James and I haven't looked over his 'transliteration' completely.

The URL is http://www.infoq.com/articles/jms-spring-messaging-interop

The connection not being closed, even after you close the app, is strange. Eventually it will close but it sounds like proper cleanup isn't occuring. I'll look into that and bring in James if needed.

Cheers,
Mark

dmar
05-02-2007, 03:48 PM
Mark,

Thanks for the prompt reply. The infoq article and associated code looks excellent. I'm going to download TIBCO EMS and give it a go, as you suggested. Looks like a great place to start.

I still can't figure out why my listener won't work, though. I'm going to drop that for now, and work through your example with TIBCO EMS.

Thanks again for the reply, and the excellent article.

Regards,

Dave Marley

Mark Pollack
05-03-2007, 03:30 AM
Hi Dave,

Cool, we'll sort this out. I forgot to mention that there are docs (http://www.springframework.net/modules/Spring.Messaging.Tibco.Ems/doc/reference/html/index.html) for the TIBCO EMS implementation, which should be useful even for the NMS version. I've isolated specific TIBCO EMS specific details to the end chapter.

Do you know which messaging middleware you will be using for you application?

Cheers,
Mark

dmar
05-03-2007, 06:21 PM
Hi Mark,

We are using ActiveMQ for the messaging middleware.

I've made some progress, but definitely have a ways to go! I downloaded TIBCO EMS and was able to run your demo code -- very nice. Also, I was able to run the unit tests that were packaged with Spring.Messaging.Nms - between that and your article/code, I'm starting to put the pieces together. Definitely still trying to wrap my brain around your code....Spring.NET, the XML-based object definitions...all new territory for me, on top of JMS/messaging :)

Problem is I still can't get an asychronous listener to work against ActiveMQ. I can see, via jconsole, that my test .NET form is establishing a connection -- and based on the ConsumerCount and DispatchCount on the topic, it appears to be picking up messages that are sent to the topic. But...they are not showing on my test form...from what I can tell OnMessage is not firing. I suspect I am missing something simple...

Also, I tried converting your TIBCO EMS code to work with NMS. I have to admit it was a mostly brute-force effort on my part -- mostly replacing TIBCO.EMS references with NMS worked. Only part that wasn't clear was TIBCO.EMS.Message and TIBCO.EMS.MapMessage, which appear to be interfaces in NMS (IMessage and IMapMessage). Still getting a few compile errors though, in the converters:

'Spring.JmsInterop.Converters.ReflectionMessageCon verter' does not implement interface member 'Spring.Messaging.Nms.Support.Converter.IMessageCo nverter.ToMessage(object, NMS.ISession)'

That even though a public ToMessage method exists. I think that's related to my replacement of TIBCO.EMS.Message with NMS.IMessage. For my purposes at the moment, I don't believe I need those converters, so this afternoon I'm going to remove those and just get the listener part (ie, the MarketData listbox at the bottom of your form) working. If I can just pass a simple string across, that will be a very good start for my project.

Thanks again for everything. As you can probably tell, I am trying to digest a lot of new technology/concepts at once here...

Cheers,
Dave

dmar
05-07-2007, 02:41 PM
OK, I now have a simple test case working that implements IMessageListener. So the basic plumbing is now in place for the Java-ActiveQ-NMS messaging. Now I just need to get my app's form responding properly to incoming messages.

I am not using the Spring.NET dependency injection style -- tried but could not get it set up properly. I am getting an error "Error instantiating context 'spring.root'" -- I hope to return to that and get it resolved, but for time being am moving forward with making the assignments in code as I am doing in my original post to this thread. Less elegant, but gets me through for now.

The Tibco EMS documentation was helpful. Thanks again for the info.

Regards,

Dave Marley