PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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