blob: df80c8f50b881ba0e89f5edd6141fc5218f8ab2a [file] [log] [blame]
---
layout: post
status: PUBLISHED
published: true
title: ".NET Applications and Artemis with AMQP"
excerpt: "<p>On this blog post I will give a quick tutorial on how to write a .NET
client with Artemis, using the AMQP libraries and protocol</p>"
id: 0a1ddb0e-7b8a-4806-b0ed-e61eae6d6198
date: '2017-02-04 04:39:14 -0500'
categories: activemq
tags:
- jms
- artemis
- dotnet
- java
permalink: activemq/entry/using-net-libraries-with-activemq
---
<p>My first draft for this blog had a very extensive introduction about AMQP, its benefits, how business manager X would preserve its investment and blablablablaba...
<p>Lets say I cut all that #@!# and I'm jumping right to the good stuff:</p>
<p><b>AMQP is cool</b>, full of Libraries that you can bring to use with <b>ActiveMQ Artemis</b>.</p></p>
<p>One of the Libraries you can use is the <a href="https://github.com/Azure/amqpnetlite/">AMQPLite with .NET</a></p>
<p>If you just want to read the code, i have recently added the <a href="https://github.com/apache/activemq-artemis/tree/master/examples/protocols/amqp/dotnet">.NET Example on Artemis</a>, which is pretty much what I'm describing here</p>
<p>Let me give you a cooking recipe on how to write a .NET application and Apache ActiveMQ Artemis</p>
<p>Let's do it!</p>
<p><b>Step 1</b> - Install .NET</p>
<p>There is this fun video explaining how to do it. My teenager daughter would definitely be able to follow this:<br />
<br/><a href="https://channel9.msdn.com/Blogs/dotnet/Get-started-with-VS-Code-using-CSharp-and-NET-Core">https://channel9.msdn.com/Blogs/dotnet/Get-started-with-VS-Code-using-CSharp-and-NET-Core</a></p>
<p>There is also a video showing how to do it on <a href="https://channel9.msdn.com/Blogs/dotnet/Get-started-with-VS-Code-using-CSharp-and-NET-Core-on-MacOS">Mac</a> and <a href="https://channel9.msdn.com/Blogs/dotnet/Get-started-with-VS-Code-Csharp-dotnet-Core-Ubuntu">Linux</a></p>
<p><b>Step 2</b> - Write the code</p>
<p>I basically followed the steps here on this hello world app:<br />
<br/><a href="https://github.com/Azure/amqpnetlite/blob/master/docs/articles/hello_amqp.md">https://github.com/Azure/amqpnetlite/blob/master/docs/articles/hello_amqp.md</a></p>
<p>The main pitfall I had since I was very rusty on .NET was to install the AMQP.net library accordingly:</p>
<p>But I reckon it was my fault, although I could fix it in 10 min by adding this to the project.json:</p>
<pre>
"dependencies": {"AMQPNetLite": "1.2.2"},
</pre>
<p>Here is the code I have:</p>
<pre>
using System;
using Amqp;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
string address = "amqp://a:a@localhost:5672";
Connection connection = new Connection(new Address(address));
Session session = new Session(connection);
SenderLink sender = new SenderLink(session, "test-sender", "q1");
Message message1 = new Message("Hello AMQP!");
sender.Send(message1);
Console.WriteLine("Message sent into queue q1");
}
}
}
</pre>
<p><b>Step 3</b> - Create an Artemis Broker and run it</p>
<p>./artemis create /tmp/myBroker</p>
<pre>
./artemis create /tmp/mybroker
Creating ActiveMQ Artemis instance at: /private/tmp/mybroker
--user: is a mandatory property!
Please provide the default username:
a
--password: is mandatory with this configuration:
Please provide the default password:
--role: is a mandatory property!
Please provide the default role:
a
--allow-anonymous | --require-login: is a mandatory property!
Allow anonymous access? (Y/N):
y
Auto tuning journal ...
done! Your system can make 25 writes per millisecond, your journal-buffer-timeout will be 40000
You can now start the broker by executing:
"/private/tmp/mybroker/bin/artemis" run
Or you can run the broker in the background using:
"/private/tmp/mybroker/bin/artemis-service" start
</pre>
<p><p><b>Finally:</b> run the thing:</p>
<p># this will install dependencies<br/>dotnet restore</p>
<pre>
log : Restoring packages for /work/apache/six/artemis-distribution/target/apache-artemis-2.0.0-SNAPSHOT-bin/apache-artemis-2.0.0-SNAPSHOT/examples/dotnet/amqp/queue/project.json...
log : Writing lock file to disk. Path: /work/apache/six/artemis-distribution/target/apache-artemis-2.0.0-SNAPSHOT-bin/apache-artemis-2.0.0-SNAPSHOT/examples/dotnet/amqp/queue/project.lock.json
log : /work/apache/six/artemis-distribution/target/apache-artemis-2.0.0-SNAPSHOT-bin/apache-artemis-2.0.0-SNAPSHOT/examples/dotnet/amqp/queue/project.json
log : Restore completed in 741ms.
</pre>
<p># this will run the code<br/>dotnet run</p>
<pre>
Project dotnet (.NETCoreApp,Version=v1.1) will be compiled because expected outputs are missing
Compiling dotnet for .NETCoreApp,Version=v1.1
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:00.9060278
Message sent into queue q1
</pre>
<p>All of this can be done with both Artemis and ActiveMQ Classic </p>
<p>AMQP is a quite powerful and you can reuse your client libraries any way you want.</p>