1 package net.walend.somnifugi.juc; 2 3 import java.util.Comparator ; 4 import java.util.Enumeration ; 5 import java.util.List ; 6 import java.util.ArrayList ; 7 8 import java.util.concurrent.TimeUnit ; 9 import java.util.concurrent.BlockingQueue ; 10 import java.util.concurrent.LinkedBlockingQueue ; 11 12 import javax.jms.Message ; 13 14 import net.walend.somnifugi.SomniRuntimeException; 15 import net.walend.somnifugi.SomniMessageSelector; 16 import net.walend.somnifugi.SomniMessageSelectorException; 17 18 import net.walend.somnifugi.channel.Channel; 19 import net.walend.somnifugi.channel.Puttable; 20 import net.walend.somnifugi.channel.Takable; 21 22 27 28 public class SimpleChannel 29 implements Channel<Message >,Puttable<Message >,Takable<Message > 30 { 31 private BlockingQueue <Message > queue; 32 33 public SimpleChannel() 34 { 35 queue = new LinkedBlockingQueue <Message >(); 36 } 37 38 public SimpleChannel(int capacity) 39 { 40 queue = new LinkedBlockingQueue <Message >(capacity); 41 } 42 43 49 public boolean offer(Message elem,long timeout) 50 throws InterruptedException 51 { 52 return queue.offer(elem,timeout,TimeUnit.MILLISECONDS); 53 } 54 55 58 public void put(Message elem) 59 throws InterruptedException 60 { 61 queue.put(elem); 62 } 63 64 68 public Message poll() 69 { 70 return queue.poll(); 71 } 72 73 76 public Message poll(long timeout) 77 throws InterruptedException 78 { 79 return queue.poll(timeout,TimeUnit.MILLISECONDS); 80 } 81 82 85 public Message take() 86 throws InterruptedException 87 { 88 return queue.take(); 89 } 90 91 94 public void pushBack(Message elem) 95 throws InterruptedException 96 { 97 queue.put(elem); 98 } 99 100 public Message peek() 101 { 102 return queue.peek(); 103 } 104 105 public boolean hasRealPushback() 107 { 108 return false; 109 } 110 111 public boolean supportsPriorities() 112 { 113 return false; 114 } 115 116 public boolean supportsMessageSelectors() 117 { 118 return false; 119 } 120 121 public Enumeration snapShot() 122 { 123 List <Message > snap = new ArrayList <Message >(queue); 124 125 return new EnumerationBridge<Message >(snap.iterator()); 126 } 127 128 public Enumeration snapShot(SomniMessageSelector messageSelector) 129 throws SomniMessageSelectorException 130 { 131 List <Message > snap = new ArrayList <Message >(queue.size()); 132 133 for(Message Message : queue) 134 { 135 if(messageSelector.matches(Message)) 136 { 137 snap.add(Message); 138 } 139 } 140 141 return new EnumerationBridge<Message >(snap.iterator()); 142 } 143 144 public Puttable<Message > getPuttable() 145 { 146 return this; 147 } 148 149 public Takable<Message > getTakable() 150 { 151 return this; 152 } 153 154 public Takable<Message > getTakable(SomniMessageSelector messageSelector) 155 { 156 throw new UnsupportedOperationException (getClass().getName()+" does not support message selectors."); 157 } 158 159 public int guessSize() 160 { 161 return queue.size(); 162 } 163 } 164 165 185 | Popular Tags |