24 Şubat 2021 Çarşamba

Hazelcast IMap Arayüzü

Giriş
Ş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 {
...
}

@Bean
public Map<String, Doctor> doctorMap(HazelcastInstance hazelcastInstance) {
  return hazelcastInstance.getMap("doctorMap");
}

@Autowired
private 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 cache
  doctorMap.put(doctor.getDoctorNumber(), doctor);
  ...
}
@DeleteMapping(path = { "/delete/{doctorNumber}" })
public Doctor deleteDoctor(@PathVariable("doctorNumber") String doctorNumber) {
  //remove doctor details from both cache and database
  doctorMap.remove(doctorNumber);
  ...
}
constructor
HazelcastInstance.getMap() tarafından döndürülür
Örnek
Şöyle yaparız
IMap<String, String> map = hazelcastInstance.getMap("my-map");
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 {

  @Override
  public void entryAdded(EntryEvent entryEvent) {
    logger.info("key {} and value {}",entryEvent.getKey(),entryEvent.getValue());
  }
  @Override
  public void entryEvicted(EntryEvent entryEvent) {
    logger.info("Map Entry was evicted : {}",entryEvent);
  }
  @Override
  public void entryRemoved(EntryEvent entryEvent) {
    logger.info("Object with key {} removed from map.",entryEvent.getKey());
  }
  @Override
  public void entryUpdated(EntryEvent entryEvent) {
    logger.info("key {} updated from {} to {}.", entryEvent.getKey(),
entryEvent.getOldValue(),entryEvent.getValue());
  }
  @Override
  public void mapCleared(MapEvent mapEvent) {
    logger.info("Map was cleared : {}",mapEvent);
  }
  @Override
  public void mapEvicted(MapEvent mapEvent) {
    logger.info("Map was evicted: {}",mapEvent);
  }
  @Override
  public 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
import 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){
  ...
}
put metodu
Ö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);
}
set metodu
Ö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