/hazelcast-client/src/main/java/com/hazelcast/client/impl/MessageListenerManager.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 85 lines · 60 code · 10 blank · 15 comment · 9 complexity · fb985800a154c35a29b996288bf0d366 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.impl;
  17. import com.hazelcast.client.Call;
  18. import com.hazelcast.client.HazelcastClient;
  19. import com.hazelcast.client.Packet;
  20. import com.hazelcast.client.ProxyHelper;
  21. import com.hazelcast.core.MessageListener;
  22. import com.hazelcast.impl.ClusterOperation;
  23. import com.hazelcast.impl.DataMessage;
  24. import com.hazelcast.nio.Data;
  25. import java.util.ArrayList;
  26. import java.util.Collection;
  27. import java.util.List;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.concurrent.CopyOnWriteArrayList;
  30. public class MessageListenerManager {
  31. final private ConcurrentHashMap<String, List<MessageListener>> messageListeners = new ConcurrentHashMap<String, List<MessageListener>>();
  32. public void registerListener(String name, MessageListener messageListener) {
  33. List<MessageListener> newListenersList = new CopyOnWriteArrayList<MessageListener>();
  34. List<MessageListener> listeners = messageListeners.putIfAbsent(name, newListenersList);
  35. if (listeners == null) {
  36. listeners = newListenersList;
  37. }
  38. listeners.add(messageListener);
  39. }
  40. public void removeListener(String name, MessageListener messageListener) {
  41. if (!messageListeners.containsKey(name)) {
  42. return;
  43. }
  44. messageListeners.get(name).remove(messageListener);
  45. if (messageListeners.get(name).isEmpty()) {
  46. messageListeners.remove(name);
  47. }
  48. }
  49. public boolean noListenerRegistered(String name) {
  50. if (!messageListeners.containsKey(name)) {
  51. return true;
  52. }
  53. return messageListeners.get(name).isEmpty();
  54. }
  55. public void notifyMessageListeners(Packet packet) {
  56. List<MessageListener> list = messageListeners.get(packet.getName());
  57. if (list != null) {
  58. for (MessageListener<Object> messageListener : list) {
  59. messageListener.onMessage(new DataMessage(packet.getName(), new Data(packet.getKey())));
  60. }
  61. }
  62. }
  63. public Call createNewAddListenerCall(final ProxyHelper proxyHelper) {
  64. Packet request = proxyHelper.createRequestPacket(ClusterOperation.ADD_LISTENER, null, null);
  65. return proxyHelper.createCall(request);
  66. }
  67. public Collection<Call> calls(final HazelcastClient client) {
  68. final List<Call> calls = new ArrayList<Call>();
  69. for (final String name : messageListeners.keySet()) {
  70. final ProxyHelper proxyHelper = new ProxyHelper(name, client);
  71. calls.add(createNewAddListenerCall(proxyHelper));
  72. }
  73. return calls;
  74. }
  75. }