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