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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Org.Apache.Qpid.Messaging;
using Org.Apache.Qpid.Messaging.SessionReceiver;
namespace Org.Apache.Qpid.Messaging.Examples
{
/// <summary>
/// A class with functions to display structured messages.
/// </summary>
public static class MessageViewer
{
/// <summary>
/// A Function to display a amqp/map message packaged as a Dictionary.
/// </summary>
/// <param name="dict">The AMQP map</param>
/// <param name="level">Nested depth</param>
public static void ShowDictionary(Dictionary<string, object> dict, int level)
{
foreach (KeyValuePair<string, object> kvp in dict)
{
Console.Write(new string(' ', level * 4));
if (QpidTypeCheck.ObjectIsMap(kvp.Value))
{
Console.WriteLine("Key: {0}, Value: Dictionary", kvp.Key);
ShowDictionary((Dictionary<string, object>)kvp.Value, level + 1);
}
else if (QpidTypeCheck.ObjectIsList(kvp.Value))
{
Console.WriteLine("Key: {0}, Value: List", kvp.Key);
ShowList((Collection<object>)kvp.Value, level + 1);
}
else
Console.WriteLine("Key: {0}, Value: {1}, Type: {2}",
kvp.Key, kvp.Value, kvp.Value.GetType().ToString());
}
}
/// <summary>
/// A function to display a ampq/list message packaged as a List.
/// </summary>
/// <param name="list">The AMQP list</param>
/// <param name="level">Nested depth</param>
public static void ShowList(Collection<object> list, int level)
{
foreach (object obj in list)
{
Console.Write(new string(' ', level * 4));
if (QpidTypeCheck.ObjectIsMap(obj))
{
Console.WriteLine("Dictionary");
ShowDictionary((Dictionary<string, object>)obj, level + 1);
}
else if (QpidTypeCheck.ObjectIsList(obj))
{
Console.WriteLine("List");
ShowList((Collection<object>)obj, level + 1);
}
else
Console.WriteLine("Value: {0}, Type: {1}",
obj.ToString(), obj.GetType().ToString());
}
}
/// <summary>
/// A function to diplay a Message. The native Object type is
/// decomposed into AMQP types.
/// </summary>
/// <param name="message">The Message</param>
public static void ShowMessage(Message message)
{
if ("amqp/map" == message.ContentType)
{
Console.WriteLine("Received a Dictionary");
Dictionary<string, object> content = new Dictionary<string, object>();
message.GetContent(content);
ShowDictionary(content, 0);
}
else if ("amqp/list" == message.ContentType)
{
Console.WriteLine("Received a List");
Collection<object> content = new Collection<object>();
message.GetContent(content);
ShowList(content, 0);
}
else
{
Console.WriteLine("Received a String");
Console.WriteLine(message.GetContent());
}
}
}
/// <summary>
/// A model class to demonstrate how a user may use the Qpid Messaging
/// interface to receive Session messages using a callback.
/// </summary>
class ReceiverProcess : ISessionReceiver
{
UInt32 messagesReceived = 0;
/// <summary>
/// SessionReceiver implements the ISessionReceiver interface.
/// It is the callback function that receives all messages for a Session.
/// It may be called any time server is running.
/// It is always called on server's private thread.
/// </summary>
/// <param name="receiver">The Receiver associated with the message.</param>
/// <param name="message">The Message</param>
public void SessionReceiver(Receiver receiver, Message message)
{
//
// Indicate message reception
//
Console.WriteLine("--- Message {0}", ++messagesReceived);
//
// Display the received message
//
MessageViewer.ShowMessage(message);
//
// Acknowledge the receipt of all received messages.
//
receiver.Session.Acknowledge();
}
/// <summary>
/// Usage
/// </summary>
/// <param name="url">Connection target</param>
/// <param name="addr">Address: broker exchange + routing key</param>
/// <param name="nSec">n seconds to keep callback open</param>
static void usage(string url, string addr, int nSec)
{
Console.WriteLine("usage: {0} [url [addr [nSec]]]",
System.Diagnostics.Process.GetCurrentProcess().ProcessName);
Console.WriteLine();
Console.WriteLine("A program to connect to a broker and receive");
Console.WriteLine("messages from a named exchange with a routing key.");
Console.WriteLine("The receiver uses a session callback and keeps the callback");
Console.WriteLine("server open for so many seconds.");
Console.WriteLine("The details of the message body's types and values are shown.");
Console.WriteLine();
Console.WriteLine(" url = target address for 'new Connection(url)'");
Console.WriteLine(" addr = address for 'session.CreateReceiver(addr)'");
Console.WriteLine(" nSec = time in seconds to keep the receiver callback open");
Console.WriteLine();
Console.WriteLine("Default values:");
Console.WriteLine(" {0} {1} {2} {3}",
System.Diagnostics.Process.GetCurrentProcess().ProcessName,
url, addr, nSec);
}
/// <summary>
/// A function to illustrate how to open a Session callback and
/// receive messages.
/// </summary>
/// <param name="args">Main program arguments</param>
public int TestProgram(string[] args)
{
string url = "amqp:tcp:localhost:5672";
string addr = "amq.direct/map_example";
int nSec = 30;
string connectionOptions = "";
if (1 == args.Length)
{
if (args[0].Equals("-h") || args[0].Equals("-H") || args[0].Equals("/?"))
{
usage(url, addr, nSec);
return 1;
}
}
if (args.Length > 0)
url = args[0];
if (args.Length > 1)
addr = args[1];
if (args.Length > 2)
nSec = System.Convert.ToInt32(args[2]);
if (args.Length > 3)
connectionOptions = args[3];
//
// Create and open an AMQP connection to the broker URL
//
Connection connection = new Connection(url, connectionOptions);
connection.Open();
//
// Create a session.
//
Session session = connection.CreateSession();
//
// Receive through callback
//
// Create callback server and implicitly start it
//
SessionReceiver.CallbackServer cbServer =
new SessionReceiver.CallbackServer(session, this);
//
// The callback server is running and executing callbacks on a
// separate thread.
//
//
// Create a receiver for the direct exchange using the
// routing key "map_example".
//
Receiver receiver = session.CreateReceiver(addr);
//
// Establish a capacity
//
receiver.Capacity = 100;
//
// Wait so many seconds for messages to arrive.
//
System.Threading.Thread.Sleep(nSec * 1000); // in mS
//
// Stop the callback server.
//
cbServer.Close();
//
// Close the receiver and the connection.
//
receiver.Close();
connection.Close();
return 0;
}
}
class MapCallbackReceiverMain
{
/// <summary>
/// Main program
/// </summary>
/// <param name="args">Main prgram args</param>
static int Main(string[] args)
{
// Invoke 'TestProgram' as non-static class.
ReceiverProcess mainProc = new ReceiverProcess();
int result = mainProc.TestProgram(args);
return result;
}
}
}
|