Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast •...

43
© 2014 Hazelcast Inc. Distributed Caching with JCache and Beyond TALIP OZTURK FOUNDER & CTO

Transcript of Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast •...

Page 1: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2014 Hazelcast Inc.

Distributed Caching with JCache and Beyond

TALIP OZTURKFOUNDER & CTO

Page 2: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Map

import java.util.Map; import java.util.HashMap;!!Map map = new HashMap();!map.put(“1”, “value”); map.get(“1”);

Page 3: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Concurrent Map

import java.util.Map; import java.util.concurrent.*;!!Map map = new ConcurrentHashMap();!map.put(“1”, “value”); map.get(“1”);

Page 4: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Map

import java.util.Map; import com.hazelcast.core.*;!!HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(new Config())

Page 5: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Map

import java.util.Map; import com.hazelcast.core.*;!!Map map = hazelcast.getMap(“mymap”);!map.put(“1”, “value”); map.get(“1”);

Page 6: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Why Hazelcast?

• Session/state sharing!

• Distributed Caching!

• Messaging!

• Distributed Processing

Page 7: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

Introducing Hazelcast

• Open source (Apache v2) • 7 MB single jar • no-dependency • maven-friendly

Page 8: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Data Partitioning in a Cluster

• Fixed number of partitions (default 271)!

• Each key falls into a partition!

• partitionId = hash(keyData)%PARTITION_COUNT!

• Partition ownerships are reassigned upon membership change!

Page 9: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

Embedded

Page 10: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

Client-Server

Page 11: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

Client-Server

Page 12: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Code Samples

Page 13: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Hazelcast

• Hazelcast is thread safe

• Many instances on the same JVMConfig config = new Config();!HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config)!HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config)!

• All objects must be serializableclass Employee implements java.io.Serializable! //better!class Employee implements com.hazelcast.nio.DataSerializable!

Map<String, Employee> = hazelcast.getMap(“employees”);!List<Users> = hazelcast.getList(“users”);!

Page 14: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Cluster Interface

import com.hazelcast.core.*; !import java.util.Set;!!Cluster cluster = hazelcast.getCluster(); cluster.addMembershipListener(listener);!!Member localMember = cluster.getLocalMember(); !System.out.println (localMember.getInetAddress());!!Set<Member> setMembers = cluster.getMembers();

Page 15: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Map

import com.hazelcast.core.*; !import java.util.ConcurrentMap;!!Map<String, Customer> map = hazelcast.getMap("customers"); !map.put ("1", customer); !Customer c = map.get("1");!!//ConcurrentMap methods!map.putIfAbsent ("2", new Customer(“Chuck Norris”));!map.replace ("3", new Customer(“Bruce Lee”));

Page 16: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed MultiMap

import com.hazelcast.core.*;!!MultiMap<String, Order> map = hazelcast.getMultiMap(“orders”);!map.put ("1”, new Order ("iPhone", 340)); !map.put ("1”, new Order (”MacBook", 1200)); !map.put ("1”, new Order (”iPod", 79)); !map.put (“2”, new Order (”iMac", 1500));!!Collection<Order> orders = map.get ("1”); !for (Order order : orders) {!// process order !boolean removed = map.remove("1”, new Order(”iPod", 79));

Page 17: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Queue

import com.hazelcast.core.*; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; !BlockingQueue<Task> queue = hazelcast.getQueue(“tasks"); !queue.offer(task); Task t = queue.poll(); !//Timed blocking Operations!queue.offer(task, 500, TimeUnit.MILLISECONDS) Task t = queue.poll(5, TimeUnit.SECONDS); !//Indefinitely blocking Operations!queue.put(task) Task t = queue.take();

Page 18: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Lock

import com.hazelcast.core.*; import java.util.concurrent.locks.Lock; !Lock mylock = hazelcast.getLock(mylockobject); mylock.lock(); try { // do something } finally { mylock.unlock(); } !//Lock on Map!IMap<String, Customer> map = hazelcast.getMap("customers");!map.lock("1"); try { // do something } finally { map.unlock("1"); }

Page 19: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Topic

import com.hazelcast.core.*; !public class Sample implements MessageListener { public static void main(String[] args) { Sample sample = new Sample(); ITopic<String> topic = hazelcast.getTopic ("default"); topic.addMessageListener(sample); topic.publish ("my-message-object"); } public void onMessage(Message<String> msg) { System.out.println("Got msg :" + msg.getMessageObject()); } }

Page 20: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Distributed Events

import com.hazelcast.core.*; !public class Sample implements EntryListener { public static void main(String[] args) { Sample sample = new Sample(); IMap map = hazelcast.getMap ("default"); map.addEntryListener (sample, true); map.addEntryListener (sample, "key"); } public void entryAdded(EntryEvent event) { System.out.println("Added " + event.getKey() + ":" + event.getValue()); } public void entryRemoved(EntryEvent event) { System.out.println("Removed " + event.getKey() + ":" + event.getValue()); } public void entryUpdated(EntryEvent event) { System.out.println("Updated " + event.getKey() + ":" + event.getValue()); } }

Page 21: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Transactions

import com.hazelcast.core.*; import java.util.*; !TransactionOptions options = new TransactionOptions().setTransactionType(TransactionType.TWO_PHASE); TransactionContext context = hz.newTransactionContext(options) context.beginTransaction(); !TransactionalQueue queue = context.getQueue("myqueue"); TransactionalMap map = context.getMap ("mymap"); TransactionalSet set = context.getSet ("myset"); !try { Object obj = queue.poll(); //process obj map.put ("1", "value1"); set.add ("value"); //do other things.. context.commitTransaction(); }catch (Throwable t) { context.rollbackTransaction(); }

Page 22: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Executor Service

Page 23: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Hello Task

• A simple task

• Execute on any memberExecutorService ex = hazelcast.getExecutorService();!Future<String> future = executor.submit(new HelloTask());!// ...!String result = future.get();!

public class HelloTask implements Callable<String>, Serializable{! @Override! public String call(){! return “Hello”;! }!}!

Page 24: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Hazelcast can execute a task ...

ExecutorService executor = hazelcast.getExecutorService(); FutureTask<String> task1, task2; !!// CASE 1: Run task on a specific member.!Member member = ... task1 = new DistributedTask<String>(new HelloTask(), member); executor.execute(task1); !// CASE 2: Run task on a member owning the key.!Member member = ... task1 = new DistributedTask<String>(new HelloTask(), “key”); executor.execute(task1); !// CASE 3: Run task on group of members. Set<Member> members = ... task = new MultiTask<String>(new HelloTask(), members); executor.execute(task2);

• On a specific node!• On any available node!• On a collection of defined nodes!• On a node owning a key

Page 25: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Query

Page 26: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Code%Samples%–%Querypublic class Customer { private boolean active; private String name; private int age; // getters ! // setters }

Page 27: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Code%Samples%–%Queryimport com.hazelcast.core.*; import com.hazelcast.query.SqlPredicate; import java.util.Collection; IMap map = hazelcast.getMap(“customers”); !!map.addIndex(“active” ,false); map.addIndex(“name” ,false); map.addIndex(“age” ,true); !!Collection<Customer> customers = map.values(new SqlPredicate(“active AND age <= 30”));

Page 28: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

Hazelcast Architecture

Page 29: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache

• Standard Java Caching API • javax.caching.Cache

• Similar to java.util.Map but supports • Events • Computation • Integration

• Implementation can be local or distributed

Page 30: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache implementations

• Hazelcast • Oracle Coherence • Pivotal Gemfire • Ehcache • Infinispan • Gridgain/Ignite • Couchbase

Page 31: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache Integration

• Java EE 8 • Spring • Mulesoft • WSO2 • Payara

Page 32: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache API

• javax.cache.Cache

• javax.cache.CacheManager

• javax.cache.spi.CachingProvider

Page 33: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache API

Page 34: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache API

• Annotations

• Expiry Policy

• Statistics

Page 35: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache API

Page 36: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

Distributed Cache

Page 37: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

JCache: TODO

• Async API • Eviction • Query • Transactions

Page 38: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Executor Service Scenario

!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();!}!

Page 39: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Add Bonus Task

!public class OrderDeletionTask implements Callable<Integer>, PartitionAware, Serializable {!! private long customerId;! private long orderId;!! public OrderDeletionTask(long customerId, long orderId) {! super();! this.customerId = customerId;! this.orderId = orderId;! }!! public Integer call () {! 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 Object getPartitionKey() {! return customerId;! }!}!

Page 40: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

Send computation over data

!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;!}!

Page 41: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

EntryProcessor

Page 42: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2015 Hazelcast Inc. Confidential and Proprietary

EntryProcessor

Page 43: Distributed Caching with JCache and Beyondfiles.meetup.com/4939632/Hazelcast_JCache.pdfHazelcast • Hazelcast is thread safe • Many instances on the same JVM Config config = new

© 2014 Hazelcast Inc.

Thank [email protected]

twitter: @oztalip