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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 106 lines · 73 code · 18 blank · 15 comment · 3 complexity · 1e694012d15d0b2234734a8421ad7713 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.MessageListenerManager;
  18. import com.hazelcast.core.ITopic;
  19. import com.hazelcast.core.MessageListener;
  20. import com.hazelcast.core.Prefix;
  21. import com.hazelcast.impl.ClusterOperation;
  22. import com.hazelcast.monitor.LocalTopicStats;
  23. import static com.hazelcast.client.ProxyHelper.check;
  24. public class TopicClientProxy<T> implements ITopic {
  25. private final String name;
  26. private final ProxyHelper proxyHelper;
  27. private final Object lock = new Object();
  28. public TopicClientProxy(HazelcastClient hazelcastClient, String name) {
  29. this.name = name;
  30. proxyHelper = new ProxyHelper(name, hazelcastClient);
  31. }
  32. public String getName() {
  33. return name.substring(Prefix.TOPIC.length());
  34. }
  35. public void publish(Object message) {
  36. check(message);
  37. proxyHelper.doFireAndForget(ClusterOperation.TOPIC_PUBLISH, message, null);
  38. }
  39. public void addMessageListener(MessageListener messageListener) {
  40. check(messageListener);
  41. synchronized (lock) {
  42. boolean shouldCall = messageListenerManager().noListenerRegistered(name);
  43. messageListenerManager().registerListener(name, messageListener);
  44. if (shouldCall) {
  45. doAddListenerCall(messageListener);
  46. }
  47. }
  48. }
  49. private void doAddListenerCall(MessageListener messageListener) {
  50. Call c = messageListenerManager().createNewAddListenerCall(proxyHelper);
  51. proxyHelper.doCall(c);
  52. }
  53. public void removeMessageListener(MessageListener messageListener) {
  54. check(messageListener);
  55. synchronized (lock) {
  56. messageListenerManager().removeListener(name, messageListener);
  57. if (messageListenerManager().noListenerRegistered(name)) {
  58. proxyHelper.doOp(ClusterOperation.REMOVE_LISTENER, null, null);
  59. }
  60. }
  61. }
  62. private MessageListenerManager messageListenerManager() {
  63. return proxyHelper.getHazelcastClient().getListenerManager().getMessageListenerManager();
  64. }
  65. public InstanceType getInstanceType() {
  66. return InstanceType.TOPIC;
  67. }
  68. public void destroy() {
  69. proxyHelper.destroy();
  70. }
  71. public Object getId() {
  72. return name;
  73. }
  74. @Override
  75. public boolean equals(Object o) {
  76. if (o instanceof ITopic) {
  77. return getName().equals(((ITopic) o).getName());
  78. }
  79. return false;
  80. }
  81. @Override
  82. public int hashCode() {
  83. return getName().hashCode();
  84. }
  85. public LocalTopicStats getLocalTopicStats() {
  86. throw new UnsupportedOperationException();
  87. }
  88. }