Uncategorized

QuickStart Infinispan Cache

Infinispan is an easy to configure caching mechanism for java based applications. It can be used as  stand alone and cluster modes. It can be run in a server as well  as a simple java process.  The following content will describe using infinispan as a java process. And all the dependencies will be packed as a jar file to start the cache with the help of maven.

Prerequisites: 1. Download maven and set the path in system environment variables. 2. internet connection

Use below command to create a new java project using maven

mvn archetype:generate -DgroupId=com.infinispan -DartifactId=infinispan -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

1. Create a maven simple project with following settings

<groupId>com.infinispan</groupId>
    <artifactId>infinispan</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>infinispan</name>

Simple Java Project Using Eclipse and Maven

2. Add following dependencies in pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<infinispan-version>5.3.0.Final</infinispan-version>
</properties>

<dependencies>
<!– Infinispan Dependencies –>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>5.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
<version>5.3.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.infinispan.StartCache</mainClass>
<packageName>com.infinispan</packageName>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>

</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.infinispan.StartCache</mainClass>
<packageName>com.infinispan</packageName>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

3. Write a java class to start infinispan cache container

// Get the Cache Container
EmbeddedCacheManager cacheManager = new DefaultCacheManager(
“app-infinispan.xml”);
// Get the Cache Store.
Cache<Object, Object> generalStore = cacheManager
.getCache(“generalStore”);
// Put into cache store.
generalStore.put(“email”, “me@mymail.com”);
// retrieve from cache store.
System.out.println(“Fetched fromCache:” + generalStore.get(“email”)+” Cache Size:”+generalStore.size());

System.exit(0);

4. Go to the project base folder and build the project by maven command

> mvn clean compile assembly:assembly

5. Go to BaseDirectory/infinispan/target folder and run the jar as below,

> java -jar infinispan-0.0.1-SNAPSHOT-jar-with-dependencies.jar

this will start infinisapn cache container in the a java process.

6. To start cluster environment, run the above jar file from multiple command prompts.

Infinispan automatically discover and form the cluster with all cache containers.

7. Use the infinispan client to put and get the objects.

// Get the Cache Container
EmbeddedCacheManager cacheManager = new DefaultCacheManager(
“app-infinispan.xml”);
// Get the Cache Store.
Cache<Object, Object> generalStore = cacheManager
.getCache(“generalStore”);
// Put into cache store.
generalStore.put(“email”, “me@mymail.com”);
// retrieve from cache store.
System.out.println(“Fetched fromCache:” + generalStore.get(“email”)+” Cache Size:”+generalStore.size());

System.exit(0);

For an easy start, import the project into eclipse and use the code.

Download Project

For more information on infinispan, visit http://infinispan.org/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s