-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessingQueue.cs
73 lines (59 loc) · 2.35 KB
/
ProcessingQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using System.Configuration;
namespace HowMuchTo.ProcessingQueue
{
public class ProcessingQueue : IProcessingQueue
{
private BinaryFormatter _formatter;
private CloudStorageAccount storageAccount;
private CloudQueueClient queueClient;
private CloudQueue queue;
private string TableName = "processingqueue";
public ProcessingQueue()
{
_formatter = new BinaryFormatter();
storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
queueClient = storageAccount.CreateCloudQueueClient();
queue = queueClient.GetQueueReference(TableName);
queue.CreateIfNotExist();
}
public void PutQueueMessage(MessageType type, Dictionary<string, long> data)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(1024 * 64);
data.Add("Type", (long)type);
_formatter.Serialize(stream, data);
CloudQueueMessage message = new CloudQueueMessage(stream.ToArray());
queue.AddMessage(message);
}
public ProcessingQueueItem GetNextMessage()
{
ProcessingQueueItem item = new ProcessingQueueItem();
CloudQueueMessage retrievedMessage = queue.GetMessage();
if (retrievedMessage == null)
return null;
queue.DeleteMessage(retrievedMessage);
try
{
item.Data = (Dictionary<string, long>)_formatter.Deserialize(new System.IO.MemoryStream(retrievedMessage.AsBytes));
item.Type = (MessageType)item.Data["Type"];
//queue.DeleteMessage(retrievedMessage);
}
catch (Exception e)
{
if (retrievedMessage.DequeueCount > 3)
{
// delete the 'poison pill'
//queue.DeleteMessage(retrievedMessage);
}
return null;
}
return item;
}
}
}