Saturday, 17 November 2012

Java Random thread save

Examples:
ThreadLocalRandom.current().nextInt()
ThreadLocalRandom.current().nextDouble()
...

TimeUnit class

If you want your java program to wait for defined time amount use class TimeUnit. It is more clean than using Thread.sleep.
Examples:
TimeUnit.NANOSECONDS.sleep(10)
TimeUnit.SECONDS.sleep(10)

Sunday, 4 November 2012

Guava light cache

If you have a need to declare a small cache which have to be cleared periodically you can easily do it using Guava Cache.
We have to define loader that will load cache element and a period after which it will throw an item away. And then next read will use a loader to load an element into cache again. We can also define a cache in the way it will throw away an item if it was not read for a period of time.
Here is an example:
private LoadingCache<String, MyClass> cache;
CacheLoader<String, MyClass> loader = new CacheLoader<String, MyClass> () {
      public MyClass load(String key) throws Exception {
        return ourService.get(key);
      }
    };
cache = CacheBuilder.newBuilder().
        expireAfterWrite(1, TimeUnit.MINUTES).
        build(loader);

More about this subject here: http://code.google.com/p/guava-libraries/wiki/CachesExplained

Defining Mockito mock bean from Spring xml definition

You can define a spring bean in spring xml context that will be a Mockito mock of a given type:

  

But if you want to inject that bean by type, you have to define it as a Proxy of given interface:

    
      
        
      
    
    
      com.example.Repository
    
  

Sunday, 28 October 2012

Monday, 8 October 2012

Velocity Europe

I have attended Velocity Europe Conference. Here is what I learned:

Tools for web performance
News:
  • HTTP 2.0 - Will be decided what it will include in about a 1,5 year. It will be based mostly on SPDY
Useful tips:
  • Do not implement Javascripts that block. You can use option async + defer (for IE). Then it will not block your page load event, what makes your page beter for google robots and higher in google searches (Google has 3,5 seconds metric for Page Load). Every JS that does not block should be loaded in the bottom of the page and ones that block should be included on the top. Use Modernizr for javascript dependency
  • If you add a resource in the page starting with https://address or http://address, just use //address
  • Add performance metrics to DoD
  • Allow business to measure RUM (Real user monitoring)
  • Learn every day is the reason for working, otherwise change your work! (From facebook guy)
  • Take risk. Otherwise you want learn. (Same guy)
  • Site performance means money for business
  • It is better to develop a native application on the mobile if you want a fully featured one. It is almost impossible to have one fast rendering on Android and IPhone.
  • Do not keep server 200 days without restart. It is not updated or it can broke down. (That's why windows 95 had to be restarted. Some unix also had Magic number bug, so they worked only about 200 days)
See also:

An here is my photo: http://www.flickr.com/photos/oreillyconf/8071104092/in/set-72157631732025793