Insert message into Service Bus Queue and Topic , Run Webjob

//public enum LocalizationCacheState
//{
// FillCache,
// RefreshCache
//}

//private void LoadEventUsingQueue()
//{
// string queueName = “TestQueue”;

// // Create a new Queue with custom settings
// string connectionString =
// CloudConfigurationManager.GetSetting(“Microsoft.ServiceBus.ConnectionString”);

// var namespaceManager =
// NamespaceManager.CreateFromConnectionString(connectionString);

// if (!namespaceManager.QueueExists(queueName))
// {
// namespaceManager.CreateQueue(new QueueDescription(queueName) { RequiresDuplicateDetection = true, DuplicateDetectionHistoryTimeWindow = new TimeSpan(0, 0, 10) });
// }

// // Get messageFactory for runtime operation
// MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);

// QueueClient queueClient = messagingFactory.CreateQueueClient(queueName);

// BrokeredMessage message = new BrokeredMessage(“Localization”);

// //message.Properties[“CacheState”] = LocalizationCacheState.FillCache.ToString(); // “FetchCache”;

// long count = namespaceManager.GetQueue(queueName).MessageCount;

// if (count == 0)
// {
// queueClient.Send(message);
// }
// else if(message.Properties[“CacheState”].ToString() == “RefreshCache”)
// {
// queueClient.Send(message);
// }

//}
private void LoadEventUsingTopics()
{
string topicName = “TestTopic”;

// Create a new Topic with custom settings
string connectionString =
CloudConfigurationManager.GetSetting(“Microsoft.ServiceBus.ConnectionString”);

var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.TopicExists(topicName))
{
namespaceManager.CreateTopic(new TopicDescription(topicName) { RequiresDuplicateDetection = true, DuplicateDetectionHistoryTimeWindow = new TimeSpan(0, 10, 0) });
}

// Create a subscription with “Localization” filter
SqlFilter localizationMessagesFilter =
new SqlFilter(“SubscriptionType = ‘Localization'”);

if (!namespaceManager.SubscriptionExists(“TestTopic”, “Localization”))
{
namespaceManager.CreateSubscription(“TestTopic”,
“Localization”,
localizationMessagesFilter);
}

// Create a subscription with “Configuration” filter
SqlFilter configurationMessagesFilter =
new SqlFilter(“SubscriptionType = ‘Configuration'”);

if (!namespaceManager.SubscriptionExists(“TestTopic”, “Configuration”))
{
namespaceManager.CreateSubscription(“TestTopic”,
“Configuration”,
configurationMessagesFilter);
}

MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);

//QueueClient queueClient = messagingFactory.CreateQueueClient(topicName);
TopicClient topicClient =
messagingFactory.CreateTopicClient(“TestTopic”);

BrokeredMessage message = new BrokeredMessage(“Test Message”);

message.Properties[“CacheState”] = “CacheRefresh”;
message.Properties[“SubscriptionType”] = “Localization”;

var subscriptionDescLocalization = namespaceManager.GetSubscription(“TestTopic”, “Localization”);
long messageCountFirstTime = subscriptionDescLocalization.MessageCount;

//if ((messageCountFirstTime == 0) && (message.Properties[“CacheState”].ToString() == “CacheMiss”))
//{
// topicClient.Send(message);
//}
if (message.Properties[“CacheState”].ToString() == “CacheRefresh”)
{
topicClient.Send(message);
}
}

WebJob1 Project

Program.cs
namespace WebJob1
{
// To learn more about Microsoft Azure WebJobs SDK, please see http://go.microsoft.com/fwlink/?LinkID=320976
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
}

Functions.cs

public static void ProcessLocalizationMessages([ServiceBusTrigger(“TestTopic”, “Localization”)] BrokeredMessage message,
TextWriter logger)
{
//logger.WriteLine(“Webjob Start” + message.MessageId + DateTime.Now);
Console.WriteLine(“Webjob Start Localization {0} {1}”, message.MessageId, DateTime.Now);
Thread.Sleep(100000);
//logger.WriteLine(“Webjob End” + message.MessageId + DateTime.Now);
Console.WriteLine(“Webjob End Localization {0} {1}”, message.MessageId, DateTime.Now);
}

public static void ProcessConfigurationMessages([ServiceBusTrigger(“TestTopic”, “Configuration”)] BrokeredMessage message,
TextWriter logger)
{
//logger.WriteLine(“Webjob Start” + message.MessageId + DateTime.Now);
Console.WriteLine(“Webjob Start Configuration {0} {1}”, message.MessageId, DateTime.Now);
Thread.Sleep(100000);
//logger.WriteLine(“Webjob End” + message.MessageId + DateTime.Now);
Console.WriteLine(“Webjob End Configuration {0} {1}”, message.MessageId, DateTime.Now);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s