🎄 Happy Holidays! 🥳
Most of Solace is closed December 24–January 1 so our employees can spend time with their families. We will re-open Thursday, January 2, 2024. Please expect slower response times during this period and open a support ticket for anything needing immediate assistance.
Happy Holidays!
Please note: most of Solace is closed December 25–January 2, and will re-open Tuesday, January 3, 2023.
Receiving Queue Alerts in C#
```
namespace SolaceTopicListener
{
class Program
{
// Copied directly from Solace's example online w/ minor changes to use settings file
public class TopicSubscriber : IDisposable
{
string VPNName { get; set; }
string UserName { get; set; }
string Password { get; set; }
const int DefaultReconnectRetries = 3;
private ISession Session = null;
private EventWaitHandle WaitEventWaitHandle;
private Logger logger_ = LogManager.GetCurrentClassLogger();
void Run(IContext context, string host, int port, string topic, EventWaitHandle handle)
{
WaitEventWaitHandle = handle;
// Validate parameters
if (context == null) {
throw new ArgumentException("Solace Systems API context Router must be not null.", "context");
}
if (string.IsNullOrWhiteSpace(host)) {
throw new ArgumentException("Solace Messaging Router host name must be non-empty.", "host");
}
if (string.IsNullOrWhiteSpace(VPNName)) {
throw new InvalidOperationException("VPN name must be non-empty.");
}
if (string.IsNullOrWhiteSpace(UserName)) {
throw new InvalidOperationException("Client username must be non-empty.");
}
// Create session properties
SessionProperties sessionProps = new SessionProperties()
{
Host = $"{host}:{port}",
VPNName = VPNName,
UserName = UserName,
Password = Password,
ReconnectRetries = DefaultReconnectRetries
};
// Connect to the Solace messaging router
Console.WriteLine("Connecting as {0}@{1} on {2}...", UserName, VPNName, host);
// NOTICE HandleMessage as the message event handler
Session = context.CreateSession(sessionProps, HandleMessage, OnSolaceEvent_);
ReturnCode returnCode = Session.Connect();
if (returnCode == ReturnCode.SOLCLIENT_OK) {
Console.WriteLine("Session successfully connected.");
topic = "#LOG/>";
returnCode = Session.Subscribe(ContextFactory.Instance.CreateTopic(topic), false);
if (returnCode == ReturnCode.SOLCLIENT_OK) {
Console.WriteLine("Waiting for a message to be published...");
WaitEventWaitHandle.WaitOne();
}
else {
Console.WriteLine("Error subscribing to topic {0} : {1}", topic, returnCode);
}
}
else {
Console.WriteLine("Error connecting, return code: {0}", returnCode);
}
}
/// <summary>
/// This event handler is invoked by Solace Systems Messaging API when a message arrives
/// </summary>
/// <param name="source"></param>
/// <param name="args"></param>
private void HandleMessage(object source, MessageEventArgs args)
{
Console.WriteLine("Received published message.");
// Received a message
using (IMessage message = args.Message) {
// Expecting the message content as a binary attachment
if (message.BinaryAttachment != null && message.BinaryAttachment.Length > 0) {
string encoded = Encoding.UTF8.GetString(message.BinaryAttachment);
Console.WriteLine("Binary Content: {0}", encoded);
logger_.Info($"Binary Content: {encoded}");
}
if (message.XmlContent != null && message.XmlContent.Length > 0) {
Console.WriteLine("Message content: {0}", Encoding.ASCII.GetString(message.XmlContent));
logger_.Info("Message content: {0}", Encoding.ASCII.GetString(message.XmlContent));
}
}
}
private void OnSolaceEvent_(object sender, SessionEventArgs e)
{
// Received a flow event
Console.WriteLine("Received Session Event '{0}' Type: '{1}' Text: '{2}'",
e.Event,
e.ResponseCode.ToString(),
e.Info);
}
public void HandleFlowEvent(object sender, FlowEventArgs args)
{
// Received a flow event
Console.WriteLine("Received Flow Event '{0}' Type: '{1}' Text: '{2}'",
args.Event,
args.ResponseCode.ToString(),
args.Info);
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue) {
if (disposing) {
if (Session != null) {
Session.Dispose();
}
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
#region Main
static void Main(string[] args)
{
SolaceListenerSettings settings = new SolaceListenerSettings();
settings.Upgrade();
Worms.Tools.Logging.NLogController.ConfigureFileTarget("SolaceListener");
string host = settings.SolaceHost; // Solace messaging router host name or IP address
string username = settings.SolaceUsername;
string vpnname = settings.SolaceVpn;
string password = settings.SolacePassword;
int port = settings.SolacePort;
string topic = settings.SolaceTopic;
EventWaitHandle handle = new AutoResetEvent(false);
// Initialize Solace Systems Messaging API with logging to console at Warning level
ContextFactoryProperties cfp = new ContextFactoryProperties()
{
SolClientLogLevel = SolLogLevel.Warning
};
cfp.LogToConsoleError();
ContextFactory.Instance.Init(cfp);
try {
// Context must be created first
using (IContext context = ContextFactory.Instance.CreateContext(new ContextProperties(), null)) {
// Create the application
using (TopicSubscriber topicSubscriber = new TopicSubscriber()
{
VPNName = vpnname,
UserName = username,
Password = password
}) {
// Run the application within the context and against the host
topicSubscriber.Run(context, host, port, topic, handle);
Console.ReadLine();
handle.Set();
}
}
}
catch (Exception ex) {
Console.WriteLine("Exception thrown: {0}", ex.Message);
}
finally {
// Dispose Solace Systems Messaging API
ContextFactory.Instance.Cleanup();
}
Console.WriteLine("Finished.");
}
#endregion
}
}
```
Answers
-
-
Hey @dkalten ,
Thanks for waiting. I had to research this a bit 😁!
You haven't indicated whether you're using Cloud/Software/Appliance. I assumed Software for the purposes of my reply.
To have the broker actually publish
system
events:- Designate a given
Message VPN
as a Management VPN- Details here
- ⚠️ Without this, the broker will not publish system events onto the
#LOG/>
topic(s).
- Set your alert thresholds on the queue as appropriate.
- Publish
This is how you enable publication using the Web Manager UI:
This is how the
alert
appeared from my tests using the Web Manager UI:Hope this helps!
1 - Designate a given