4. Mise en bouche
!
La Map classique
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("hello", "world");
String value = map.get("hello");
}
5. Mise en bouche
!
La ConcurrentMap
public static void main(String[] args) {
ConcurrentMap<String, String> map = new ConcurrentHashMap<>();
map.put("hello", "world");
String value = map.get("hello");
String previous = map.putIfAbsent("goodbye", "marylou");
}
6. Mise en bouche
!
La ConcurrentMap distribuée
public static void main(String[] args) {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
ConcurrentMap<String, String> map = hz.getMap("mymap");
map.put("hello", "world");
String value = map.get("hello");
String previous = map.putIfAbsent("goodbye", "marylou");
}
17. P2P event bus
!
Les topics : broadcast à tous les listeners
ITopic<String> topic = hz.getTopic("alerts");
topic.addMessageListener(new MessageListener<String>() {
public void onMessage(Message<String> message) {
}
println("Received " + message.getMessageObject() +
" from " + message.getPublishingMember());
});
Nœud d’origine
du message
18. P2P event bus
!
Les queues
BlockingQueue<String> queue = hz.getQueue("jobs");
queue.offer("Make me a sandwich");
BlockingQueue<String> queue = hz.getQueue("jobs");
while(true) {
String job = queue.take();
println("Now working on " + job);
}
(Aussi en asynchrone avec poll() et les listeners)
20. NoSQL datastore
!
La Map « interrogeable »
Indexation des
propriétés
IMap<Long, Employee> map = hz.getMap("employee");
map.addIndex("active", true);
map.addIndex("age", true);
Collection<Employee> employees =
map.values(new SqlPredicate("active AND age < 30"));
Sous ensemble de SQL sur
les propriétés JavaBean
21. Cluster coordination
!
Compteurs distribués
IAtomicLong reqCount = hz.getAtomicLong("users");
// Start request
long count = reqCount.incrementAndGet();
try {
// Do some stuff
println("There are " + count + " request in progress");
} finally {
// End request
reqCount.decrementAndGet();
}
22. Cluster coordination
!
Locks distribués (à utiliser avec parcimonie)
Lock lock = hz.getLock("maintenance_mode");
lock.lock();
try {
// There can be only one in the cluster
} finally {
lock.unlock();
}
ILock lock = hz.getLock("maintenance_mode");
lock.lock(10, TimeUnit.SECONDS);
try {
// There can be only one in the cluster
} finally {
lock.unlock();
}
Protection :
durée limitée