/// <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 and ISession and ITopic. IContext context = null; ISession session = null; ITopic topic = null; 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, PrintReceivedMessage, 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)); } topic = ContextFactory.Instance.CreateTopic(SampleUtils.SAMPLE_TOPIC); Console.WriteLine("About to subscribe to topic " + SampleUtils.SAMPLE_TOPIC); if (session.Subscribe(topic, true) == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Successfully added topic subscription"); } // Create the message independent stream. IStreamContainer stream = SDTUtils.CreateStream(1024); // Populate the stream. stream.AddDouble(3.141592654); stream.AddString("message"); // Create the message-independent map. IMapContainer map = SDTUtils.CreateMap(1024); // Add a well known integer to the map. map.AddInt32("mersenne", 43112609); // Create the message. IMessage message = ContextFactory.Instance.CreateMessage(); // Set the message delivery options. message.DeliveryMode = MessageDeliveryMode.Direct; message.Destination = topic; int numMsgsToSend = 10; Console.WriteLine(string.Format("About to send {0} messages ...", numMsgsToSend)); for (int i = 0; i < numMsgsToSend; i++) { // Overwrite the "message" field, set the container, and send. map.DeleteField("message"); map.AddString("message", "message" + (i + 1)); // Set the user property map to the map. message.UserPropertyMap = map; SDTUtils.SetSDTContainer(message, stream); session.Send(message); } // Dispose of the map, stream, and message. map.Dispose(); stream.Dispose(); message.Dispose(); Thread.Sleep(500); // wait for 0.5 seconds Console.WriteLine("\nDone"); } catch (Exception ex) { PrintException(ex); } finally { if (session != null) { if (topic != null) { session.Unsubscribe(topic, true); } 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; } cmdLineParser.Config.DeliveryMode = MessageDeliveryMode.Persistent; cmdLineParser.Config.DestMode = DestMode.QUEUE; #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; IFlow flow = null; 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, 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)); } if (!session.IsCapable(CapabilityType.SELECTOR)) { Console.WriteLine(string.Format("Capability '{0}' is required to run this sample", CapabilityType.SELECTOR)); return; } if (!session.IsCapable(CapabilityType.SUB_FLOW_GUARANTEED) || !(session.IsCapable(CapabilityType.TEMP_ENDPOINT))) { Console.WriteLine(string.Format("Capabilities '{0}' and {1} are required to run this sample", CapabilityType.SUB_FLOW_GUARANTEED, CapabilityType.TEMP_ENDPOINT)); return; } // The creation of the Queue object. Temporary destinations must be // acquired from a connected session, as they require knowledge // about the connected appliance. IQueue queue = session.CreateTemporaryQueue(); // The creation of a flow. A FlowReceiver is acquired for consuming // messages from a specified endpoint. // // The selector "pasta = 'rotini' OR pasta = 'farfalle'" is used to // select only messages matching those pasta types in their user // property map. FlowProperties flowProps = new FlowProperties(); flowProps.FlowStartState = true; // created in a started state; flowProps.Selector = "pasta = 'rotini' OR pasta = 'farfalle'"; flow = session.CreateFlow(flowProps, queue, null, SampleUtils.HandleMessageEvent, SampleUtils.HandleFlowEvent); // Now publish a number of messages to queue, the user should only get the ones with 'rotini' or 'farfalle'. // Note that this uses SDT and custom header properties, which could impact performance. IMessage message = SampleUtils.CreateMessage(cmdLineParser.Config, session); message.Destination = queue; string[] pastas = new string[] { "macaroni", "fettuccini", "farfalle", "fiori", "rotini", "penne" }; for (int i = 0; i < pastas.Length; i++) { IMapContainer userProps = message.CreateUserPropertyMap(); userProps.AddString("pasta", pastas[i]); if (session.Send(message) == ReturnCode.SOLCLIENT_OK) { Console.WriteLine(String.Format("- Sent {0}", pastas[i])); } } Console.WriteLine(string.Format("\nDone\n Sleeping for {0} secs before exiting. Expecting 2 messages to match the selector ", Timeout / 1000)); Thread.Sleep(Timeout); } catch (Exception ex) { PrintException(ex); } finally { if (flow != null) { flow.Dispose(); } if (session != null) { session.Dispose(); } if (context != null) { context.Dispose(); } // Must cleanup after. CleanupContext(); } }
private static void WriteCustomHeader(string key, object val, IMapContainer map) { if (val is string) { map.AddString(key, val as string); } else if (val is Int16 || val is short) { map.AddInt16(key, (short)val); } else if (val is Int32 || val is int) { map.AddInt32(key, (int)val); } else if (val is Int64 || val is long) { map.AddInt64(key, (long)val); } else if (val is bool) { map.AddBool(key, (bool)val); } else if (val is float) { map.AddFloat(key, (float)val); } else if (val is double) { map.AddDouble(key, (double)val); } else if (val is UInt16 || val is ushort) { map.AddUInt16(key, (short)val); } else if (val is UInt32 || val is uint) { map.AddUInt32(key, (uint)val); } else if (val is UInt64 || val is ulong) { map.AddUInt64(key, (long)val); } else if (val is byte) { map.AddUInt8(key, (byte)val); } else if (val is sbyte) { map.AddInt8(key, (sbyte)val); } else if (val is byte[]) { map.AddByteArray(key, (byte[])val); } else if (val is char) { map.AddChar(key, (char)val); } else { throw new MessagingException("Unsupported app header type" + val.GetType()); } }