1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.pojo.notification; |
24 |
| |
25 |
| import java.util.Queue; |
26 |
| import java.util.concurrent.ConcurrentHashMap; |
27 |
| import java.util.concurrent.ConcurrentLinkedQueue; |
28 |
| import java.util.concurrent.ConcurrentMap; |
29 |
| |
30 |
| import javax.transaction.Transaction; |
31 |
| |
32 |
| import org.jboss.cache.pojo.notification.annotation.Attached; |
33 |
| import org.jboss.cache.pojo.notification.annotation.Detached; |
34 |
| import org.jboss.cache.pojo.notification.annotation.FieldModified; |
35 |
| import org.jboss.cache.pojo.notification.annotation.ListModified; |
36 |
| import org.jboss.cache.pojo.notification.annotation.MapModified; |
37 |
| import org.jboss.cache.pojo.notification.annotation.PojoCacheListener; |
38 |
| import org.jboss.cache.pojo.notification.annotation.SetModified; |
39 |
| import org.jboss.cache.pojo.notification.annotation.TransactionCompleted; |
40 |
| import org.jboss.cache.pojo.notification.event.Event; |
41 |
| import org.jboss.cache.pojo.notification.event.TransactionCompletedEvent; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| @PojoCacheListener |
51 |
| public class TxGauranteedListener |
52 |
| { |
53 |
| private class TxEventQueue |
54 |
| { |
55 |
| private ConcurrentMap<Transaction, Queue<Event>> map = new ConcurrentHashMap<Transaction, Queue<Event>>(); |
56 |
| |
57 |
9
| public void offer(Event event)
|
58 |
| { |
59 |
9
| Queue<Event> queue = getQueue(event.getContext().getTransaction());
|
60 |
9
| queue.offer(event);
|
61 |
| } |
62 |
| |
63 |
9
| private Queue<Event> getQueue(Transaction transaction)
|
64 |
| { |
65 |
9
| Queue<Event> queue = map.get(transaction);
|
66 |
9
| if (queue == null)
|
67 |
| { |
68 |
5
| queue = new ConcurrentLinkedQueue<Event>();
|
69 |
5
| map.putIfAbsent(transaction, queue);
|
70 |
| } |
71 |
| |
72 |
9
| return queue;
|
73 |
| } |
74 |
| |
75 |
5
| public Queue<Event> takeAll(Transaction transaction)
|
76 |
| { |
77 |
5
| return map.remove(transaction);
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
| private TxEventQueue events = new TxEventQueue(); |
82 |
| private Queue<Event> committed = new ConcurrentLinkedQueue<Event>(); |
83 |
| |
84 |
9
| @SuppressWarnings("unchecked")
|
85 |
| public <T extends Event> T take(Class<T> t) |
86 |
| { |
87 |
9
| Event notification = committed.remove();
|
88 |
9
| if (!t.isInstance(notification))
|
89 |
0
| throw new IllegalStateException("Expected notification type: " + t.getSimpleName() + " but was: " + notification.getClass().getSimpleName());
|
90 |
| |
91 |
9
| return (T) notification;
|
92 |
| } |
93 |
| |
94 |
9
| @Attached
|
95 |
| @Detached |
96 |
| @FieldModified |
97 |
| @ListModified |
98 |
| @SetModified |
99 |
| @MapModified |
100 |
| public void handle(Event event) |
101 |
| { |
102 |
9
| events.offer(event);
|
103 |
| } |
104 |
| |
105 |
5
| @TransactionCompleted
|
106 |
| public void handleTx(TransactionCompletedEvent event) |
107 |
| { |
108 |
5
| Queue<Event> completed = events.takeAll(event.getContext().getTransaction());
|
109 |
5
| if (completed != null && event.isSuccessful())
|
110 |
5
| committed.addAll(completed);
|
111 |
| } |
112 |
| } |