Giter Club home page Giter Club logo

devcenter-redis-java's Introduction

Using Redis from Java

This is an example of using Jedis to connecto 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 https://github.com/heroku/devcenter-redis-java.git

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] ------------------------------------------------------------------------
[INFO] Building redisSample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...

Run it with foreman:

$ foreman start
22:31:48 sample.1        | started with pid 74251
22:31:48 springsample.1  | started with pid 74252
22:31:48 sample.1        | Setting up new RedisPool for connection redis://redistogo:[email protected]:9411/
22:31:48 springsample.1  | Nov 21, 2011 10:31:48 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
22:31:48 springsample.1  | INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@95c083: startup date [Mon Nov 21 22:31:48 PST 2011]; root of context hierarchy
22:31:48 springsample.1  | Nov 21, 2011 10:31:48 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
22:31:48 springsample.1  | INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@d8d9850: 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
22:31:48 springsample.1  | Setting up new RedisPool for connection redis://redistogo:[email protected]:9411/
22:31:49 springsample.1  | Value set into Redis is: testValueSpring
22:31:49 sample.1        | Value set into Redis is: testValue
22:31:49 springsample.1  | Value retrieved from Redis is: testValueSpring
22:31:49 sample.1        | Value retrieved from Redis is: testValue
22:31:49 springsample.1  | process terminated
22:31:49 system          | sending SIGTERM to all processes

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

:::java
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
http://quiet-waterfall-6274.herokuapp.com/ | [email protected]:quiet-waterfall-6274.git
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] ------------------------------------------------------------------------
       [INFO] Building redisSample 0.0.1-SNAPSHOT
       [INFO] ------------------------------------------------------------------------
       Downloading: http://s3pository.heroku.com/jvm/redis/clients/jedis/2.0.0/jedis-2.0.0.pom
       ...
       [INFO] ------------------------------------------------------------------------
       [INFO] BUILD SUCCESS
       [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
       http://quiet-waterfall-6274.herokuapp.com 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

and

$ heroku run springsample
Running springsample attached to terminal... up, run.2
Nov 22, 2011 10:18:54 PM org.springframework.context.support.AbstractApplicationContext 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 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d10c424: 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

devcenter-redis-java's People

Contributors

jsimone avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.