1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd,
39 // Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
41 // Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 // Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 // are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 // Technologies LLC, and Rabbit Technologies Ltd.
46 // Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 // Ltd. Portions created by Cohesive Financial Technologies LLC are
48 // Copyright (C) 2007-2009 Cohesive Financial Technologies
49 // LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 // (C) 2007-2009 Rabbit Technologies Ltd.
52 // All Rights Reserved.
54 // Contributor(s): ______________________________________.
56 //---------------------------------------------------------------------------
58 using System.Collections;
62 using RabbitMQ.Client;
63 using RabbitMQ.Client.Content;
65 namespace RabbitMQ.Client.Examples {
66 public class SendMap {
67 public static int Main(string[] args) {
69 bool persistMode = false;
72 while (optionIndex < args.Length) {
73 if (args[optionIndex] == "/persist") {
81 if (((args.Length - optionIndex) < 1) ||
82 (((args.Length - optionIndex - 1) % 2) == 1))
84 Console.Error.WriteLine("Usage: SendMap [<option> ...] <exchange-uri> [[<key> <value>] ...]");
85 Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
86 Console.Error.WriteLine("Exchange-URI: amqp://host[:port]/exchange[/routingkey][?type=exchangetype]");
87 Console.Error.WriteLine("Keys must start with '+' or with '-'. Those starting with '+' are placed in");
88 Console.Error.WriteLine("the body of the message, and those starting with '-' are placed in the headers.");
89 Console.Error.WriteLine("Values must start with a single character typecode and a colon.");
90 Console.Error.WriteLine("Supported typecodes are:");
91 Console.Error.WriteLine(" S - string/byte array");
92 Console.Error.WriteLine(" x - byte array (base-64)");
93 Console.Error.WriteLine(" t - boolean");
94 Console.Error.WriteLine(" i - 32-bit integer");
95 Console.Error.WriteLine(" d - double-precision float");
96 Console.Error.WriteLine(" D - fixed-point decimal");
97 Console.Error.WriteLine("Note that some types are valid only in the body of a message, and some are");
98 Console.Error.WriteLine("valid only in the headers.");
99 Console.Error.WriteLine("The exchange \"amq.default\" is an alias for the default (\"\") AMQP exchange,");
100 Console.Error.WriteLine("introduced so that the default exchange can be addressed via URI syntax.");
101 Console.Error.WriteLine("Available options:");
102 Console.Error.WriteLine(" /persist send message in 'persistent' mode");
106 Uri uri = new Uri(args[optionIndex++]);
107 string exchange = uri.Segments[1].TrimEnd(new char[] { '/' });
108 string exchangeType = uri.Query.StartsWith("?type=") ? uri.Query.Substring(6) : null;
109 string routingKey = uri.Segments.Length > 2 ? uri.Segments[2] : "";
111 if (exchange == "amq.default") {
115 using (IConnection conn = new ConnectionFactory().CreateConnection(uri))
117 using (IModel ch = conn.CreateModel()) {
119 if (exchangeType != null) {
120 ch.ExchangeDeclare(exchange, exchangeType);
123 IMapMessageBuilder b = new MapMessageBuilder(ch);
124 while ((optionIndex + 1) < args.Length) {
125 string keyAndDiscriminator = args[optionIndex++];
126 string valueAndType = args[optionIndex++];
128 if (keyAndDiscriminator.Length < 1) {
129 Console.Error.WriteLine("Invalid key: '{0}'", keyAndDiscriminator);
132 string key = keyAndDiscriminator.Substring(1);
133 char discriminator = keyAndDiscriminator[0];
136 switch (discriminator) {
144 Console.Error.WriteLine("Invalid key: '{0}'",
145 keyAndDiscriminator);
149 if (valueAndType.Length < 2 || valueAndType[1] != ':') {
150 Console.Error.WriteLine("Invalid value: '{0}'", valueAndType);
153 string valueStr = valueAndType.Substring(2);
154 char typeCode = valueAndType[0];
162 value = new BinaryTableValue(Convert.FromBase64String(valueStr));
165 value = (valueStr.ToLower() == "true" ||
166 valueStr.ToLower() == "yes" ||
167 valueStr.ToLower() == "on" ||
171 value = int.Parse(valueStr);
174 value = double.Parse(valueStr);
177 value = decimal.Parse(valueStr);
180 Console.Error.WriteLine("Invalid type code: '{0}'", typeCode);
187 ((IBasicProperties) b.GetContentHeader()).DeliveryMode = 2;
189 ch.BasicPublish(exchange,
191 (IBasicProperties) b.GetContentHeader(),
196 } catch (Exception e) {
197 Console.Error.WriteLine(e);