Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datagrid; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.FileNotFoundException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
Brian O'Connor | 119672d | 2014-08-03 21:36:58 -0700 | [diff] [blame^] | 8 | import java.util.concurrent.BlockingQueue; |
| 9 | import java.util.concurrent.ConcurrentMap; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 10 | |
| 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 16 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.datagrid.web.DatagridWebRoutable; |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 18 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import com.hazelcast.config.Config; |
| 23 | import com.hazelcast.config.FileSystemXmlConfig; |
| 24 | import com.hazelcast.core.Hazelcast; |
| 25 | import com.hazelcast.core.HazelcastInstance; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * A datagrid service that uses Hazelcast as a datagrid. |
| 29 | * The relevant data is stored in the Hazelcast datagrid and shared as |
| 30 | * appropriate in a multi-node cluster. |
| 31 | */ |
Brian O'Connor | 119672d | 2014-08-03 21:36:58 -0700 | [diff] [blame^] | 32 | public class HazelcastDatagrid implements IFloodlightModule, IDatagridService, |
| 33 | ISharedCollectionsService { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 34 | static final Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class); |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * The name of Hazelcast instance in this JVM. |
| 38 | */ |
| 39 | public static final String ONOS_HAZELCAST_INSTANCE = "ONOS_HazelcastInstance"; |
| 40 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 41 | private IRestApiService restApi; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 42 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 43 | static final String HAZELCAST_CONFIG_FILE = "datagridConfig"; |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 44 | private static final String HAZELCAST_DEFAULT_XML = "conf/hazelcast.default.xml"; |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 45 | private HazelcastInstance hazelcastInstance; |
| 46 | private Config hazelcastConfig; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 47 | |
Pavlin Radoslavov | 07af5f2 | 2014-03-21 15:17:58 -0700 | [diff] [blame] | 48 | // |
| 49 | // NOTE: eventChannels is kept thread safe by using explicit "synchronized" |
| 50 | // blocks below. Those are needed to protect the integrity of each entry |
| 51 | // instance, and avoid preemption during channel creation/startup. |
| 52 | // |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 53 | private final Map<String, IEventChannel<?, ?>> eventChannels = new HashMap<>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 54 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 55 | /** |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 56 | * Load the Hazelcast Datagrid configuration file. |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 57 | * |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 58 | * @param configFilename the configuration filename. |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 59 | * @return Hazelcast configuration |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 60 | */ |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 61 | public static Config loadHazelcastConfig(String configFilename) { |
| 62 | |
| 63 | Config hzConfig = null; |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 64 | /* |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 65 | System.setProperty("hazelcast.socket.receive.buffer.size", "32"); |
| 66 | System.setProperty("hazelcast.socket.send.buffer.size", "32"); |
| 67 | */ |
| 68 | // System.setProperty("hazelcast.heartbeat.interval.seconds", "100"); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 69 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 70 | // Init from configuration file |
| 71 | try { |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 72 | hzConfig = new FileSystemXmlConfig(configFilename); |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 73 | } catch (FileNotFoundException e) { |
| 74 | log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 75 | |
| 76 | // Fallback mechanism to support running unit test without setup. |
| 77 | log.error("Falling back to default Hazelcast XML {}", HAZELCAST_DEFAULT_XML); |
| 78 | try { |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 79 | hzConfig = new FileSystemXmlConfig(HAZELCAST_DEFAULT_XML); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 80 | } catch (FileNotFoundException e2) { |
| 81 | log.error("Error opening fall back Hazelcast XML configuration. " |
| 82 | + "File not found: " + HAZELCAST_DEFAULT_XML, e2); |
| 83 | // XXX probably should throw some exception to kill ONOS instead. |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 84 | hzConfig = new Config(); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 85 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 86 | } |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 87 | |
| 88 | // set the name of Hazelcast instance in this JVM. |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 89 | hzConfig.setInstanceName(ONOS_HAZELCAST_INSTANCE); |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 90 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 91 | /* |
| 92 | hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1"); |
| 93 | hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1"); |
| 94 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1"); |
| 95 | */ |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 96 | |
| 97 | return hzConfig; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Shutdown the Hazelcast Datagrid operation. |
| 102 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 103 | @Override |
| 104 | protected void finalize() { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 105 | close(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Shutdown the Hazelcast Datagrid operation. |
| 110 | */ |
| 111 | public void close() { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 112 | Hazelcast.shutdownAll(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the collection of offered module services. |
| 117 | * |
| 118 | * @return the collection of offered module services. |
| 119 | */ |
| 120 | @Override |
| 121 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 122 | Collection<Class<? extends IFloodlightService>> l = |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 123 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 124 | l.add(IDatagridService.class); |
Brian O'Connor | 119672d | 2014-08-03 21:36:58 -0700 | [diff] [blame^] | 125 | l.add(ISharedCollectionsService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 126 | return l; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the collection of implemented services. |
| 131 | * |
| 132 | * @return the collection of implemented services. |
| 133 | */ |
| 134 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 135 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 136 | getServiceImpls() { |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 137 | Map<Class<? extends IFloodlightService>, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 138 | IFloodlightService> m = |
| 139 | new HashMap<Class<? extends IFloodlightService>, |
| 140 | IFloodlightService>(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 141 | m.put(IDatagridService.class, this); |
Brian O'Connor | 119672d | 2014-08-03 21:36:58 -0700 | [diff] [blame^] | 142 | m.put(ISharedCollectionsService.class, this); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 143 | return m; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the collection of modules this module depends on. |
| 148 | * |
| 149 | * @return the collection of modules this module depends on. |
| 150 | */ |
| 151 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 152 | public Collection<Class<? extends IFloodlightService>> |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 153 | getModuleDependencies() { |
| 154 | Collection<Class<? extends IFloodlightService>> l = |
| 155 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 156 | l.add(IFloodlightProviderService.class); |
| 157 | l.add(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 158 | return l; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Initialize the module. |
| 163 | * |
| 164 | * @param context the module context to use for the initialization. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 165 | * @throws FloodlightModuleException on error |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 166 | */ |
| 167 | @Override |
| 168 | public void init(FloodlightModuleContext context) |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 169 | throws FloodlightModuleException { |
| 170 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 171 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 172 | // Get the configuration file name and configure the Datagrid |
| 173 | Map<String, String> configMap = context.getConfigParams(this); |
| 174 | String configFilename = configMap.get(HAZELCAST_CONFIG_FILE); |
Yuta HIGUCHI | 8aa2f9b | 2014-07-28 11:33:45 -0700 | [diff] [blame] | 175 | hazelcastConfig = loadHazelcastConfig(configFilename); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Startup module operation. |
| 180 | * |
| 181 | * @param context the module context to use for the startup. |
| 182 | */ |
| 183 | @Override |
| 184 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 185 | hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig); |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 186 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 187 | restApi.addRestletRoutable(new DatagridWebRoutable()); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 191 | * Create an event channel. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 192 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 193 | * If the channel already exists, just return it. |
| 194 | * NOTE: The channel is started automatically. |
| 195 | * |
| 196 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 197 | * @param <K> the type of the Key in the Key-Value store. |
| 198 | * @param <V> the type of the Value in the Key-Value store. |
| 199 | * @param typeK the type of the Key in the Key-Value store. |
| 200 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 201 | * @return the event channel for the channel name. |
| 202 | */ |
| 203 | @Override |
| 204 | public <K, V> IEventChannel<K, V> createChannel(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 205 | Class<K> typeK, Class<V> typeV) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 206 | synchronized (eventChannels) { |
| 207 | IEventChannel<K, V> eventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 208 | createChannelImpl(channelName, typeK, typeV); |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 209 | eventChannel.startup(); |
| 210 | return eventChannel; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 211 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Create an event channel implementation. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 216 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 217 | * If the channel already exists, just return it. |
| 218 | * NOTE: The caller must call IEventChannel.startup() to startup the |
| 219 | * channel operation. |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 220 | * NOTE: The caller must own the lock on "eventChannels". |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 221 | * |
| 222 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 223 | * @param <K> the type of the Key in the Key-Value store. |
| 224 | * @param <V> the type of the Value in the Key-Value store. |
| 225 | * @param typeK the type of the Key in the Key-Value store. |
| 226 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 227 | * @return the event channel for the channel name. |
| 228 | */ |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 229 | private <K, V> IEventChannel<K, V> createChannelImpl( |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 230 | String channelName, |
| 231 | Class<K> typeK, Class<V> typeV) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 232 | IEventChannel<?, ?> genericEventChannel = |
| 233 | eventChannels.get(channelName); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 234 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 235 | // Add the channel if the first listener |
| 236 | if (genericEventChannel == null) { |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 237 | IEventChannel<K, V> castedEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 238 | new HazelcastEventChannel<K, V>(hazelcastInstance, |
| 239 | channelName, typeK, typeV); |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 240 | eventChannels.put(channelName, castedEventChannel); |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 241 | return castedEventChannel; |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 242 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 243 | |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 244 | // |
| 245 | // TODO: Find if we can use Java internal support to check for |
| 246 | // type mismatch. |
| 247 | // |
| 248 | if (!genericEventChannel.verifyKeyValueTypes(typeK, typeV)) { |
| 249 | throw new ClassCastException("Key-value type mismatch for event channel " + channelName); |
| 250 | } |
| 251 | @SuppressWarnings("unchecked") |
| 252 | IEventChannel<K, V> castedEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 253 | (IEventChannel<K, V>) genericEventChannel; |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 254 | return castedEventChannel; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Add event channel listener. |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 259 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 260 | * NOTE: The channel is started automatically right after the listener |
| 261 | * is added. |
| 262 | * |
| 263 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 264 | * @param listener the listener to add. |
| 265 | * @param <K> the type of the Key in the Key-Value store. |
| 266 | * @param <V> the type of the Value in the Key-Value store. |
| 267 | * @param typeK the type of the Key in the Key-Value store. |
| 268 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 269 | * @return the event channel for the channel name. |
| 270 | */ |
| 271 | @Override |
| 272 | public <K, V> IEventChannel<K, V> addListener(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 273 | IEventChannelListener<K, V> listener, |
| 274 | Class<K> typeK, Class<V> typeV) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 275 | synchronized (eventChannels) { |
| 276 | IEventChannel<K, V> eventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 277 | createChannelImpl(channelName, typeK, typeV); |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 278 | eventChannel.addListener(listener); |
| 279 | eventChannel.startup(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 280 | |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 281 | return eventChannel; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 282 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Remove event channel listener. |
| 287 | * |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 288 | * @param <K> the type of the Key in the Key-Value store. |
| 289 | * @param <V> the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 290 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 291 | * @param listener the listener to remove. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 292 | */ |
| 293 | @Override |
| 294 | public <K, V> void removeListener(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 295 | IEventChannelListener<K, V> listener) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 296 | synchronized (eventChannels) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 297 | IEventChannel<?, ?> genericEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 298 | eventChannels.get(channelName); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 299 | |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 300 | if (genericEventChannel != null) { |
| 301 | // |
| 302 | // TODO: Find if we can use Java internal support to check for |
| 303 | // type mismatch. |
| 304 | // NOTE: Using "ClassCastException" exception below doesn't |
| 305 | // work. |
| 306 | // |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 307 | @SuppressWarnings("unchecked") |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 308 | IEventChannel<K, V> castedEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 309 | (IEventChannel<K, V>) genericEventChannel; |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 310 | castedEventChannel.removeListener(listener); |
| 311 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 312 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 313 | } |
Brian O'Connor | 119672d | 2014-08-03 21:36:58 -0700 | [diff] [blame^] | 314 | |
| 315 | /** |
| 316 | * Create an shared, concurrent map. |
| 317 | * |
| 318 | * @param mapName the shared map name. |
| 319 | * @param typeK the type of the Key in the map. |
| 320 | * @param typeV the type of the Value in the map. |
| 321 | * @return the shared map for the channel name. |
| 322 | */ |
| 323 | @Override |
| 324 | public <K, V> ConcurrentMap<K, V> getConcurrentMap(String mapName, Class<K> typeK, |
| 325 | Class<V> typeV) { |
| 326 | return hazelcastInstance.getMap(mapName); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Create an shared, blocking queue. |
| 331 | * |
| 332 | * @param queueName the shared queue name. |
| 333 | * @param typeT the type of the queue. |
| 334 | * @return the shared queue for the queue name. |
| 335 | */ |
| 336 | @Override |
| 337 | public <T> BlockingQueue<T> getBlockingQueue(String queueName, Class<T> typeT) { |
| 338 | return hazelcastInstance.getQueue(queueName); |
| 339 | } |
| 340 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 341 | } |