A .NET class library for accessing to the HornetQ REST interface

This is the third article of my series about the HornetQ REST interface, in the first post I wrote about the building and deployment of a customized version of the Web application that implements the interface, in the second post I talked about the development of a Java class library for accessing to the HornetQ REST interface, this time I want to develop a .NET API with the same goal.

I was an enthusiastic VB6 programmer long, long time ago. I also participated in some projects that made use of Visual Basic .NET and I even made some things in ASP.NET, but, to be perfectly honest, I’ve been focused in Java based technologies last years, so I’ve had to bring myself up to date on .NET!

The first task was to setup a development environment. I decided to use the free tool Microsoft Visual Web Developer 2010 Express and I also installed the extension NuGet Package Manager, which made the setup of Microsoft ASP.NET Web API Client Libraries easier. These libraries helped me to access to the HornetQ REST interface, like RESTEasy helped me in Java. This post of Mike Wasson was very useful to get a quick introduction, although he programs in C#!

My idea was to rewrite the library I created in Java using Visual Basic .NET, as simple as that. The first thing I realized is that nowadays both languages have similar capacities, a quick proof, this is the messaging interface in Java:


public interface MessagingInterface {

    void start() throws MessagingException;

    <T> void sendMessage(T message) throws MessagingException;

    <T> T receiveNextMessage(Class<T> type) throws MessagingException;

    void ackLastMessageReceived() throws MessagingException;

    void stop() throws MessagingException;
}

And this .NET:


Public Interface MessagingInterface(Of T)

    Sub ClientStart()

    Sub SendMessage(ByRef Mensaje As T)

    Function ReceiveNextMessage() As T

    Sub AckLastMessageReceived()

    Sub ClientStop()

End Interface

The HTTP messages to send in each case are determined by the HornetQ REST Interface user’s manual, so the algorithms are the same in Java and in Visual Basic .NET, I just had to adapt the code to the language and the special features of .NET
Another interesting comparison, this the Java method to send a message:


@Override
public <T> void sendMessage(T message) throws MessagingException {
    ClientResponse response;

    try {
        if (!this.isStarted) {
            throw new IllegalStateException("The client is not started");
        }

        response = this.msgCreateLink.request().body(MediaType.APPLICATION_XML, message).post();

        if (response.getStatus() == 307) {
            this.msgCreateLink = response.getLocation();

            response = this.msgCreateLink.request().body(MediaType.APPLICATION_XML, message).post();
        }

        if (response.getResponseStatus().equals(Response.Status.CREATED)) {
            this.msgCreateLink = response.getHeaderAsLink("msg-create-next");
        } else {
            throw new MessagingException("Response code  " + response.getStatus() + " not supported");
        }
    } catch (MessagingException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MessagingException(ex);
    }
}

And this is the .NET one:


Public Sub SendMessage(ByRef Message As T) Implements MessagingInterface(Of T).SendMessage
    Dim Response As HttpResponseMessage
    Dim XmlFormatter As XmlMediaTypeFormatter

    Try
        If Not Me.IsStarted Then
            Throw New InvalidOperationException("The client is not started")
        End If

        XmlFormatter = New XmlMediaTypeFormatter
        XmlFormatter.UseXmlSerializer = True

        Response = Me.HornetQHttpClient.PostAsync(Me.MsgCreateUri, 
                                                  Message, 
                                                  XmlFormatter).Result

        If Response.StatusCode = HttpStatusCode.RedirectKeepVerb Then
            Me.MsgCreateUri = Response.Headers.GetValues("Location").First
            Response = Me.HornetQHttpClient.PostAsync(Me.MsgCreateUri, 
                                                      Message, 
                                                      XmlFormatter).Result
        End If

        If Response.StatusCode = HttpStatusCode.Created Then
            Me.MsgCreateUri = Response.Headers.GetValues("msg-create-next").First
        Else
            Throw New MessagingException("Response code " + Response.StatusCode + " not supported")
        End If
    Catch ex As MessagingException
        Throw ex
    Catch ex As Exception
        Throw New MessagingException(ex.Message, ex)
    End Try
End Sub

References



Leave a comment