/hazelcast-client/src/main/java/com/hazelcast/client/QueueClientProxy.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 207 lines · 159 code · 33 blank · 15 comment · 18 complexity · 7a288770142b616badb759dbc5ff7bb2 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.client;
  17. import com.hazelcast.client.impl.QueueItemListenerManager;
  18. import com.hazelcast.core.IQueue;
  19. import com.hazelcast.core.ItemListener;
  20. import com.hazelcast.core.Prefix;
  21. import com.hazelcast.impl.ClusterOperation;
  22. import com.hazelcast.impl.Keys;
  23. import com.hazelcast.monitor.LocalQueueStats;
  24. import com.hazelcast.nio.Data;
  25. import java.util.AbstractQueue;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.List;
  29. import java.util.concurrent.TimeUnit;
  30. import static com.hazelcast.client.IOUtil.toObject;
  31. import static com.hazelcast.client.ProxyHelper.check;
  32. public class QueueClientProxy<E> extends AbstractQueue<E> implements IQueue<E> {
  33. final protected ProxyHelper proxyHelper;
  34. final protected String name;
  35. final Object lock = new Object();
  36. public QueueClientProxy(HazelcastClient hazelcastClient, String name) {
  37. super();
  38. this.name = name;
  39. proxyHelper = new ProxyHelper(name, hazelcastClient);
  40. }
  41. public String getName() {
  42. return name.substring(Prefix.QUEUE.length());
  43. }
  44. public InstanceType getInstanceType() {
  45. return InstanceType.QUEUE;
  46. }
  47. public void destroy() {
  48. proxyHelper.destroy();
  49. }
  50. public Object getId() {
  51. return name;
  52. }
  53. @Override
  54. public String toString() {
  55. return "Queue{" +
  56. "name='" + name + '\'' +
  57. '}';
  58. }
  59. @Override
  60. public int hashCode() {
  61. return name.hashCode();
  62. }
  63. public LocalQueueStats getLocalQueueStats() {
  64. throw new UnsupportedOperationException();
  65. }
  66. public boolean offer(E e) {
  67. check(e);
  68. return innerOffer(e, 0);
  69. }
  70. public E poll() {
  71. return innerPoll(0);
  72. }
  73. public E peek() {
  74. return (E) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_PEEK, null, null);
  75. }
  76. public boolean offer(E e, long l, TimeUnit timeUnit) throws InterruptedException {
  77. check(e);
  78. ProxyHelper.checkTime(l, timeUnit);
  79. l = (l < 0) ? 0 : l;
  80. if (e == null) {
  81. throw new NullPointerException();
  82. }
  83. return innerOffer(e, timeUnit.toMillis(l));
  84. }
  85. private boolean innerOffer(E e, long millis) {
  86. return (Boolean) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_OFFER, e, millis);
  87. }
  88. public E poll(long l, TimeUnit timeUnit) throws InterruptedException {
  89. ProxyHelper.checkTime(l, timeUnit);
  90. l = (l < 0) ? 0 : l;
  91. return innerPoll(timeUnit.toMillis(l));
  92. }
  93. private E innerPoll(long millis) {
  94. return (E) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_POLL, null, millis);
  95. }
  96. public E take() throws InterruptedException {
  97. return innerPoll(-1);
  98. }
  99. public void put(E e) throws InterruptedException {
  100. check(e);
  101. innerOffer(e, -1);
  102. }
  103. public int remainingCapacity() {
  104. return (Integer) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_REMAINING_CAPACITY, null, null);
  105. }
  106. public int drainTo(Collection<? super E> objects) {
  107. return drainTo(objects, Integer.MAX_VALUE);
  108. }
  109. public int drainTo(Collection<? super E> objects, int i) {
  110. if (objects == null) throw new NullPointerException("drainTo null!");
  111. if (i < 0) throw new IllegalArgumentException("Negative maxElements:" + i);
  112. if (i == 0) return 0;
  113. if (objects instanceof IQueue) {
  114. if (((IQueue) objects).getName().equals(getName())) {
  115. throw new IllegalArgumentException("Cannot drainTo self!");
  116. }
  117. }
  118. E e;
  119. int counter = 0;
  120. while (counter < i && (e = poll()) != null) {
  121. objects.add(e);
  122. counter++;
  123. }
  124. return counter;
  125. }
  126. @Override
  127. public int size() {
  128. return (Integer) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_SIZE, null, null);
  129. }
  130. @Override
  131. public boolean equals(Object o) {
  132. if (o instanceof IQueue && o != null) {
  133. return getName().equals(((IQueue) o).getName());
  134. } else {
  135. return false;
  136. }
  137. }
  138. @Override
  139. public boolean remove(Object o) {
  140. return (Boolean) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_REMOVE, null, o);
  141. }
  142. @Override
  143. public java.util.Iterator<E> iterator() {
  144. Keys keys = (Keys) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_ENTRIES, null, null);
  145. List<E> list = new ArrayList<E>();
  146. for (Data d : keys) {
  147. list.add((E) toObject(d.buffer));
  148. }
  149. return new QueueItemIterator(list.toArray(), this);
  150. }
  151. public void addItemListener(ItemListener<E> listener, boolean includeValue) {
  152. check(listener);
  153. synchronized (lock) {
  154. boolean shouldCall = listenerManager().noListenerRegistered(name);
  155. listenerManager().registerListener(name, listener, includeValue);
  156. if (shouldCall) {
  157. Call c = listenerManager().createNewAddItemListenerCall(proxyHelper, includeValue);
  158. proxyHelper.doCall(c);
  159. }
  160. }
  161. }
  162. public void removeItemListener(ItemListener<E> listener) {
  163. check(listener);
  164. synchronized (lock) {
  165. listenerManager().removeListener(name, listener);
  166. Packet request = proxyHelper.createRequestPacket(ClusterOperation.REMOVE_LISTENER, null, null);
  167. Call c = proxyHelper.createCall(request);
  168. proxyHelper.doCall(c);
  169. }
  170. }
  171. private QueueItemListenerManager listenerManager() {
  172. return proxyHelper.getHazelcastClient().getListenerManager().getQueueItemListenerManager();
  173. }
  174. }