Accessing an ActiveMQ Stomp Feed In C# With Apache.NMS

The following tutorial explains how to connect to a Stomp data feed in C#, by using the Apache.NMS client.

To start with, download the Apache.NMS.Stomp Client.

Once downloaded, open the zip file and extract the following DLLs – copy them to the desktop for now:

/build/<.NET or Mono Version Required>/Release/Apache.NMS.Stomp.dll
/lib/Apache.NMS/<.NET or Mono Version Required>/Apache.NMS.dll

Next, start a new Console Application project in Visual Studio. Add a reference to the two DLLs selected earlier. Also add the following using statement – `using Apache.NMS;`

NOTE: You may receive a compile error that “apache” could not be found. this seems to occur when your project target type is set to “.NET Framework 4 Client Profile”. If you change it to “.NET Framework 4” this error goes away.

Ok, lets write some code.

In your main() function, create a ConnectionFactory, which we will then use to create a connection and session:

IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeed:61618"));

IConnection connection = factory.CreateConnection("username", "password");
ISession session = connection.CreateSession();

Next subscribe to the feed you wish to access:

IDestination destination = session.GetDestination("topic://topic_name");
IMessageConsumer consumer = session.CreateConsumer(destination);
connection.Start();

We have now connected to the data source and subscribed to a particular feed. Next we need to handle any messages that are received from the feed. We do this by creating a MessageListener:

consumer.Listener += new MessageListener(OnMessage);

That is all that we need in main – simply add a readline statement to stop the program from exiting immediately:

Console.ReadLine();
connection.Close();

We now create a new function called OnMessage, which will be called every time a message is received:

private static void OnMessage(IMessage message)
{
ITextMessage msg = (ITextMessage)message;
message.Acknowledge();

Console.WriteLine(msg.Text);
}

And that is all there is to it! If you run this now, you should see messages arriving from your data feed. Of course you may need to do additional processing on these to extract the data in a more useful format, but that’s a tutorial for another day.

Full application code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Apache.NMS;

namespace ApacheNMS
{
class Program
{
static void Main(string[] args)
{
IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeed:61618"));

IConnection connection = factory.CreateConnection("username", "password");
ISession session = connection.CreateSession();

IDestination destination = session.GetDestination("topic://" + "DATA_FEED");
IMessageConsumer consumer = session.CreateConsumer(destination);

connection.Start();
consumer.Listener += new MessageListener(OnMessage);
Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");

Console.ReadLine();
connection.Close();
}

private static void OnMessage(IMessage message)
{
try
{
Console.WriteLine("Median-Server (.NET): Message received");

ITextMessage msg = (ITextMessage)message;
message.Acknowledge();

Console.WriteLine(msg.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException.Message);
}
}
}
}
  • Jim

    I’m just getting started on this kind of programming so be gentle…
    I get “an System.TypeLoadException was unhandled
    HResult=-2146233054
    Message=Method ‘PurgeTempDestinations’ in type ‘Apache.NMS.Stomp.Connection’ from assembly ‘Apache.NMS.Stomp, Version=1.5.4.3215, Culture=neutral, PublicKeyToken=82756feee3957618’ does not have an implementation.
    Source=Apache.NMS.Stomp
    TypeName=Apache.NMS.Stomp.Connection
    StackTrace:
    at Apache.NMS.Stomp.ConnectionFactory.CreateConnection(String userName, String password)
    at Apache.NMS.NMSConnectionFactory.CreateConnection(String userName, String password) in c:\dev\NMS\src\main\csharp\NMSConnectionFactory.cs:line 387
    at ApacheNMS.Program.Main(String[] args) in c:\Users\Jim\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 16
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException: ”

    Any idea what I’ve done wrong. I used my network rail login email and password and my account is active?

  • gavco98uk

    It’s a while since I touched Apache.NMS, so I’m a bit rusty. However, MSDN has the following to say about the error:

    TypeLoadException is thrown when the common language runtime cannot find the assembly, the type within the assembly, or cannot load the type.

    I would try deleting the reference to Apache.NMS, and re-add it. Also check that you are using a version compatible with the version of the .NET runtime you are targeting (i.e don’t use Apache.NMS v4.0 if you are using .NET 3.5 etc).

    • Jim

      Thanks, I got the same idea – I had already linked the Apache.NMS through the VS tool before I saw your way to do it. Intriguingly I got a similar, but different issue with the Python code I got from the same wiki. I think I need to do more research before trying to run with things like this…

  • http://aiframeworks.net John Newcombe

    Many thanks for this example. It worked like a charm once I had added my user/pwd and set the data feed.

    Kind Regards

    John

    • gavco98uk

      No problem John, glad you found it useful.

  • parviz

    Hi Gavin,

  • parviz

    When I try to download Apache.NMS.Stomp Client I get a blank Confluence page

  • Ian Weaver

    We couldn’t get it to work until we changed it to versions 1.5.1 and 1.5.3 for Apache NMS and stomp respectively.

    • Ian Weaver

      To be clear, before that we get the following error on the CreateConnection:

      Message=Method ‘PurgeTempDestinations’ in type ‘Apache.NMS.Stomp.Connection’ from assembly ‘Apache.NMS.Stomp, Version=1.5.4.3215, Culture=neutral, PublicKeyToken=82756feee3957618’ does not have an implementation.

      • siva

        I am using 1.7.1 on NMS and 1.5.4 on Stomp , its not working for this Versions , what should i do?