Şu satırı dahil ederiz
import com.hazelcast.core.IMap;
Açıklaması şöyle
com.hazelcast.map.IMap extends java.util.Map. So there is a lesser learning curve here. The distributed map implementation has a method to lock a specific key. If the lock is not available, the current thread is blocked until the lock has been released. We can get a lock on the key even if it is not present in the map. If the key does not exist in the map, any thread apart from the lock owner will get blocked if it tries to put the locked key in the map.
Aslında List arayüzü de aynı Java'daki gibi kullanılabiliyor. Açıklaması şöyle
The data structures are standard ones like Map, List, or Queue (in Java there are just other implementations of the standard Java interfaces for java.util.List or java.util.Map).
Kullanım
Örnek
Şöyle yaparız. Spring ile JSON kullanmak için özel bir ayar yapmadıysak, Key ve Value nesnelerinin Serializable olması gerekir.
public class Doctor implements Serializable {...}@Beanpublic Map<String, Doctor> doctorMap(HazelcastInstance hazelcastInstance) {return hazelcastInstance.getMap("doctorMap");}@Autowiredprivate Map<String, Doctor> doctorMap;@GetMapping(path = { "/get/{doctorNumber}" })public Doctor getDoctor(@PathVariable("doctorNumber") String doctorNumber) {//First call is to check if doctormap has doctor details if yes,
//return the value otherwise call database.Doctor doctor = doctorMap.get(doctorNumber);if (doctor == null){doctor = ...;}return doctor;}@PostMapping("/add")public void createDoctor(@RequestBody Doctor doctor) {//save doctor details in cachedoctorMap.put(doctor.getDoctorNumber(), doctor);...}@DeleteMapping(path = { "/delete/{doctorNumber}" })public Doctor deleteDoctor(@PathVariable("doctorNumber") String doctorNumber) {//remove doctor details from both cache and databasedoctorMap.remove(doctorNumber);...}
constructor
HazelcastInstance.getMap() tarafından döndürülür
addEntryListener
Örnek
Şöyle yaparız
import com.hazelcast.core.EntryEvent;import com.hazelcast.core.EntryListener;import com.hazelcast.map.MapEvent;public class MapEntryListener implements EntryListener {@Overridepublic void entryAdded(EntryEvent entryEvent) {logger.info("key {} and value {}",entryEvent.getKey(),entryEvent.getValue());}@Overridepublic void entryEvicted(EntryEvent entryEvent) {logger.info("Map Entry was evicted : {}",entryEvent);}@Overridepublic void entryRemoved(EntryEvent entryEvent) {logger.info("Object with key {} removed from map.",entryEvent.getKey());}@Overridepublic void entryUpdated(EntryEvent entryEvent) {logger.info("key {} updated from {} to {}.", entryEvent.getKey(),
entryEvent.getOldValue(),entryEvent.getValue());}@Overridepublic void mapCleared(MapEvent mapEvent) {logger.info("Map was cleared : {}",mapEvent);}@Overridepublic void mapEvicted(MapEvent mapEvent) {logger.info("Map was evicted: {}",mapEvent);}@Overridepublic void entryExpired(EntryEvent entryEvent) {}}IMap<String,String> hazelcastMap = ...;hazelcastMap.addEntryListener(mapEntryListener,true);
get metodu
Örnek
Şöyle yaparız
public String getDataByKey(String key) { IMap<String, String> map = hazelcastInstance.getMap("my-map"); return map.get(key);}
lock metodu
Örnek
Şöyle yaparız
put metoduimport com.hazelcast.core.Hazelcast;import com.hazelcast.core.HazelcastInstance;import com.hazelcast.core.IMap;HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();IMap txLockMap = hazelcast.getMap("txLockMap");String lock = "...";txLockMap.lock(key);try {txLockMap.tryLock(key,10,TimeUnit.SECONDS);} catch (Exception e){...}txLockMap.isLocked(key);try {txLockMap.unlock(key);} catch (Exception e){...}
Örnek
Şöyle yaparız
public String createData(String key, String value) { IMap<String, String> map = hazelcastInstance.getMap("my-map"); map.put(key, value); return "Data is stored.";}
remove metodu
Örnek
Örnek
Şöyle yaparız
public String deleteData(String key) { IMap<String, String> map = hazelcastInstance.getMap("my-map"); return map.remove(key);}
Örnek
Şöyle yaparız
public String update(String key, String value) { IMap<String, String> map = hazelcastInstance.getMap("my-map"); map.set(key, value); return "Data is stored.";}
Hiç yorum yok:
Yorum Gönder