<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>722 &#187; Java</title>
	<atom:link href="http://722.kalaari.net/b/lang/en/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://722.kalaari.net/b</link>
	<description>f 722 t 722 p 722 oxygen 722 722 722</description>
	<lastBuildDate>Sat, 24 Dec 2011 11:56:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jsr107 javax.cache annotations with Google Guice</title>
		<link>http://722.kalaari.net/b/lang/en/2011/12/23/jsr107-javax-cache-annotations-with-google-guice</link>
		<comments>http://722.kalaari.net/b/lang/en/2011/12/23/jsr107-javax-cache-annotations-with-google-guice#comments</comments>
		<pubDate>Fri, 23 Dec 2011 21:00:41 +0000</pubDate>
		<dc:creator>Christophe Furmaniak</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[jsr107]]></category>
		<category><![CDATA[lombok]]></category>

		<guid isPermaLink="false">http://722.kalaari.net/b/?p=108</guid>
		<description><![CDATA[<a href="http://722.kalaari.net/b/lang/en/2011/12/23/jsr107-javax-cache-annotations-with-google-guice" title="jsr107 javax.cache annotations with Google Guice"></a>I&#8217;m using Google Guice as my Dependency Injection framework. I wanted to use the javax.cache api aka JSR107 (with EHcache as implementation). If you want to learn more about this famous JSR107, it could be a good idea to go &#8230;<p class="read-more"><a href="http://722.kalaari.net/b/lang/en/2011/12/23/jsr107-javax-cache-annotations-with-google-guice">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://722.kalaari.net/b/lang/en/2011/12/23/jsr107-javax-cache-annotations-with-google-guice" title="jsr107 javax.cache annotations with Google Guice"></a><p>I&#8217;m using <a title="Google Guice" href="http://code.google.com/p/google-guice/wiki/Motivation?tm=6" target="_blank">Google Guice</a> as my Dependency Injection framework. I wanted to use the javax.cache api aka <a title="JSR107" href="http://jcp.org/en/jsr/detail?id=107" target="_blank">JSR107</a> (with <a href="http://ehcache.org/" target="_blank">EHcache</a> as implementation). If you want to learn more about this famous JSR107, it could be a good idea to go to <a href="http://gregluck.com/blog/archives/category/jsr107/">Greg Luck blog</a> to get some more infos about it.</p>
<p>In my case, I wanted to use the annotations part of the JSR107 because I find them to be more elegant than a pure code solution (understand, you can put cache in your app without changing the logic, you just annotate your business methods, and <em>woosh</em>, you have cache set up).</p>
<p>But the JSR107 annotations implementations are DI framework dependant. And untill some days ago, only Spring and CDI implementation were provided.</p>
<p>I <a href="https://twitter.com/#!/cfurmaniak/status/147052796154359808" target="_blank">twitted</a> to search any volunteers that might be interested in contributing (with or without me) to a Guice implementation.</p>
<p>Some hours laters, some commits from Michael Stachel were <a href="https://github.com/jsr107/RI/tree/master/cache-annotations-ri/cache-annotations-ri-guice" target="_blank">pushed by Alex Snaps to the github repo</a> and here they are, the JSR 107 annotations for Guice were available.</p>
<p>I&#8217;d like to show you how you can use them.</p>
<p>The full source code is available <a href="https://bitbucket.org/looztra/guicyjsr107app/src">here</a> as a <a href="http://mercurial.selenic.com/">Mercurial</a> repository on <a href="http://bitbucket.org/">Bitbucket</a>.</p>
<p>I&#8217;m using <a href="http://maven.apache.org/">Maven</a>, so here is the dependency part of the pom</p>
<pre class="brush:xml">
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <!-- cache jsr107 stuff -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-jcache</artifactId>
            <version>1.4.0-beta1</version>
        </dependency>
        <dependency>
            <groupId>javax.cache.implementation</groupId>
            <artifactId>cache-annotations-ri-guice</artifactId>
            <version>0.5-SNAPSHOT</version>
        </dependency>
</pre>
<p>You can see here that I&#8217;m using the latest snapshot of the <strong>cache-annotations-ri-guice</strong> artifact (<em>0.5-SNAPSHOT</em> as I&#8217;m writing this post). I don&#8217;t know if the snapshots are available somewhere, so I just cloned the corresponding <a href="https://github.com/jsr107">git repositories</a> and installed the snapshots locally. To be more precise, you will need to clone the <a href="https://github.com/jsr107/jsr107spec">jsr107-spec</a> repo to build and install locally the jsr107 api artifacts that are needed by the <a href="https://github.com/jsr107/RI">jsr107 Reference Implementation</a> artifacts, among which you&#8217;ll find the <strong>cache-annotations-ri-guice</strong> artifact.</p>
<p>Now, let&#8217;s write a basic <em>UserService</em> interface (ok, you don&#8217;t need an interface actually):</p>
<pre class="brush:java">package net.awl.ismp.guice.jsr107;

import java.util.List;

/**
 *
 * @author looztra
 */
public interface UserService {
    public User getUserById(int id, int callId);
    public List&#60;User&#62; getUserByLastName(String name, int callId);
    public boolean calledBy(int callId);
}</pre>
<p>The <em>UserService</em> makes reference to a <em>User</em> class:</p>
<pre class="brush:java">package net.awl.ismp.guice.jsr107;

import java.io.Serializable;
import lombok.Data;

/**
 *
 * @author looztra
 */
@Data
public class User implements Serializable {
    private int id;
    private String firstName;
    private String lastName;
    private int age;
}</pre>
<p>People that follow may have spotted that I was too lazy to write the accessors and that I chose to use the <a href="http://projectlombok.org/">Lombok</a> <strong>@Data</strong> annotation instead.</p>
<p>Here is the main part of a simple implementation of the basic UserService:</p>
<pre class="brush:java">package net.awl.ismp.guice.jsr107;

import java.util.*;
import javax.cache.annotation.CacheKeyParam;
import javax.cache.annotation.CacheResult;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author looztra
 */
@Slf4j
public class SimpleUserService implements UserService {

    private Map&#60;Integer, User&#62; users = new HashMap&#60;Integer, User&#62;();
    private List&#60;Integer&#62; callers = new ArrayList&#60;Integer&#62;();

    public SimpleUserService() {
        initContent();
    }

    @Override
    @CacheResult(cacheName = "getUserById")
    public User getUserById(@CacheKeyParam int id, int callId) {
        log.info("getUserById(): business method called for id (" + id + ") and callId (" + callId + ")");
        registerCaller(callId);
        if (id &#62;= 0 &#38;&#38; id &#60; 100) {
            return users.get(id);
        } else {
            return null;
        }
    }

    @Override
    @CacheResult(cacheName = "getUserByLastName")
    public List&#60;User&#62; getUserByLastName(@CacheKeyParam String name, int callId) {
        log.info("getUserByLastName(): business method called for name &#60;" + name + "&#62; and callId &#60;" + callId + "&#62;");
        registerCaller(callId);
        List&#60;User&#62; matches = new ArrayList&#60;User&#62;();
        for (Map.Entry&#60;Integer, User&#62; entry : users.entrySet()) {
            User potentialMatch = entry.getValue();
            if (potentialMatch.getLastName().contains(name)) {
                matches.add(potentialMatch);
            }
        }
        return matches;
    }
...
}</pre>
<p>The initContent() an the registerCaller() method are only utilities methods (full source code available <a href="https://bitbucket.org/looztra/guicyjsr107app/src/54a8fd3cd8be/src/main/java/net/awl/ismp/guice/jsr107/SimpleUserService.java">here</a>).</p>
<p>The interesting parts are the two annotations <a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/annotation/CacheResult.java">@CacheResult</a> and <a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/annotation/CacheKeyParam.java">@CacheKeyParam</a>.</p>
<p>Straight from the javadoc: <span style="color: #ff6600;">When a method annotated with <em>@CacheResult</em> is invoked a CacheKey will be generated and javax.cache.Cache#get(Object) is called before the invoked method actually executes. If a value is found in the cache it is returned and the annotated method is never actually executed. If no value is found the annotated method is invoked and the returned value is stored in the cache with the generated key.</span></p>
<p>The @CacheResult is then a very simple way to set up a <a href="http://ehcache.org/documentation/user-guide/concepts#read-through">Read-Through cache</a>.</p>
<p>The <em>@CacheKeyParam</em> is used to specify which subset of the method params are to be used as part of the CacheKey (by default, all the method params are used).</p>
<p>Those 2 annotations are not the only one existing of course. You could use @CachePut to implement a Write-Through cache for instance.</p>
<p>Before it can work as expected, don&#8217;t forget to register the <a href="https://github.com/jsr107/RI/blob/master/cache-annotations-ri/cache-annotations-ri-guice/src/main/java/javax/cache/annotation/impl/guice/module/CacheAnnotationsModule.java">CacheAnnotationsModule</a> so that Guice injector can use the jsr107 annotations.</p>
<pre class="brush:java">...
@BeforeClass
    public void setUp() {
        Injector injector = Guice.createInjector(new MyModule(),new CacheAnnotationsModule());
        service = injector.getInstance(UserService.class);
    }
...</pre>
<p><em>MyModule</em> being a very simple Guice module matching my simple needs:</p>
<pre class="brush:java">package net.awl.ismp.guice.jsr107;

import com.google.inject.AbstractModule;

/**
 *
 * @author looztra
 */
public class MyModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(UserService.class).to(SimpleUserService.class);
    }

}</pre>
<p>A simple unit test to see it in action</p>
<pre class="brush:java">
    @Test
    public void testGetIdTwice() {
        Assert.assertNotNull(service);
        int callId = getCallerId();
        log.info("testGetIdTwice(): calling the service the first time with callerId &#60;" + callId + "&#62;");
        User found = service.getUserById(testGetIdTwice_SEARCHED_ID, callId);
        Assert.assertNotNull(found);
        log.info("testGetIdTwice(): found user &#60;" + found + "&#62;");
        // check that we went through the business method the time
        Assert.assertTrue(service.calledBy(callId));
        //
        int callId2 = getCallerId();
        log.info("testGetIdTwice(): calling the service the second time with callerId &#60;" + callId2 + "&#62;");
        User found2 = service.getUserById(testGetIdTwice_SEARCHED_ID, callId2);
        Assert.assertNotNull(found2);
        // check that we did not go through the business method this time
        Assert.assertFalse(service.calledBy(callId2));

    }
</pre>
<p>Feel free to get the full source code that is available <a href="https://bitbucket.org/looztra/guicyjsr107app/src">here</a> as a <a href="http://mercurial.selenic.com/">Mercurial</a> repository on <a href="http://bitbucket.org/">Bitbucket</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://722.kalaari.net/b/lang/en/2011/12/23/jsr107-javax-cache-annotations-with-google-guice/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some jRRD news</title>
		<link>http://722.kalaari.net/b/lang/en/2010/02/17/some-jrrd-news</link>
		<comments>http://722.kalaari.net/b/lang/en/2010/02/17/some-jrrd-news#comments</comments>
		<pubDate>Wed, 17 Feb 2010 17:23:31 +0000</pubDate>
		<dc:creator>Christophe Furmaniak</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[jrrd]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jrobin]]></category>
		<category><![CDATA[rrd]]></category>
		<category><![CDATA[rrd4j]]></category>

		<guid isPermaLink="false">http://722.kalaari.net/b/?p=90</guid>
		<description><![CDATA[<a href="http://722.kalaari.net/b/lang/en/2010/02/17/some-jrrd-news" title="Some jRRD news"></a>Some (old) news about jRRD, a pure java library that allows you to read Round Robin Database files created by rrdtool. The project has been &#8220;mavenized&#8221; and moved to SVN (ok, maybe we should have switched directly to Git, please &#8230;<p class="read-more"><a href="http://722.kalaari.net/b/lang/en/2010/02/17/some-jrrd-news">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://722.kalaari.net/b/lang/en/2010/02/17/some-jrrd-news" title="Some jRRD news"></a><p>Some (old) news about <a href="http://jrrd.sourceforge.net/index.html">jRRD</a>, a pure java library that allows you to read <a href="http://oss.oetiker.ch/rrdtool/">Round Robin Database</a> files created by rrdtool.</p>
<p>The project has been &#8220;mavenized&#8221; and moved to SVN (ok, maybe we should have switched directly to Git, please <a href="http://vodpod.com/watch/65074-tech-talk-linus-torvalds-on-git">don&#8217;t tell Linus we are still using SVN</a>).</p>
<p>A <a href="http://jrrd.sourceforge.net/download.html">version 0.3</a> has been released, a full changelog can be found <a href="http://jrrd.sourceforge.net/news.html">here</a>. The main changes are:</p>
<ul>
<li>a patch to fix the case of multiple datasources has been applied (initialy provided by Juraj Sucik).</li>
<li>supports version 3 rrd files (jRRD v0.1 only had support for version 1 rrd files)</li>
<li>x86_64 files (only tested on linux 64bits plateform, if you have access to other 64bits plateform, please contact us)</li>
</ul>
<p><br/><br />
The project is not really active because in its current state, it fits our needs, so fill free to wake it up and send us your feature requests.</p>
]]></content:encoded>
			<wfw:commentRss>http://722.kalaari.net/b/lang/en/2010/02/17/some-jrrd-news/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

