Friday, February 20, 2015

New Java Features in Version 9 and 8

1. Project Kulla – REPL in Java  ( may be in version 9)

REPL (Read-Eval-Print-Loop). Meaning, if you want to run a few lines of Java to check out them quickly on their own you will have to wrap it all in a separate project or method .
There are REPL add-ons to popular IDEs and some other solutions like Java REPL, but no official way to do this so far – Project Kulla might be the answer.

2. HTTP 2 Client  ( may be in version 9)

(To understand this you need to understand normal things which happen when we give a request from a browser -- actually a HTTP request is sent that mean request uses HTTP 1.0 protocol ,
In future default protocol will be HTTP 2.0   (to make things fast a better protocol) )

3.  Smart Java Compilation, Phase Two

Improve the sjavac tool so that it can be used by default in the JDK build (as of now not default), and generalize it so that it can be used to build large projects other than the JDK.(for better speed)

4. Segmented Code Cache

(Prerequisite understanding : What is JIT :
A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently.
)

Another performance improvement for Java 9 is coming from the JIT compiler angle. When certain areas of code are executed rapidly, the VM compiles them to native code and stores them in the code cache. This update looks into segmenting the code cache to different areas of compiled code in order to improve the compiler’s performance.

Instead of a single area, the code cache will be segmented into 3 by the code’s lifetime in the cache:
– Code that will stay in the cache forever (JVM internal / non-method code)
– Short lifetime (Profiled code, specific to a certain set of conditions)
– Potentially long lifetime (Non-profiled code)
The segmentation would allow for several performance improvements to happen. For example, the method sweeper would be able to skip non-method code and act faster.


5. Improve Contended Locking

(Prerequisite understanding about Load Contention: http://stackoverflow.com/questions/1970345/what-is-thread-contention  --Read the big explanation )


Lock contention is a performance bottleneck for many multithreaded Java applications. The enhancement proposal looks into improving the performance of Java object monitors as measured by different benchmarks. One of the these tests is Volano. It simulates a chat server with huge thread counts and client connections, many of them trying to access the same resources and simulate a heavy duty real world application.

These kind of stress tests push JVMs to the limit and try to determine the maximum throughput they can achieve, usually in terms of messages per second. The ambitious success metric for this JEP is a significant improvement over 22 different benchmarks. If the effort will succeed, these performance improvements will be rolling out in Java 9.

6.  Money and Currency API


After the new Date and Time API introduced in Java 8, Java 9 brings with it a new and official API for representing, transporting, and performing comprehensive calculations with Money and Currency. To find out more about the project, you can visit JavaMoney on Github. Code and usage examples are already available right here . Here are a few highlights:

   
Money amt1 = Money.of(10.1234556123456789, "USD"); // Money is a BigDecimal
FastMoney amt2 = FastMoney.of(123456789, "USD"); // FastMoney is up to 5 decimal places
Money total = amt1.add(amt2);

The new money types: Money & FastMoney :
   
MonetaryAmountFormat germanFormat = MonetaryFormats.getAmountFormat(
Locale.GERMANY);

System.out.println(germanFormat.format(monetaryAmount)); // 1.202,12 USD

Formatting money according to different countries

7.  Light-Weight JSON API :

 for handling JSON in Java, what’s unique about this API is that it would be part of the language, lightweight and would use the new capabilities of Java 8. And will be delivered right through java.util .

8. Process API Updates

So far there has been a limited ability for controlling and managing operating system processes with Java

Saturday, January 31, 2015

Query Caching and Second level Caching using Ehcache.


Query Caching ( Using Ehcache )

You can also enable query caching. To do so configure it in your hbm.xml:

property key="hibernate.cache.use_query_cache">true</property
and where queries are defined in your code, add the method call setCacheable(true) to the queries that should be cached:

sessionFactory.getCurrentSession().createQuery("...").setCacheable(true).list();
By default, Ehcache will create separate cache regions for each entity that you configure for caching. You can change the defaults for these regions by adding the configuration to your ehcache.xml. To provide this configuration file, use this property in hibernate configuration:1

<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
And use below configuration to override the default configuration:

<cache
    name="com.somecompany.someproject.domain.Country"
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600"
    overflowToDisk="true"
/>

Please note that in ehcache.xml, if eternal=”true” then we should not write timeToIdealSeconds, timeToLiveSeconds, hibernate will take care about those values
So if you want to give values manually better use eternal=”false” always, so that we can assign values into timeToIdealSeconds, timeToLiveSeconds manually.
timeToIdealSeconds=”seconds” means, if the object in the global cache is ideal, means not using by any other class or object then it will be waited for some time we specified and deleted from the global cache if time is exceeds more than timeToIdealSeconds value.
timeToLiveSeconds=”seconds” means, the other Session or class using this object or not, i mean may be it is using by other sessions or may not, what ever the situation might be, once it competed the time specified timeToLiveSeconds, then it will be removed from the global cache by hibernate.

Second level caching ( Using Ehcache )

A very beautiful example for Understanding Ehcache and Implementing it is here explained

Second level caching using Ehcache. - By Pankaj