.NET Applications and Artemis with AMQP
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...
Lets say I cut all that #@!# and I'm jumping right to the good stuff:
AMQP is cool, full of Libraries that you can bring to use with ActiveMQ Artemis.
One of the Libraries you can use is the AMQPLite with .NET
If you just want to read the code, i have recently added the .NET Example on Artemis, which is pretty much what I'm describing here
Let me give you a cooking recipe on how to write a .NET application and Apache ActiveMQ Artemis
Let's do it!
Step 1 - Install .NET
There is this fun video explaining how to do it. My teenager daughter would definitely be able to follow this:
https://channel9.msdn.com/Blogs/dotnet/Get-started-with-VS-Code-using-CSharp-and-NET-Core
There is also a video showing how to do it on Mac and Linux
Step 2 - Write the code
I basically followed the steps here on this hello world app:
https://github.com/Azure/amqpnetlite/blob/master/docs/articles/hello_amqp.md
The main pitfall I had since I was very rusty on .NET was to install the AMQP.net library accordingly:
But I reckon it was my fault, although I could fix it in 10 min by adding this to the project.json:
"dependencies": {"AMQPNetLite": "1.2.2"},
Here is the code I have:
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"); } } }
Step 3 - Create an Artemis Broker and run it
./artemis create /tmp/myBroker
./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
Finally: run the thing:
# this will install dependencies
dotnet restore
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.
# this will run the code
dotnet run
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
All of this can be done with both Artemis and ActiveMQ Classic
AMQP is a quite powerful and you can reuse your client libraries any way you want.