Communication between C# .NET and Java via ActiveMQ messaging – Part 1

Cross-language communication between a C# .NET client and a Java server application.
A (very) simple approach.

JMS is widely used, and has become a de-facto communication standard. It is also pretty easy to use and allows asynchronous processing of messages. A popular open source messaging provider is ActiveMQ. It has a very big advantage, because there is also a C# implementation available, which is part of the messaging in Spring.NET.
If your are familiar with JMS and Spring it’s very simple to understand how to use NMS and Spring.NET.

For this communication example i used Spring 3.0.0 Release, ActiveMq NMS 1.1.0 and Spring.NET 1.3.0 GA.

First step:
We need a POJO/PONO object as data container. A language independed approach is to use XML for schema description. A more improved version would be to generate the neccessary MessageConverters also, but I will drop some words about this later.

<xs:schema xmlns:xs="">
   <xs:element name="ExampleMessage">
            <xs:element name="Messagetext" type="xs:string"/>
            <xs:element name="ImageData" type="xs:hexBinary"/>

Our ExampleMessage encloses two fields. A string variable for the message text and a byte-array for image data.
With this XML definition its easy to generate code (C#,Java) for both application parts.

In the Microsoft world there is a command line tool named xsd.exe, which is part of the Windows SDK.

xsd.exe /c examplemessage.xsd

The output:

using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class ExampleMessage {
    private string messagetextField;
    private byte[] imageDataField;
    public string Messagetext {
        get {
            return this.messagetextField;
        set {
            this.messagetextField = value;

jaxb can be used for the Java code generation.

xjc.bat -p it.koller.interopexample examplemessage.xsd
package it.koller.interopexample;
public class ExampleMessage
     private String messagetext;
     private byte[] imagedata;
    /* getter &amp; setter methods for var access */
     public String getMessagetext()
           return messagetext;
     public void setName(String messagetext)
          this.messagetext = messagetext;

For C# client example see Part 2.

Please feel free to leave any comments!

Be Sociable, Share!

, , , , ,

  1. #1 by CWD on 27. May 2011 - 21:00

    Hello Alois,

    I am trying to consume a topic (java objectmessage) from JMS to a .NET app using Spring.NET and TIBCO EMS. I am getting a the topic but the object is null in the message body and I am getting Desrialization failed message.

    Can you please suggest a way of converting java objectmessage to a c# object? thanks.

  2. #2 by alois koller on 28. May 2011 - 12:24


    could you post some more details.
    Is the object serialized correctly?
    Is it possible to consume the message from a Java JMS client?

(will not be published)