Receiving Queue Alerts in C#
dkalten
Member Posts: 1 ✭
I'm currently running into issues trying to subscribe to alerts from my queue. I currently have my queue set up where I should be getting threshold alerts when I have 2+ msgs in the queue. From https://docs.solace.com/Monitoring/Subscribing-to-MBus-Events.htm and https://solace.community/discussion/236/queue-depth-and-consumer-monitoring I have gathered that i need to be reading the VPN_AD_MSG_SPOOL_HIGH alerts; however, I've tried a multitude of topic names, and i never receives any msgs regarding these alerts. I've even tried "#LOG/>" which is supposedly supposed to send all alerts, but even that gives me no msgs. I have gotten it to be able to recieve normal topic msgs, but haven't been able to figure out these alerts.
```
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
}
}
```
```
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
}
}
```
0
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