To create our first Infinispan project we are going to use Eclipse with a specific Maven archetype to create the skeleton of our project.
Tip
Maven provides a useful mechanism called Maven Archetypes that you can use to define a template for project creation; the archetype can create the skeleton of a project with the right structure and dependencies.
Infinispan currently provides two archetypes that we can use to create a skeleton project, which includes everything you need to start to use Infinispan. The archetype also generates a sample code and a pom.xml
file with all required dependencies.
To create an Infinispan project using the Maven template inside Eclipse, you will have to add the Infinispan archetype. To do so perform the following steps:
Navigate to File | New | Project. In the dialog box that opens, select the existing Maven projects option inside the
Maven
folder.Select your project location and press the Next button in the dialog that will appear.
On the screen that comes up next, hit the Add Archetype button, as shown in the following screenshot:
A new dialog box will appear asking you to add the new Archetype; include the information for the new project archetype, as in the following screenshot and press OK:
The new archetype should now be available in the list. Select it and confirm by pressing the Next button, as shown in the following screenshot;
You will be prompted to enter some information to create the project; in this example, enter the Group ID (
com.packtpub
) and the Artifact ID (infinispan-sample
), for the rest, keep the default values as they are.You should then be taken back to the Project Explorer where you can see your new project created.
The first thing to notice is the
pom.xml
file, which includes the necessary dependencies, such as the infinispan-embedded module (see the following code), which provides the basic functionalities:<dependencies> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-embedded</artifactId> <version>${version.infinispan}</version> </dependency> </dependencies>
This also includes plugins to enforce the correct versions of Java and Maven, the JBoss public repository configuration, and a profile to run the sample application created.
In the structure of the project, you can find two classes for the sample application:
SampleCacheContainer.java
andApplication.java
. TheSampleCacheContainer
class contains all the logic to create and configure an embedded cache manager. The sample application ships with four different Infinispan files, to test different configurations.The
Application
class is responsible for actually starting the program, and it executes four distinct operations. This example is demonstrated as follows:It presents a simple example of how to use Infinispan API, the cache stores and replaces arbitrary strings.
This demonstrates how you can use Infinispan to store an entry with a lifespan.
This demonstrates asynchronous operations, where writes can be done in a non-blocking fashion.
This demonstrates how to register listeners.
The following code is an extract from the main method
Application
class:public static void main(String[] args) throws Exception { System.out.println("\n\n\n ******************************** \n\n\n"); System.out.println("Hello. This is a sample application making use of Infinispan."); Application a = new Application(); a.basicUse(); a.lifespans(); a.asyncOperations(); a.registeringListeners(); System.out.println("Sample complete."); System.out.println("\n\n\n ******************************** \n\n\n"); }
Examine the source code to figure out what its doing and execute the
Application
class. TheApplication
class will execute and print the four operations on your prompt, which is shown in the following screenshot:
You have also the option to create an Infinispan manually using the same archetype that was used before. To do so, perform the following steps:
To create a new project manually, open a terminal prompt and navigate to the directory you want to create the project then type the following command:
mvn archetype:generate \ -DarchetypeGroupId=org.infinispan.archetypes \ -DarchetypeArtifactId=newproject-archetype \ -DarchetypeVersion=1.0.14 \ -DarchetypeRepository=http://repository.jboss.org/nexus/content/groups/public
To execute the application, type the following command in the same folder you created the project in:
mvn install –Prun
That's all! The application will execute and print the four operations on your prompt.