Permalink
Browse files

HelloWorld quickstart

  • Loading branch information...
1 parent 9fc11df commit 43817d765864355981b49815d1253b73b3edad5a @mgencur mgencur committed with tristantarrant Oct 2, 2012
View
@@ -0,0 +1,133 @@
+helloworld-jdg: Basic Infinispan example
+====================================
+Author: Burr Sutter, Martin Gencur
+Level: Intermediate
+Technologies: Infinispan, CDI
+Summary: Shows how to use Infinispan in clustered mode, with expiration enabled.
+Target Product: JDG
+
+What is it?
+-----------
+
+HelloWorld-JDG is a basic example that shows how to store and retrieve data to/from the cache. Users can access the cache
+either from a servlet or from a JSF page through request scoped beans.
+
+Infinispan is configured in clustered distributed mode with synchronous replication. Entries have their lifespan (expiration)
+and are removed from the cache after 60 seconds since last update.
+
+HelloWorld-JDG example works in _Library mode_. In this mode, the application and the data grid are running in the same
+JVM. All libraries (JAR files) are bundled with the application and deployed to JBoss Enterprise Application Platform 6
+or JBoss AS 7. The library usage mode only allows local access to a single node in a distributed cluster. This usage
+mode gives the application access to data grid functionality within a virtual machine in the container being used.
+
+
+System requirements
+-------------------
+
+All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven 3.0 or better.
+
+The application this project produces is designed to be run on JBoss Enterprise Application Platform 6 or JBoss AS 7.
+
+
+Configure Maven
+---------------
+
+If you have not yet done so, you must [Configure Maven](../README.md#mavenconfiguration) before testing the quickstarts.
+
+
+Start first instance of JBoss Enterprise Application Platform 6 or JBoss AS 7
+-----------------------------------------------------------------------------
+
+1. Open a command line and navigate to the root of the JBoss server directory.
+2. The following shows the command line to start the server with the web profile:
+
+ For Linux: JBOSS_HOME/bin/standalone.sh
+ For Windows: JBOSS_HOME\bin\standalone.bat
+
+Start second instance of JBoss Enterprise Application Platform 6 or JBoss AS 7
+------------------------------------------------------------------------------
+
+1. Make a second copy of the JBoss Server
+2. Open a command line and navigate to the root of the second JBoss server directory.
+3. Start the server with pre-configured port offset so that the server can run on the same host
+
+ For Linux: JBOSS_HOME2/bin/standalone.sh -Djboss.socket.binding.port-offset=100
+ For Windows: JBOSS_HOME2\bin\standalone.bat -Djboss.socket.binding.port-offset=100
+
+
+Build and Deploy the Quickstart
+-------------------------------
+
+_NOTE: The following build command assumes you have configured your Maven user settings. If you have not, you must
+include Maven setting arguments on the command line. See [Build and Deploy the Quickstarts](../README.md#buildanddeploy)
+for complete instructions and additional options._
+
+1. Make sure you have started both instances of the JBoss Server as described above.
+2. Open a command line and navigate to the root directory of this quickstart.
+3. Type this command to build and deploy the archive to the first server:
+
+ mvn clean package jboss-as:deploy
+
+4. This will deploy `target/jboss-as-helloworld-jdg.war` to the first running instance of the server.
+5. Type this command to build and deploy the archive to the second server (running on different ports):
+
+ mvn clean package jboss-as:deploy -Ddeploy.port=10099
+
+6. This will deploy `target/jboss-as-helloworld-jdg.war` to the second running instance of the server.
+
+
+Access the application
+----------------------
+
+The application will be running at the following URLs:
+
+ <http://localhost:8080/jboss-as-helloworld-jdg> (first server instance)
+ <http://localhost:8180/jboss-as-helloworld-jdg> (second server instance)
+
+You can test replication of entries in the following way:
+
+1. Access first server at <http://localhost:8080/jboss-as-helloworld-jdg> and insert key "foo" with value "bar"
+2. Access second server at <http://localhost:8180/jboss-as-helloworld-jdg> and do the following:
+ * Click on "Get Some"
+ * Get the value for key "foo"
+ * Click "Put Some More"
+ * Insert key "mykey" with value "myvalue"
+3. Access the first server at <http://localhost:8080/jboss-as-helloworld-jdg> and do the following:
+ * Click on "Get Some"
+ * Get all mappings by clicking on "Get All"
+4. All data entered on each server was replicated to the other server
+
+NOTE: Entries expire and simply disappear after 60 seconds from last update.
+
+To access predefined servlets and directly store/retrieve a key in the cache, access the following URLs:
+
+<http://localhost:8080/jboss-as-helloworld-jdg/TestServletPut>
+<http://localhost:8180/jboss-as-helloworld-jdg/TestServletGet> (note the different port 8180)
+
+
+Undeploy the Archive
+--------------------
+
+1. Make sure you have started the JBoss Server as described above.
+2. Open a command line and navigate to the root directory of this quickstart.
+3. When you are finished testing, type this command to undeploy the archive from both running servers:
+
+ mvn jboss-as:undeploy
+ mvn jboss-as:undeploy -Ddeploy.port=10099
+
+
+Run the Quickstart in JBoss Developer Studio or Eclipse
+-------------------------------------------------------
+You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information,
+see [Use JBoss Developer Studio or Eclipse to Run the Quickstarts](../README.md#useeclipse)
+
+
+Debug the Application
+---------------------
+
+If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following
+commands to pull them into your local repository. The IDE should then detect them.
+
+ mvn dependency:sources
+ mvn dependency:resolve -Dclassifier=javadoc
+
View
@@ -0,0 +1,149 @@
+<?xml version="1.0"?>
+<!-- JBoss, Home of Professional Open Source Copyright 2012, Red Hat, Inc.
+ and/or its affiliates, and individual contributors by the @authors tag. See
+ the copyright.txt in the distribution for a full listing of individual contributors.
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ use this file except in compliance with the License. You may obtain a copy
+ of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.jboss.datagrid.quickstart</groupId>
+ <artifactId>jboss-as-helloworld-jdg</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>war</packaging>
+ <name>JBoss AS Quickstarts: JBoss Data Grid HelloWorld</name>
+
+ <properties>
+ <!-- Explicitly declaring the source encoding eliminates the following
+ message: -->
+ <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
+ resources, i.e. build is platform dependent! -->
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+
+ <!-- JBoss dependency versions -->
+ <jboss.as.plugin.version>7.1.1.Final</jboss.as.plugin.version>
+
+ <!-- This is the JBoss management port, so jboss-as-maven-plugin
+ can deploy this quickstart on it -->
+ <deploy.port>9999</deploy.port>
+
+ <!-- This is the host where JBoss is running, so jboss-as-maven-plugin
+ can deploy this quickstart on it -->
+ <deploy.host>localhost</deploy.host>
+
+ <!-- other plugin versions -->
+ <compiler.plugin.version>2.3.2</compiler.plugin.version>
+ <buildhelper.plugin.version>1.7</buildhelper.plugin.version>
+ <war.plugin.version>2.2</war.plugin.version>
+
+ <!-- maven-compiler-plugin -->
+ <maven.compiler.target>1.6</maven.compiler.target>
+ <maven.compiler.source>1.6</maven.compiler.source>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- JBoss distributes a complete set of Java EE 6 APIs including
+ a Bill of Materials (BOM). A BOM specifies the versions of a "stack" (or
+ a collection) of artifacts. We use this here so that we always get the correct
+ versions of artifacts. Here we use the jboss-javaee-6.0-with-infinispan stack
+ and the jboss-javaee-6.0-with-transactions stack (you can read this as the
+ JBoss stack of the Java EE 6 APIs, with Infinispan and Transactions) -->
+ <dependency>
+ <groupId>org.jboss.bom</groupId>
+ <artifactId>jboss-javaee-6.0-with-infinispan</artifactId>
+ <version>1.0.1.Final</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.infinispan</groupId>
+ <artifactId>infinispan-core</artifactId>
+ <scope>compile</scope>
+ </dependency>
+
+ <!-- Import the Servlet API, we use provided scope as the API is included
+ in JBoss AS 7 -->
+ <dependency>
+ <groupId>org.jboss.spec.javax.servlet</groupId>
+ <artifactId>jboss-servlet-api_3.0_spec</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Import the CDI API, we use provided scope as the API is included
+ in JBoss AS 7 -->
+ <dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Import the JSF API, we use provided scope as the API is included
+ in JBoss AS 7 -->
+ <dependency>
+ <groupId>org.jboss.spec.javax.faces</groupId>
+ <artifactId>jboss-jsf-api_2.1_spec</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <!-- Set the name of the war, used as the context root when the app
+ is deployed -->
+ <finalName>${project.artifactId}</finalName>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${compiler.plugin.version}</version>
+ <configuration>
+ <source>${maven.compiler.source}</source>
+ <target>${maven.compiler.target}</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-war-plugin</artifactId>
+ <version>${war.plugin.version}</version>
+ <configuration>
+ <!-- Java EE 6 doesn't require web.xml, Maven needs to
+ catch up! -->
+ <failOnMissingWebXml>false</failOnMissingWebXml>
+ </configuration>
+ </plugin>
+ <plugin>
+ <!-- The JBoss AS plugin deploys your war to a local
+ JBoss AS container -->
+ <!-- To use, run: mvn package jboss-as:deploy -->
+ <groupId>org.jboss.as.plugins</groupId>
+ <artifactId>jboss-as-maven-plugin</artifactId>
+ <version>${jboss.as.plugin.version}</version>
+ <executions>
+ <execution>
+ <phase>install</phase>
+ <goals>
+ <goal>deploy</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <port>${deploy.port}</port>
+ <hostname>${deploy.host}</hostname>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
+
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.as.quickstarts.datagrid;
+
+import java.util.logging.Logger;
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.inject.Named;
+import org.infinispan.manager.DefaultCacheManager;
+import org.infinispan.Cache;
+import java.util.Set;
+
+/**
+ * Retrieves entries from the cache.
+ *
+ * @author Burr Sutter
+ *
+ */
+@Named
+@RequestScoped
+public class GetController {
+
+ @Inject
+ private Logger log;
+
+ @Inject
+ DefaultCacheManager m;
+
+ private String key;
+
+ private String message;
+
+ private StringBuffer allKeyValues = new StringBuffer();
+
+ // Called by the get.xhtml - get button
+ public void getOne() {
+ Cache<String, String> c = m.getCache();
+ message = c.get(key);
+ log.info("get: " + key + " " + message);
+ }
+
+ // Called by the get.xhtml - get all button
+ public void getAll() {
+ Cache<String, String> c = m.getCache();
+
+ Set<String> keySet = c.keySet();
+ for (String key : keySet) {
+
+ String value = c.get(key);
+ log.info("k: " + key + " v: " + value);
+
+ allKeyValues.append(key + "=" + value + ", ");
+ } // for
+
+ if (allKeyValues == null || allKeyValues.length() == 0) {
+ message = "Nothing in the Cache";
+ } else {
+ //remote trailing comma
+ allKeyValues.delete(allKeyValues.length() - 2, allKeyValues.length());
+ message = allKeyValues.toString();
+ }
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ public void setKey(String key) {
+ this.key = key;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+}
Oops, something went wrong.

0 comments on commit 43817d7

Please sign in to comment.