Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (80 sloc) 5.63 KB

Using MongoDB from Java

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

Using The Sample Code

Clone the repo with:

$ git clone

Start up MongoDB locally and set the MONGOHQ_URL environment variable:

$ export MONGOHQ_URL="mongodb://:@localhost:6379/dbname"

Build the sample:

$ mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mongoSample 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 Mongo HQ Service

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

Deploy Sample Using Git

$ git push heroku master
Counting objects: 62, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (45/45), done.
Writing objects: 100% (62/62), 11.52 KiB, done.
Total 62 (delta 12), reused 0 (delta 0)

-----> 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_31ktigwl5losz -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 mongoSample 0.0.1-SNAPSHOT
       [INFO] ------------------------------------------------------------------------
       Downloaded: (850 B at 2.1 KB/sec)
       Downloaded: (3 KB at 17.6 KB/sec)

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
Launching MongoDB sample application.
Collections found in DB: [system.indexes, system.users]


$ heroku run springsample
Running springsample attached to terminal... up, run.2
Launching MongoDB sample application with Spring configuration.
Dec 1, 2011 11:40:14 PM prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6345e044: startup date [Thu Dec 01 23:40:14 UTC 2011]; root of context hierarchy
Dec 1, 2011 11:40:14 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,getDb]; root of factory hierarchy
Setting up new mongo client for connection mongodb://
Collections found in DB (Spring Configuration): [system.indexes, system.users]