Académique Documents
Professionnel Documents
Culture Documents
Map
import java.util.Map;
import java.util.HashMap;!
!
Concurrent Map
import java.util.Map;
import java.util.concurrent.*;!
!
Distributed Map
import java.util.Map;
import com.hazelcast.core.*;!
!
HazelcastInstance hazelcast =
Hazelcast.newHazelcastInstance(new Config())
Distributed Map
import java.util.Map;
import com.hazelcast.core.*;!
!
Why Hazelcast?
Session/state sharing
Distributed Caching
Messaging
Distributed Processing
Introducing Hazelcast
Apache License
3 MB jar and no-dependency
Map, queue, set, list, lock, semaphore, topic and
executor service
Code Samples
Hazelcast
Cluster Interface
import com.hazelcast.core.*; !
import java.util.Set;!
Distributed Map
import com.hazelcast.core.*; !
import java.util.ConcurrentMap;!
//ConcurrentMap methods!
map.putIfAbsent ("2", new Customer(Chuck Norris));!
map.replace ("3", new Customer(Bruce Lee));
Distributed MultiMap
import com.hazelcast.core.*;!
MultiMap<String, Order>
map.put ("1, new Order
map.put ("1, new Order
map.put ("1, new Order
map.put (2, new Order
map = hazelcast.getMultiMap(orders);!
("iPhone", 340)); !
(MacBook", 1200)); !
(iPod", 79)); !
(iMac", 1500));!
Distributed Queue
import com.hazelcast.core.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
queue.offer(task);
Task t = queue.poll();
Distributed Lock
import com.hazelcast.core.*;
import java.util.concurrent.locks.Lock;
//Lock on Map!
IMap<String, Customer> map = hazelcast.getMap("customers");!
map.lock("1");
try {
// do something
} finally {
map.unlock("1");
}
Distributed Topic
import com.hazelcast.core.*;
Distributed Events
import com.hazelcast.core.*;
Transactions
import com.hazelcast.core.*;
import java.util.*;
try {
Object obj = queue.poll();
//process obj
map.put ("1", "value1");
set.add ("value");
//do other things..
context.commitTransaction();
}catch (Throwable t) {
context.rollbackTransaction();
}
Data Affinity
Data Affinity
Data Affinity
Data Affinity
Data Affinity
Executor Service
Hello Task
A simple task
ExecutorService ex = hazelcast.getExecutorService();!
Future<String> future = executor.submit(new HelloTask());!
// ...!
String result = future.get();!
On a specific node
On any available node
On a collection of defined nodes
On a node owning a key
!
!
!
public int removeOrder(long customerId, long orderId){!
IMap<Long, Customer> map = Hazelcast.getMap("customers");!
map.lock(customerId);!
Customer customer = map.get(customerId);!
customer.removeOrder (orderId);!
map.put(customerId, customer);!
map.unlock(customerId);!
return customer.getOrderCount();!
}!
!
!
}!
!
public static int removeOrder(long customerId, long orderId){!
ExecutorService es = Hazelcast.getExecutorService();!
OrderDeletionTask task = new OrderDeletionTask(customerId, orderId);!
Future future = es.submit(task);!
int remainingOrders = future.get();!
return remainingOrders;!
}!
Query
// setters
}
Persistence
Persistence
Write-Behind
:
asynchronously
storing
entries
Write-Through
:
synchronous
Read-Through
:
if
get(key)
is
null,
load
it
Configuration
<hazelcast>
<group>
<name>dev</name>
<password>dev-pass</password>
</group>
<properties>
<property name="hazelcast.initial.min.cluster.size">8</property>
<property name="hazelcast.icmp.enabled">true</property>
</properties>
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="true">
<multicast-group>224.2.2.3</multicast-group>
<multicast-port>54327</multicast-port>
</multicast>
<tcp-ip enabled="false">
<interface>192.168.1.2-5</interface>
</tcp-ip>
</join>
<interfaces enabled="true">
<interface>192.168.1.*</interface>
</interfaces>
</network>
<map name="default">
<backup-count>1</backup-count>
<time-to-live-seconds>60</time-to-live-seconds>
<max-idle-seconds>60</max-idle-seconds>
<max-size>10000</max-size>
<eviction-policy>LRU</eviction-policy>
<eviction-percentage>25</eviction-percentage>
<map-store>
....
</map-store>
</map>
</hazelcast>
Wildcard Configuration
Recap
Distributed implementation of
Map
Queue
Set
List
MultiMap
Lock
Executor Service
Topic
Semaphore
AtomicLong
CountDownLatch
!
!
Thank You!
@oztalip
@hazelcast
talip@hazelcast.com