Skip to content

Commit

Permalink
ActiveMQ tester application.
Browse files Browse the repository at this point in the history
  • Loading branch information
matkun committed Nov 21, 2022
1 parent 7dd3e5e commit 47ed167
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 0 deletions.
Binary file added ActiveMqTester/ActiveMqTester_1.0.zip
Binary file not shown.
13 changes: 13 additions & 0 deletions ActiveMqTester/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tester application for ActiveMQ

## Usage

Update appsettings.json with new values.

Make application act as sender: `.\ActiveMqTester.exe sender`

Make application act as receiver: `.\ActiveMqTester.exe receiver`

## More information

Please see https://blog.mathiaskunto.com/2022/06/10/activemq-connection-tester-application/
25 changes: 25 additions & 0 deletions ActiveMqTester/src/ActiveMqTester.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Apache.NMS.ActiveMQ" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions ActiveMqTester/src/ActiveMqTester.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31702.278
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ActiveMqTester", "ActiveMqTester.csproj", "{63F88131-964B-4925-B89C-C3CDB145E034}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{63F88131-964B-4925-B89C-C3CDB145E034}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63F88131-964B-4925-B89C-C3CDB145E034}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63F88131-964B-4925-B89C-C3CDB145E034}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63F88131-964B-4925-B89C-C3CDB145E034}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F9221A66-D5C9-4356-9996-E4C2BAF4BFE6}
EndGlobalSection
EndGlobal
43 changes: 43 additions & 0 deletions ActiveMqTester/src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.Extensions.Configuration;

using System;
using System.IO;

namespace ActiveMqTester
{
class Program
{
private const string _help = "Usage:\r\nActiveMqTester.exe sender (the app will act as a sender).\r\nActiveMqTester.exe reciever (the app will act as a receiver).";

static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine(_help);
return;
}

IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.Build();
IConfigurationSection section = configuration.GetSection("TesterSettings");

Console.WriteLine($"\r\nActiveMQ tester.");

switch (args[0])
{
case "sender":
Sender.Send(section);
break;
case "receiver":
Receiver.Receive(section);
break;
default:
Console.WriteLine(_help);
break;
}
Console.WriteLine($"\r\nExiting ...");
}
}
}
12 changes: 12 additions & 0 deletions ActiveMqTester/src/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net5.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
</PropertyGroup>
</Project>
35 changes: 35 additions & 0 deletions ActiveMqTester/src/Receiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Apache.NMS;
using Apache.NMS.ActiveMQ;

using Microsoft.Extensions.Configuration;

using System;

namespace ActiveMqTester
{
internal class Receiver
{
public static void Receive(IConfigurationSection section)
{
(ConnectionFactory factory, string queue, string username, string password, MsgDeliveryMode deliveryMode, AcknowledgementMode acknowledgementMode) = Settings.GetValues(section);


using IConnection connection = factory.CreateConnection(username, password);
connection.Start();

using ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
using IDestination destination = session.GetQueue(queue);
using IMessageConsumer consumer = session.CreateConsumer(destination);

Console.WriteLine("\r\n--- Waiting for messages (press any key to quit) ---\r\n");

consumer.Listener += (IMessage message) =>
{
var textMessage = message as ITextMessage;
Console.WriteLine($"Received {DateTime.Now:HH:mm:ss}: {textMessage.Text}");
};

_ = Console.ReadLine();
}
}
}
42 changes: 42 additions & 0 deletions ActiveMqTester/src/Sender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Apache.NMS;
using Apache.NMS.ActiveMQ;

using Microsoft.Extensions.Configuration;

using System;

namespace ActiveMqTester
{
internal class Sender
{
public static void Send(IConfigurationSection section)
{
(ConnectionFactory factory, string queue, string username, string password, MsgDeliveryMode deliveryMode, AcknowledgementMode acknowledgementMode) = Settings.GetValues(section);

using IConnection connection = factory.CreateConnection(username, password);
connection.Start();

using ISession session = connection.CreateSession(acknowledgementMode);
using IDestination destination = session.GetQueue(queue);
using IMessageProducer producer = session.CreateProducer(destination);

producer.DeliveryMode = deliveryMode;

string message;
while (true)
{
Console.Write("\r\nMessage to send (ENTER to quit): ");
message = Console.ReadLine();
if (message == string.Empty)
{
break;
}

ITextMessage textMessage = session.CreateTextMessage(message);
producer.Send(textMessage);

Console.WriteLine($"Message sent {DateTime.Now:HH:mm:ss}");
}
}
}
}
39 changes: 39 additions & 0 deletions ActiveMqTester/src/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Apache.NMS;
using Apache.NMS.ActiveMQ;

using Microsoft.Extensions.Configuration;

using System;

namespace ActiveMqTester
{
internal class Settings
{
public static (ConnectionFactory factory, string queue, string username, string password, MsgDeliveryMode deliveryMode, AcknowledgementMode acknowledgementMode) GetValues(IConfigurationSection section)
{
string hostName = section.GetSection("HostName").Value;
string queue = section.GetSection("Queue").Value;
int port = int.Parse(section.GetSection("Port").Value);
string username = section.GetSection("Username").Value;
string password = section.GetSection("Password").Value;
var delMode = (MsgDeliveryMode)int.Parse(section.GetSection("MsgDeliveryMode").Value);
var ackMode = (AcknowledgementMode)int.Parse(section.GetSection("AcknowledgementMode").Value);
string brokerAddress = $"activemq:tcp://{hostName}:{port}";

Console.WriteLine("\r\nUsing the following settings from appsettings.json:");
Console.WriteLine($"HostName: {hostName}");
Console.WriteLine($"Port: {port}");
Console.WriteLine($"Username: {username}");
Console.WriteLine($"Password: Not shown");
Console.WriteLine($"Queue: {queue}");
Console.WriteLine($"Del.Mode: {delMode}");
Console.WriteLine($"Ack.Mode: {ackMode}");
Console.WriteLine($"Broker Url: {brokerAddress}");

var brokerUri = new Uri(brokerAddress);
var factory = new ConnectionFactory(brokerUri);

return (factory, queue, username, password, delMode, ackMode);
}
}
}
11 changes: 11 additions & 0 deletions ActiveMqTester/src/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"TesterSettings": {
"HostName": "my-activemq-server.com",
"Port": 61616,
"Queue": "nameofqueue",
"Username": "username",
"Password": "password",
"MsgDeliveryMode": 1,
"AcknowledgementMode": 0
}
}
7 changes: 7 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ Descriptions:

*****************************************************************************

ActiveMqTester
https://blog.mathiaskunto.com/2022/06/10/activemq-connection-tester-application/

Tester application for ActiveMQ.



Base54EmbeddedImage
https://blog.mathiaskunto.com/2011/08/21/embedding-episerver-vpp-images-in-html-using-base64-encoding/

Expand Down

0 comments on commit 47ed167

Please sign in to comment.