Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jesperfj/master
Remove config bean
- Loading branch information
Showing
6 changed files
with
202 additions
and
130 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
package com.heroku.devcenter; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.pool.impl.GenericObjectPool.Config; | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.JedisPoolConfig; | ||
import redis.clients.jedis.Protocol; | ||
|
||
public class Main { | ||
|
||
protected static final Pattern HEROKU_REDISTOGO_URL_PATTERN = Pattern.compile("^redis://([^:]*):([^@]*)@([^:]*):([^/]*)(/)?"); | ||
|
||
public static void main(String[] args) { | ||
//Parse the configuration URL | ||
Matcher matcher = HEROKU_REDISTOGO_URL_PATTERN.matcher(System.getenv("REDISTOGO_URL")); | ||
matcher.matches(); | ||
|
||
//Create the connection pool from the values in the URL | ||
//This pool can be stored in a singleton and reused | ||
JedisPool pool = new JedisPool(new Config(), matcher.group(3), Integer.parseInt(matcher.group(4)), Protocol.DEFAULT_TIMEOUT, matcher.group(2)); | ||
JedisPool pool; | ||
try { | ||
URI redisURI = new URI(System.getenv("REDISTOGO_URL")); | ||
System.out.println("Setting up new RedisPool for connection "+redisURI); | ||
pool = new JedisPool(new JedisPoolConfig(), | ||
redisURI.getHost(), | ||
redisURI.getPort(), | ||
Protocol.DEFAULT_TIMEOUT, | ||
redisURI.getUserInfo().split(":",2)[1]); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException("Redis couldn't be configured from URL in REDISTOGO_URL env var: "+ | ||
System.getenv("REDISTOGO_URL")); | ||
} | ||
Jedis jedis = pool.getResource(); | ||
String testValue = "testValue"; | ||
|
||
jedis.set("testKey", testValue); | ||
System.out.println("Value set into Redis is: " + testValue); | ||
|
||
System.out.println("Value retrieved from Redis is: " + jedis.get("testKey")); | ||
|
||
try { | ||
String testValue = "testValue"; | ||
|
||
jedis.set("testKey", testValue); | ||
System.out.println("Value set into Redis is: " + testValue); | ||
|
||
System.out.println("Value retrieved from Redis is: " | ||
+ jedis.get("testKey")); | ||
} finally { | ||
pool.returnResource(jedis); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
package com.heroku.devcenter.spring; | ||
|
||
import org.apache.commons.pool.impl.GenericObjectPool.Config; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.support.GenericXmlApplicationContext; | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.Protocol; | ||
|
||
public class Main { | ||
|
||
|
||
public static void main(String[] args) { | ||
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); | ||
//ApplicationContext ctx = new GenericXmlApplicationContext("applicationContext.xml"); | ||
RedisConfig config = ctx.getBean(RedisConfig.class); | ||
//Create the connection pool from the values in the URL | ||
//This pool can be stored in a singleton and reused | ||
JedisPool pool = new JedisPool(new Config(), config.getHost(), config.getPort(), Protocol.DEFAULT_TIMEOUT, config.getPassword()); | ||
|
||
JedisPool pool = ctx.getBean(JedisPool.class); | ||
Jedis jedis = pool.getResource(); | ||
String testValue = "testValueSpring"; | ||
|
||
jedis.set("testKey", testValue); | ||
System.out.println("Value set into Redis is: " + testValue); | ||
|
||
System.out.println("Value retrieved from Redis is: " + jedis.get("testKey")); | ||
try { | ||
String testValue = "testValueSpring"; | ||
|
||
jedis.set("testKey", testValue); | ||
System.out.println("Value set into Redis is: " + testValue); | ||
|
||
System.out.println("Value retrieved from Redis is: " + jedis.get("testKey")); | ||
} finally { | ||
pool.returnResource(jedis); | ||
} | ||
} | ||
|
||
} |
29 changes: 0 additions & 29 deletions
29
src/main/java/com/heroku/devcenter/spring/RedisConfig.java
This file was deleted.
Oops, something went wrong.
34 changes: 20 additions & 14 deletions
34
src/main/java/com/heroku/devcenter/spring/SpringConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
package com.heroku.devcenter.spring; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.JedisPoolConfig; | ||
import redis.clients.jedis.Protocol; | ||
|
||
@Configuration | ||
public class SpringConfig { | ||
|
||
@Bean | ||
public RedisConfig getRedisConfig() { | ||
Pattern pattern = Pattern.compile("^redis://([^:]*):([^@]*)@([^:]*):([^/]*)(/)?"); | ||
//Parse the configuration URL | ||
Matcher matcher = pattern.matcher(System.getenv("REDISTOGO_URL")); | ||
matcher.matches(); | ||
|
||
RedisConfig config = new RedisConfig(); | ||
config.setHost(matcher.group(3)); | ||
config.setPort(Integer.parseInt(matcher.group(4))); | ||
config.setPassword(matcher.group(2)); | ||
return config; | ||
} | ||
public JedisPool getJedisPool() { | ||
try { | ||
URI redisURI = new URI(System.getenv("REDISTOGO_URL")); | ||
System.out.println("Setting up new RedisPool for connection "+redisURI); | ||
return new JedisPool(new JedisPoolConfig(), | ||
redisURI.getHost(), | ||
redisURI.getPort(), | ||
Protocol.DEFAULT_TIMEOUT, | ||
redisURI.getUserInfo().split(":",2)[1]); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException("Redis couldn't be configured from URL in REDISTOGO_URL env var: "+ | ||
System.getenv("REDISTOGO_URL")); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.