1 7 package org.jboss.jms.serverless; 8 9 import org.jboss.logging.Logger; 10 import org.jgroups.Address; 11 import java.util.Map ; 12 import java.util.HashMap ; 13 import java.util.List ; 14 import java.util.ArrayList ; 15 import org.jgroups.util.Util; 16 import java.util.Iterator ; 17 18 24 class GroupState { 25 26 private static final Logger log = Logger.getLogger(GroupState.class); 27 28 private Map queues; 29 30 public synchronized byte[] toByteBuffer() throws Exception { 31 return Util.objectToByteBuffer(queues); 32 } 33 34 public synchronized void fromByteBuffer(byte[] ba) throws Exception { 35 Object o = Util.objectFromByteBuffer(ba); 36 if (o == null) { 37 queues = null; 38 } 39 else if (o instanceof Map ) { 40 queues = (Map )o; 41 } 42 else { 43 throw new IllegalStateException ("Invalid group state"); 44 } 45 } 46 47 48 public synchronized void addQueueReceiver(String queueName, Address addr, String sessionID, 49 String queueReceiverID) { 50 if (queues == null) { 51 queues = new HashMap (); 52 } 53 List l = (List )queues.get(queueName); 54 if (l == null) { 55 l = new ArrayList (); 56 queues.put(queueName, l); 57 } 58 QueueReceiverAddress ra = new QueueReceiverAddress(addr, sessionID, queueReceiverID); 59 if (l.contains(ra)) { 60 log.warn(ra+" already in the group state"); 61 return; 62 } 63 l.add(ra); 64 log.debug("New GroupState: "+toString()); 65 } 66 67 70 public synchronized void removeQueueReceiver(String queueName, Address addr, String sessionID, 71 String queueReceiverID) { 72 73 String noSuchReceiverMsg = 74 "No such queue receiver: "+queueName+"/"+addr+"/"+sessionID+"/"+queueReceiverID; 75 76 List l = null; 77 78 if (queues == null || 79 ((l = (List )queues.get(queueName)) == null) || 80 l.isEmpty()) { 81 log.warn(noSuchReceiverMsg); 82 } 83 if (!l.remove(new QueueReceiverAddress(addr, sessionID, queueReceiverID))) { 84 log.warn(noSuchReceiverMsg); 85 } 86 log.debug("New GroupState: "+toString()); 87 } 88 89 90 93 public synchronized QueueReceiverAddress selectReceiver(String queueName) { 94 95 if (queues == null) { 96 return null; 97 } 98 List l = (List )queues.get(queueName); 99 if (l == null || l.size() == 0) { 100 return null; 101 } 102 QueueReceiverAddress selected = null; 103 int crtidx = 0; 104 for(Iterator i = l.iterator(); i.hasNext(); crtidx++) { 105 QueueReceiverAddress crt = (QueueReceiverAddress)i.next(); 106 if (crt.isNextForDelivery()) { 107 selected = crt; 108 crt.setNextForDelivery(false); 109 ((QueueReceiverAddress)l.get((crtidx + 1) % l.size())).setNextForDelivery(true); 110 break; 111 } 112 } 113 if (selected == null) { 114 selected = (QueueReceiverAddress)l.get(0); 115 ((QueueReceiverAddress)l.get(1 % l.size())).setNextForDelivery(true);; 116 117 } 118 return selected; 119 } 120 121 122 public String toString() { 123 return queues == null ? "null" : queues.toString(); 124 } 125 126 } 127 | Popular Tags |