• Like
  • Save
Guava Overview Part 2 Bucharest JUG #2
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Guava Overview Part 2 Bucharest JUG #2


A few slides on Cache and Service abstractions from Guava.

A few slides on Cache and Service abstractions from Guava.

Published in Technology , News & Politics
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Guava Overview. Part 2 Andrei Savu / Bucharest JUG #2 asavu @ axemblr.com
  • 2. About meFounding Partner @ Axemblr.comApache Whirr PMC Memberjclouds committerConnect with me on LinkedIn
  • 3. @ AxemblrBuilding a tool and a platform for managingApache Hadoop clusters on cloud infrastructureAxemblr Tool for Cloudera CDH3 http://axemblr.com/toolAxemblr Service for Apache Hadoop http://axemblr.com/service
  • 4. Plan● What is Guava?● Caches● Concurrency Services
  • 5. Quick Intro
  • 6. What is Guava?Open-source version of Googles core Javalibraries[...] carefully designed, tested, optimized andused in production at GoogleYou dont need to write them, test them, oroptimize them: you can just use them.
  • 7. What is Guava? (cont)Battle-tested in production at GoogleStaggering numbers of unit tests. >110.000 asof January 2012 (generated)Lives near the bottom of the stack, right on topof the JDK itself
  • 8. Caches
  • 9. Caches - When to use?When a value is either● expensive to compute● expensive to retrieveMore or less similar to ConcurrentMap +automatic eviction
  • 10. Caches - When to use? (cont)● spend memory to improve speed● keys are queried multiple times● your application would work if every value was evicted immediately● you have less data than RAM
  • 11. Caches - Example #1
  • 12. Caches - Example #2
  • 13. Caches - Example #3cache.put(key, value)A Cache can be accessed as a Map:Cache.asMap() returns a ConcurrentMap
  • 14. Caches - Eviction #1By size CacheBuilder.maximumSize(long)By custom weight CacheBuilder.weigher(Weigher) CacheBuilder.maximumWeight(long)
  • 15. Caches - Eviction #2By time expireAfterAccess(long, TimeUnit) expireAfterWrite(long, TimeUnit)
  • 16. Caches - Eviction #3Reference-based eviction CacheBuilder.weakKeys() CacheBuilder.weakValues() CacheBuilder.softValues()Note: all of them are using == for equality and*not* equals()
  • 17. Caches - Eviction #4Explicit Cache.invalidate(key) Cache.invalidateAll(keys) Cache.invalidateAll()
  • 18. Caches - Removal ListenersCacheBuilder .removalListener(RemovalListener)Be aware of long running listeners. They areblocking the queue.RemovalListeners .asynchronous(RemovalListener, Executor)
  • 19. Caches - CleanupAutomatic eviction happens on writesExplicit Cache.cleanup()(you can put this in a thread)
  • 20. Caches - RefreshLoadingCache.refresh(K)CacheLoader.reload(K, V)(if the new value can be computed from the oldone)
  • 21. Cache - StatisticsCacheBuilder.recordStats()CacheStats Cache.stats() hitRate() averageLoadPenalty() evictionCount()
  • 22. Service
  • 23. What is this?Object with operational stateStart and stop methodsE.g. web servers, RPC servers, timers etc.Non-trivial if multiple threads are involved.
  • 24. Lifecycle● Service.State.NEW to● Service.State.STARTING to● Service.State.RUNNING to● Service.State.STOPPING to● Service.State.TERMINATEDOn failure:● Service.State.FAILEDA stopped service may not be restarted.
  • 25. Lifecycle #2asynchronously: start - returns ListenableFuture stopsynchronously startAndWait stopAndWait
  • 26. Implementations
  • 27. AbstractIdleServiceHas nothing to run:
  • 28. AbstractExecutionThreadService
  • 29. AbstractScheduledServiceSimilar to AbstractExecutionThreadServiceOverride runOneIterationImplement scheduler() newFixedRateSchedule(initialDelay, delay,TimeUnit) newFixedDelaySchedule(initialDelay, delay,TimeUnit)
  • 30. Next?Functional IdiomsRangesI/OHashingMathReflectionConcurrency (ListenableFuture)EventBusSee Guava Wiki
  • 31. Thanks! Questions?Andrei Savu / asavu @ axemblr.com