PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

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