activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgo...@apache.org
Subject svn commit: r777295 - /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/TestMainAsync.cs
Date Thu, 21 May 2009 22:26:56 GMT
Author: jgomes
Date: Thu May 21 22:26:56 2009
New Revision: 777295

URL: http://svn.apache.org/viewvc?rev=777295&view=rev
Log:
Added TestMainAsync.cs for documentation guide.

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/TestMainAsync.cs

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/TestMainAsync.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/TestMainAsync.cs?rev=777295&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/TestMainAsync.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/TestMainAsync.cs Thu
May 21 22:26:56 2009
@@ -0,0 +1,102 @@
+/*
+ * 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.Threading;
+using Apache.NMS;
+using Apache.NMS.Util;
+
+namespace Apache.NMS.ActiveMQ.Test
+{
+	public class TestMain
+	{
+		protected static AutoResetEvent semaphore = new AutoResetEvent(false);
+		protected static ITextMessage message = null;
+		protected static TimeSpan receiveTimeout = TimeSpan.FromSeconds(10);
+		
+		public static void Main(string[] args)
+		{
+			try
+			{
+				// START SNIPPET: demo
+				Uri connecturi = new Uri("activemq:tcp://activemqhost:61616");
+				
+				Console.WriteLine("About to connect to " + connecturi);
+
+				// NOTE: ensure the nmsprovider-activemq.config file exists in the executable folder.
+				IConnectionFactory factory = new NMSConnectionFactory(connecturi);
+
+				using(IConnection connection = factory.CreateConnection())
+				using(ISession session = connection.CreateSession())
+				{
+					/*
+					 * Examples for getting a destination:
+					 *   IDestination destination = session.GetQueue("FOO.BAR");  // Hard coded to queue
destination
+					 *   IDestination destination = session.GetTopic("FOO.BAR");  // Hard coded to topic
destination
+					 *   IDestination destination = SessionUtil.GetDestination(session, "queue://FOO.BAR");
 // Allows destination type to be embedded in name
+					 *   IDestination destination = SessionUtil.GetDestination(session, "topic://FOO.BAR");
 // Allows destination type to be embedded in name
+					 *   IDestination destination = SessionUtil.GetDestination(session, "FOO.BAR");    
     // Defaults to queue if type not specified.
+					 */
+					IDestination destination = SessionUtil.GetDestination(session, "queue://FOO.BAR");
+					Console.WriteLine("Using destination: " + destination);
+
+					// Create a consumer and producer
+					using(IMessageConsumer consumer = session.CreateConsumer(destination))
+					using(IMessageProducer producer = session.CreateProducer(destination))
+					{
+						connection.Start();		// Must start the connection for async messaging.
+						producer.Persistent = true;
+						producer.RequestTimeout = receiveTimeout;
+						consumer.Listener += new MessageListener(OnMessage);
+
+						// Send a message
+						ITextMessage request = session.CreateTextMessage("Hello World!");
+						request.NMSCorrelationID = "abc";
+						request.Properties["NMSXGroupID"] = "cheese";
+						request.Properties["myHeader"] = "Cheddar";
+
+						producer.Send(request);
+
+						// Wait for the message
+						semaphore.WaitOne((int) receiveTimeout.TotalMilliseconds, true);
+						if(message == null)
+						{
+							Console.WriteLine("No message received!");
+						}
+						else
+						{
+							Console.WriteLine("Received message with ID:   " + message.NMSMessageId);
+							Console.WriteLine("Received message with text: " + message.Text);
+						}
+					}
+				}
+				// END SNIPPET: demo
+			}
+			catch(Exception e)
+			{
+				Console.WriteLine("Caught: " + e);
+				Console.WriteLine("Stack: " + e.StackTrace);
+			}
+		}
+
+		protected static void OnMessage(IMessage receivedMsg)
+		{
+			message = receivedMsg as ITextMessage;
+			semaphore.Set();
+		}
+	}
+}



Mime
View raw message