main
1using System;
2using System.IO;
3using System.Transactions;
4using Gorilla.Commons.Infrastructure.Logging;
5using gorilla.commons.utility;
6using ProtoBuf;
7using Rhino.Queues;
8
9namespace presentation.windows.common
10{
11 public class RhinoPublisher : ServiceBus
12 {
13 readonly int port;
14 string destination_queue;
15 IQueueManager sender;
16
17 public RhinoPublisher(string destination_queue, int port, IQueueManager manager)
18 {
19 this.port = port;
20 this.destination_queue = destination_queue;
21 sender = manager;
22 }
23
24 public void publish<T>() where T : new()
25 {
26 publish(new T());
27 }
28
29 public void publish<T>(T item) where T : new()
30 {
31 using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew))
32 {
33 var destination = "rhino.queues://localhost:{0}/{1}".format(port, destination_queue);
34 this.log().debug("sending {0} to {1}", item, destination);
35 sender.Send(new Uri(destination), create_payload_from(item));
36 transaction.Complete();
37 }
38 }
39
40 MessagePayload create_payload_from<T>(T item)
41 {
42 using (var stream = new MemoryStream())
43 {
44 Serializer.Serialize(stream, item);
45
46 var payload = new MessagePayload {Data = stream.ToArray()};
47 payload.Headers["type"] = typeof (T).FullName;
48 return payload;
49 }
50 }
51
52 public void publish<T>(Action<T> configure) where T : new()
53 {
54 var item = new T();
55 configure(item);
56 publish(item);
57 }
58 }
59}