If you don't understand what a topic is please refer to this link: How to register a new topic

There are two ways to publish messages. You can publish a single message or publish a collection of a specific message. In order to be able to publish a message, you need to identify which topic you want to publish the message to. You will need to first select the topic either by the name(string based) or based on the Type that match the topic(matches are determined by type name). The following does the same thing but uses two different approach. One select using a Generic Type constraint and the other using a string name

Note: Publishing using a collection approach would batch the message into a single call which will in turn save the need of having to iterate a collection one at a time. This approach should be used when you want to gain performance for publishing multiple messages of the same topic and also when you want to specific the order of your message

//Retrieve Topic object by using the type name as the topic name
var topic = Topic.Select<MyTopic>() 

//Retrieve Topic object by using a string based name
var topic = Topic.Select("MyTopic")

 

Publishing a single message

There are fours ways to go about publishing a single message.

//Explicitly add the parameter for topic message
topic.SetParameter("Param1", "Value1")
.SetParameter("Param2", "Value2")
.Publish()

//Map an anonymous object or any instance of a class that matches the topic contract
//This option allow you to build up the parameter that would be published from one or more object as needed
topic.Message(new {Param1 = "Value1", Param2 = "Value2"})
.Publish()

//Map with instance of a class
topic.Message(new MyTopic(){ Param1 = "Value1", Param2 = "Value2" })
.Publish();

//Publish using anonymous object
topic.Publish(new {Param1 = "Value1", Param2 = "Value2"})

//Publish using instance of a class
topic.Publish(new MyTopic(){ Param1 = "Value1", Param2 = "Value2" })

//Publish message and infer the specific topic to publish to by using the class name
//This approach does not need the instance of topic object since it is inferring the topic name
Topic.PublishMessage(new MyTopic(){ Param1 = "Value1", Param2 = "Value2"})



 

Publishing multiple message for a topic

There are three ways to publish messages in batches

//Publish using a collection of anonymous objects
topic.PublishMany(new List<object>(){ 
	new {Param1 = "Value1", Param2 = "Value2"},
	new {Param1 = "Value1", Param2 = "Value2"}
} );

//Publish using a collection of instance of a class
topic.PublishMany(new List<MyTopic>(){
	new MyTopic(){ Param1 = "Value1", Param2 = "Value2"},
	new MyTopic(){ Param1 = "Value1", Param2 = "Value2"}
});

//Publish by infer the topic name from the type constrain of the collection
Topic.PublishMessages(new List<MyTopic>(){
	new MyTopic(){ Param1 = "Value1", Param2 = "Value2"},
	new MyTopic(){ Param1 = "Value1", Param2 = "Value2"}
});

Last edited Dec 2, 2011 at 4:42 AM by rpgmaker, version 4

Comments

No comments yet.