Skip to content

Instantly share code, notes, and snippets.

@louismrose
Created December 3, 2012 21:00
Show Gist options
  • Save louismrose/4197983 to your computer and use it in GitHub Desktop.
Save louismrose/4197983 to your computer and use it in GitHub Desktop.
ENAR - Redis on Heroku
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;
}
}
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.0.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
@louismrose
Copy link
Author

Don't forget to add a Redis add-on to your Heroku app! I used the Redis Cloud add-on.

@louismrose
Copy link
Author

No I didn't! I used the Redistogo add-on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment