Created
December 3, 2012 21:00
-
-
Save louismrose/4197983 to your computer and use it in GitHub Desktop.
ENAR - Redis on Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletOutputStream; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import redis.clients.jedis.Jedis; | |
import redis.clients.jedis.JedisPool; | |
import redis.clients.jedis.JedisPoolConfig; | |
import redis.clients.jedis.Protocol; | |
public class HelloServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
ServletOutputStream out = resp.getOutputStream(); | |
String output = ""; | |
try { | |
Jedis store = openConnection(); | |
store.set("changenum", "third"); | |
output = "Hello: here's my " + store.get("changenum") + " change"; | |
} catch (Exception e) { | |
output = e.getMessage(); | |
} | |
out.write(output.getBytes()); | |
out.flush(); | |
out.close(); | |
} | |
private Jedis openConnection() throws URISyntaxException { | |
URI redisURI = new URI(System.getenv("REDISTOGO_URL")); | |
JedisPool pool = new JedisPool(new JedisPoolConfig(), redisURI.getHost(), | |
redisURI.getPort(), | |
Protocol.DEFAULT_TIMEOUT, | |
redisURI.getUserInfo().split(":",2)[1]); | |
Jedis store = pool.getResource(); | |
return store; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>redis.clients</groupId> | |
<artifactId>jedis</artifactId> | |
<version>2.0.0</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to add a Redis add-on to your Heroku app! I used the Redis Cloud add-on.