-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueBrowser.cs
106 lines (101 loc) · 3.8 KB
/
QueueBrowser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Text;
using SolaceSystems.Solclient.Messaging;
using ISession = SolaceSystems.Solclient.Messaging.ISession;
namespace QueueBrowserExample.Solace
{
public class QueueBrowser
{
public string VPNName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
const int DefaultReconnectRetries = 3;
public List<string> Run(string queuename,IContext context, string host)
{
// 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,
VPNName = VPNName,
UserName = UserName,
Password = Password,
ReconnectRetries = DefaultReconnectRetries
};
Console.WriteLine("Connecting as {0}@{1} on {2}...", UserName, VPNName, host);
try
{
using (ISession session = context.CreateSession(sessionProps, null, null))
{
ReturnCode returnCode = session.Connect();
if (returnCode == ReturnCode.SOLCLIENT_OK)
{
Console.WriteLine("Session successfully connected.");
return BrowseQueue(queuename,session);
}
else
{
throw new SolaceConnectionException($"Error connecting, return code: {returnCode}");
Console.WriteLine($"Error connecting, return code: {returnCode}");
}
}
}
catch (Exception ex)
{
throw ;
}
}
private List<string> BrowseQueue(string queuename,ISession session)
{
try
{
List<string> RESULT = new List<string>();
BrowserProperties browserProperties = new BrowserProperties
{
TransportWindowSize = 255,
WaitTimeout = 1000,
};
IQueue q = ContextFactory.Instance.CreateQueue(queuename);
IBrowser browser = session.CreateBrowser(q,browserProperties);
IMessage message;
do
{
message = browser.GetNext();
try {
var bin = message.BinaryAttachment;
if (bin !=null)
{
string messageContent = Encoding.UTF8.GetString(bin);
RESULT.Add(messageContent);
}
}
catch (Exception)
{
// sometimes the BinaryAttachment property fails with an Exception
// add your personal extra handling here
}
} while (message != null);
return RESULT;
}
catch (Exception ex)
{
throw;
}
}
}
}