// 1st example: using defaults
// if your server expects a AUTH password, this will fail so see next block
{
// Note that if your localhost:6379 redis server expects a password
// this will fail
ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec();
JRedisClient jredis = new JRedisClient(connectionSpec);
try {
jredis.ping();
Log.log("Sweet success -- we're connected using all default values.");
}
catch (RedisException e) {
Log.error("Failed to connect to Redis using JRedisClient default ConnectionSpec -- password perhaps? => " + e.getLocalizedMessage());
}
}
// 2nd example: using defaults but setting the password and the database.
// also demonstrated using the method chaining on the ConnectionSpec property setters.
{
// Note that if your localhost:6379 redis server expects a password
// this will fail
String password = "jredis";
int database = 11;
ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec();
connectionSpec
.setCredentials(password.getBytes())
.setDatabase(database);
JRedisClient jredis = new JRedisClient(connectionSpec);
try {
jredis.ping();
Log.log("Sweet success -- we're connected using %s as password to %d database.", password, database);
}
catch (RedisException e) {
Log.error("Failed to connect to Redis using JRedisClient default ConnectionSpec -- password perhaps? => " + e.getLocalizedMessage());
}
}
// final example: using defaults but setting the full set of basic settings
{
// Note that if your localhost:6379 redis server expects a password
// this will fail
try {
String password = "jredis";
int port = 6379;
int database = 11;
InetAddress address = InetAddress.getLocalHost();
// 1 - get the default connection spec for the basic settings
//
ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec(address, port, database, password.getBytes());
// finally - use it to create the JRedisClient instance
//
JRedisClient jredis = new JRedisClient(connectionSpec);
jredis.ping();