Search code examples
c#sslssl-certificateactivemq-classic

How to use SslStream connection for ActiveMQ communication


I want to establish connection with server and communicate with ActiveMQ running on that server I take code from MSDN:

TcpClient client = new TcpClient(machineName, port); // ActiveMQ is running on that port on the server
Console.WriteLine("Client connected.");
SslStream sslStream = new SslStream(
    client.GetStream(),
    false,
    new RemoteCertificateValidationCallback(ValidateServerCertificate),
    null
    );
try
{
    sslStream.AuthenticateAsClient(machineName, certificates, SslProtocols.Tls, true);
}
catch (AuthenticationException e)
{
    Console.WriteLine("Exception: {0}", e.Message);
    if (e.InnerException != null)
    {
        Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
    }
    Console.WriteLine("Authentication failed - closing the connection.");
    client.Close();
    return;
}

and I am able to establish connection. I send request to the server:

byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>");
// Send hello message to the server. 
sslStream.Write(messsage);
sslStream.Flush();
// Read message from the server.
string serverMessage = ReadMessage(sslStream);
Console.WriteLine("Server says: {0}", serverMessage);

In the response, server says:

?ActiveMQ y TcpNoDelayEnabled SizePrefixDisabled CacheSize ProviderName ActiveMQ StackTraceEnabled PlatformDetails TJVM: 1.8.0_202, 25.202-b08, Oracle Corporation, OS: Windows Server

So, it means communication with Server AMQ established. My question is, how can I use now that AMQ channel for sending and receiving messages? Normally, I have to specify message queue I want to use. How and where I have to do that? Should it be in message I send to server? Something like:

string message_queue = "test.message.tosend";
byte[] request = Encoding.UTF8.GetBytes(String.Format(<formatted queue name with message content>, machineName));

Example of code would be very much appreciated.


Solution

  • ActiveMQ is a message broker which supports lots of different messaging protocols (e.g. OpenWire, AMQP 1.0, STOMP, & MQTT). Each of these protocols is similar in that they support the exchange of messages between a client and the broker, but they are different in what specific functionality they support, their protocol handshakes, wire formats, etc.

    For whatever reason you are not using any of the client implementations for these protocols. You are using the low level TcpClient which means you'll need to find the specification for the protocol you wish to speak (e.g. AMPQ 1.0, STOMP 1.2) and implement a client for it. Depending on the protocol you choose this could be a significant amount of work.

    However, instead of reimplementing a client you could use one which already exists, e.g.: