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