Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Java portion of the Devcenter article on Redis
branch: master
Failed to load latest commit information.
src/main update log statements
.gitignore add files style and typo correction
Procfile split article from readme. Add procfile.
pom.xml java.version doesn't work for pointing to java version in compiler pl…

Using Redis from Java

This is an example of using Jedis to connect to the Redis To Go service from both a generic Java application and a Spring configured application on Heroku. Read more about how to use Redis To Go in the add-on article.

Using The Sample Code

Clone the repo with:

$ git clone

Start up Redis locally and set the REDISTOGO_URL environment variable:

$ export REDISTOGO_URL="redis://:@localhost:6379"

Build the sample:

$ mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building redisSample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------

Now that it's built, you can run the plain Java example, or the Spring example:

  • To run the plain Java example:

    $ sh target/bin/sample

  • To run the Spring example:

    $ sh target/bin/spring-sample

Note: you can also use foreman to execute the Procfile. [Read more about foreman and procfiles](

You can switch between the Java and XML based configuration by commenting out one of the two lines in in the spring sub-package:

public class Main {

    public static void main(String[] args) throws Exception{

        // If you want Java based configuration:
        final ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        // If you want XML based configuration:
        //final ApplicationContext ctx = new GenericXmlApplicationContext("applicationContext.xml");


Test on Heroku

Assuming you already have a Heroku account and have installed the Heroku command line tool, you can test this sample on Heroku in a few steps.

Create Heroku App

$ heroku create -s cedar
Creating quiet-waterfall-6274... done, stack is cedar |
Git remote heroku added

Add Redis To Go Service

$ heroku addons:add redistogo:nano
-----> Adding redistogo:nano to quiet-waterfall-6274... done, v1 (free)

Deploy Sample Using Git

$ git push heroku master
Counting objects: 94, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (94/94), 14.58 KiB, done.
Total 94 (delta 22), reused 57 (delta 13)

-----> Heroku receiving push
-----> Java app detected
-----> Installing Maven 3.0.3..... done
-----> Installing settings.xml..... done
-----> executing /app/tmp/repo.git/.cache/.maven/bin/mvn -B -Duser.home=/tmp/build_2trks2rwxstiq -Dmaven.repo.local=/app/tmp/repo.git/.cache/.m2/repository -s /app/tmp/repo.git/.cache/.m2/settings.xml -DskipTests=true clean install
       [INFO] Scanning for projects...
       [INFO] ------------------------------------------------------------------------
       [INFO] Building redisSample 0.0.1-SNAPSHOT
       [INFO] ------------------------------------------------------------------------
       [INFO] ------------------------------------------------------------------------
       [INFO] ------------------------------------------------------------------------
       [INFO] Total time: 7.055s
       [INFO] Finished at: Tue Nov 22 22:18:19 UTC 2011
       [INFO] Final Memory: 10M/490M
       [INFO] ------------------------------------------------------------------------
-----> Discovering process types
       Procfile declares types -> sample, springsample
-----> Compiled slug size is 2.4MB
-----> Launching... done, v4 deployed to Heroku

Execute the Sample Code as One-Off Processes

The two sample apps are listed as two process types, "sample" and "springsample". They are designed to be executed as one-off processes, so you execute them with

$ heroku run sample
Running sample attached to terminal... up, run.1
Value set into Redis is: testValue
Value retrieved from Redis is: testValue


$ heroku run springsample
Running springsample attached to terminal... up, run.2
Nov 22, 2011 10:18:54 PM prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@50a9ae05: startup date [Tue Nov 22 22:18:54 UTC 2011]; root of context hierarchy
Nov 22, 2011 10:18:54 PM preInstantiateSingletons
INFO: Pre-instantiating singletons in defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springConfig,getJedisPool]; root of factory hierarchy
Value set into Redis is: testValueSpring
Value retrieved from Redis is: testValueSpring
Something went wrong with that request. Please try again.