public override void SampleCall(string[] args) { #region Parse Arguments ArgParser cmdLineParser = new ArgParser(); if (!cmdLineParser.Parse(args)) { // Parse failed. PrintUsage(INVALID_ARGUMENTS_ERROR); return; } if (!SampleParseArgs(cmdLineParser)) { // Parse failed for sample's arguments. PrintUsage(INVALID_ARGUMENTS_ERROR); return; } #endregion #region Initialize properties from command line // Initialize the properties. ContextProperties contextProps = new ContextProperties(); SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config); #endregion // Define IContext and ISession. IContext context = null; ISession session = null; // Create the LinkedList. LinkedList <MessageRecord> msgRecords = new LinkedList <MessageRecord>(); try { InitContext(cmdLineParser.LogLevel); Console.WriteLine("About to create the context ..."); context = ContextFactory.Instance.CreateContext(contextProps, null); Console.WriteLine("Context successfully created. "); Console.WriteLine("About to create the session ..."); session = context.CreateSession(sessionProps, SampleUtils.HandleMessageEvent, HandleSessionEvent); Console.WriteLine("Session successfully created."); // Connect the session. Console.WriteLine("About to connect the session ..."); if (session.Connect() == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Session successfully connected"); Console.WriteLine(GetRouterInfo(session)); } else { Console.WriteLine("Failed to connect session, aborting ..."); return; } // Validate required capabilities. if (!session.IsCapable(CapabilityType.PUB_GUARANTEED)) { Console.WriteLine(string.Format("This sample requires capability '{0}' to be supported", CapabilityType.PUB_GUARANTEED)); return; } // At this point the session is connected and not reconnecting sessionIsReconnecting = false; sessionIsReconnecting = false; // Send cmdLineParser.Config.NumberOfMessagesToPublish messages. for (int i = 0; i < cmdLineParser.Config.NumberOfMessagesToPublish; i++) { // If the session is reconnecting the applications should not send messages while (sessionIsReconnecting) { Thread.Sleep(100); } if (sessionIsDisconnected) { // No automatic reconnect attemps will be made by the API. It's up to the // client application to reconnect the session Console.WriteLine("About to connect the session ..."); if (session.Connect() == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Session successfully connected"); } else { Console.WriteLine("Failed to connect session, aborting ..."); return; } // The client application in this case is responsible for re-pulishing all unacked message foreach (MessageRecord record in msgRecords) { if (!record.Acked) { ReturnCode rc = session.Send(record.Message); if (rc != ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Failed to send unacked messages, aborting ..."); return; } } } } // Allocate a new message. IMessage message = SampleUtils.CreateMessage(cmdLineParser.Config, session); message.DeliveryMode = MessageDeliveryMode.Persistent; // Create a record, and set it as CorrelationKey. MessageRecord msgRecord = new MessageRecord(message); message.CorrelationKey = msgRecord; // Add it to the list of send message records and send it. msgRecords.AddLast(msgRecord); session.Send(message); // Sleep for 500 msecs and check to see if the message was acknowledged (positively or negatively). Thread.Sleep(500); while (msgRecords.First != null && msgRecords.First.Value.Acked) { MessageRecord record = msgRecords.First.Value; msgRecords.RemoveFirst(); Console.WriteLine( string.Format("Freeing memory for message {0}, Result: Acked ({1}))\n", i, record.Acked)); record.Message.Dispose(); } Thread.Sleep(1000); } } catch (Exception ex) { PrintException(ex); } finally { // There should not be any left in the list, but just in case. foreach (MessageRecord record in msgRecords) { record.Message.Dispose(); } if (session != null) { session.Dispose(); } if (context != null) { context.Dispose(); } // Must cleanup after. CleanupContext(); } }
/// <summary> /// Main sample method /// </summary> /// <param name="args"></param> public override void SampleCall(string[] args) { #region Parse Arguments ArgParser cmdLineParser = new ArgParser(); if (!cmdLineParser.Parse(args)) { // parse failed PrintUsage(INVALID_ARGUMENTS_ERROR); return; } cmdLineParser.Config.SetDestMode(DestMode.TOPIC); cmdLineParser.Config.DeliveryMode = MessageDeliveryMode.Direct; #endregion #region Initialize properties from command line. // Initialize the properties. ContextProperties contextProps = new ContextProperties(); SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config); #endregion // Define IContext and ISession. IContext context = null; ISession session = null; IMessage message = null; try { InitContext(); Console.WriteLine("About to create the context ..."); context = ContextFactory.Instance.CreateContext(contextProps, null); Console.WriteLine("Context successfully created. "); Console.WriteLine("About to create the session ..."); session = context.CreateSession(sessionProps, SampleUtils.HandleMessageEvent, SampleUtils.HandleSessionEvent); Console.WriteLine("Session successfully created."); Console.WriteLine("About to connect the session ..."); if (session.Connect() == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Session successfully connected"); Console.WriteLine(GetRouterInfo(session)); } message = SampleUtils.CreateMessage(cmdLineParser.Config, session); message.DeliveryMode = MessageDeliveryMode.Direct; message.Destination = ContextFactory.Instance.CreateTopic(SampleUtils.SAMPLE_TOPIC); session.Send(message); Console.WriteLine("\nDone"); } catch (Exception ex) { PrintException(ex); } finally { if (session != null) { session.Dispose(); } if (context != null) { context.Dispose(); } // Must cleanup after. CleanupContext(); } }
/// <summary> /// The main function in the sample. /// </summary> /// <param name="args"></param> public override void SampleCall(string[] args) { #region Parse Arguments ArgParser cmdLineParser = new ArgParser(); if (!cmdLineParser.Parse(args)) { // parse failed PrintUsage(INVALID_ARGUMENTS_ERROR); return; } #endregion #region Initialize properties from command line // Initialize the properties. ContextProperties contextProps = new ContextProperties(); SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config); #endregion // Define IContext. IContext context = null; try { InitContext(cmdLineParser.LogLevel); Console.WriteLine("About to create the context ..."); context = ContextFactory.Instance.CreateContext(contextProps, null); Console.WriteLine("Context successfully created. "); // We will create two sessions, and give them different msg receiver callbacks. IdPrintingReceiver recvCallback1 = new IdPrintingReceiver("Client 1"); IdPrintingReceiver recvCallback2 = new IdPrintingReceiver("Client 2"); Console.WriteLine("About to create the Sessions..."); session = context.CreateSession(sessionProps, recvCallback1.HandleMessageEvent, SampleUtils.HandleSessionEvent); session2 = context.CreateSession(sessionProps, recvCallback2.HandleMessageEvent, SampleUtils.HandleSessionEvent); Console.WriteLine("Sessions successfully created."); Console.WriteLine("About to connect the Session..."); if (session.Connect() == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Session 1 successfully connected."); Console.WriteLine(GetRouterInfo(session)); } Console.Write(String.Format("Check for capability: {0}... ", CapabilityType.SUBSCRIPTION_MANAGER)); if (!session.IsCapable(CapabilityType.SUBSCRIPTION_MANAGER)) { Console.WriteLine("Not Supported. Exiting."); return; } else { Console.WriteLine("OK"); } // Appliance supports this sample, connect 2nd session. if (session2.Connect() == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Session 2 successfully connected"); } // Create the Topic to subscribe and send messages to. ITopic serviceTopic = ContextFactory.Instance.CreateTopic(topic_str); // Once clients have been connected, their ClientNames can be extracted. ContextFactory cf = ContextFactory.Instance; string strClientName1 = (string)session.GetProperty(SessionProperties.PROPERTY.ClientName); string strClientName2 = (string)session2.GetProperty(SessionProperties.PROPERTY.ClientName); IClientName clientName1 = cf.CreateClientName(strClientName1); IClientName clientName2 = cf.CreateClientName(strClientName2); Console.WriteLine("Client '{0}' adding subscription on behalf of client '{1}'", clientName1, clientName2); session.Subscribe(clientName2, serviceTopic, SubscribeFlag.RequestConfirm | SubscribeFlag.WaitForConfirm, null); Console.WriteLine("OK. Added subscription '{0}'.", serviceTopic); IMessage messageOne = SampleUtils.CreateMessage(cmdLineParser.Config, session); messageOne.Destination = serviceTopic; Console.WriteLine("Sending a message from Client 1..."); session.Send(messageOne); Console.WriteLine("Sent."); Thread.Sleep(500); Console.WriteLine("Done."); } catch (Exception ex) { PrintException(ex); } finally { if (session != null) { session.Dispose(); } if (session2 != null) { session2.Dispose(); } if (context != null) { context.Dispose(); } // Must cleanup after. CleanupContext(); } }
public override void SampleCall(string[] args) { #region Parse Arguments ArgParser cmdLineParser = new ArgParser(); if (!cmdLineParser.Parse(args)) { // Parse failed. PrintUsage(INVALID_ARGUMENTS_ERROR); return; } if (!SampleParseArgs(cmdLineParser)) { // Parse failed for sample's arguments. PrintUsage(INVALID_ARGUMENTS_ERROR); return; } #endregion #region Initialize properties from command line // Initialize the properties. ContextProperties contextProps = new ContextProperties(); SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config); // Uncomment the line below if you wish to send using a non-blocking mode // sessionProps.SendBlocking = false; #endregion // Define IContext and ISession. IContext context = null; ISession session = null; // Create the LinkedList. LinkedList <MessageRecord> msgRecords = new LinkedList <MessageRecord>(); try { InitContext(cmdLineParser.LogLevel); Console.WriteLine("About to create the context ..."); context = ContextFactory.Instance.CreateContext(contextProps, null); Console.WriteLine("Context successfully created. "); Console.WriteLine("About to create the session ..."); session = context.CreateSession(sessionProps, SampleUtils.HandleMessageEvent, HandleSessionEvent); Console.WriteLine("Session successfully created."); // Connect the session. Console.WriteLine("About to connect the session ..."); if (session.Connect() == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Session successfully connected"); Console.WriteLine(GetRouterInfo(session)); } // Validate required capabilities. if (!session.IsCapable(CapabilityType.PUB_GUARANTEED)) { Console.WriteLine(string.Format("This sample requires capability '{0}' to be supported", CapabilityType.PUB_GUARANTEED)); return; } // Send cmdLineParser.Config.NumberOfMessagesToPublish messages. for (int i = 0; i < cmdLineParser.Config.NumberOfMessagesToPublish; i++) { // Allocate a new message. IMessage message = SampleUtils.CreateMessage(cmdLineParser.Config, session); message.DeliveryMode = MessageDeliveryMode.Persistent; try { // Create a record, and set it as CorrelationKey. MessageRecord msgRecord = new MessageRecord(message); message.CorrelationKey = msgRecord; ReturnCode rc = session.Send(message); Console.WriteLine("Sending message " + i + ": " + rc); if (rc == ReturnCode.SOLCLIENT_OK) { // Add it to the list of send message records and send it. msgRecord.MessageId = i; msgRecords.AddLast(msgRecord); } else { // The message was not sent, free it up message.Dispose(); } } catch (OperationErrorException opex) { // Ignore OperationErrorException if you don't want the publisher // to abort on transient send errors Console.WriteLine("Got an excpetion " + opex.ReturnCode); message.Dispose(); continue; } // Sleep for 500 msecs and check to see if the message was acknowledged (positively or negatively). Thread.Sleep(100); while (msgRecords.First != null && msgRecords.First.Value.Acked) { MessageRecord record = msgRecords.First.Value; msgRecords.RemoveFirst(); Console.WriteLine( string.Format("Freeing memory for message {0}, Result: Acked ({1}), Accepted ({2})\n", record.MessageId, record.Acked, record.Accepted)); record.Message.Dispose(); } } } catch (Exception ex) { PrintException(ex); } finally { Thread.Sleep(3000); // There should not be any left in the list, but just in case. foreach (MessageRecord record in msgRecords) { Console.WriteLine( string.Format("Freeing memory for message {0}, Result: Acked ({1}), Accepted ({2})\n", record.MessageId, record.Acked, record.Accepted)); record.Message.Dispose(); } if (session != null) { session.Dispose(); } if (context != null) { context.Dispose(); } // Must cleanup after. CleanupContext(); } }