Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alphazero/240843 to your computer and use it in GitHub Desktop.
Save alphazero/240843 to your computer and use it in GitHub Desktop.
/*
* Copyright 2009 Joubin Houshyar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package temp.tests;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.jredis.JRedis;
import org.jredis.ProviderException;
import org.jredis.RedisException;
import org.jredis.connector.ConnectionSpec;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.JRedisPipeline;
import org.jredis.ri.alphazero.JRedisService;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
/**
* Edit your redis.conf to a timeout value of less than 10 (secs).
* This code has been tested with timeout of 3 seconds. Each
* test routine loops indefinitely while INCRing every 10 seconds.
*
* Heartbeat keeps the connections alive.
*
* @author joubin (alphazero@sensesay.net)
* @date Nov 22, 2009
*
*/
public class TestHearbeat {
public static void main (String[] args) throws RedisException {
// just uncomment the test you want to run.
testJRedisService();
// testJRedisClient();
// testJRedisPipeline();
}
private static void testJRedisClient () {
ConnectionSpec spec = DefaultConnectionSpec.newSpec().setCredentials("jredis".getBytes());
JRedis client = new JRedisClient(spec);
try {
String key = "cntr";
client.del(key);
long n = 0;
while (n < 10000000){
Thread.sleep(10 * 1000);
n = client.incr(key);
System.out.format("counter: %d\n", n);
}
client.quit();
}
catch (Exception e){
e.printStackTrace();
}
}
private static void testJRedisService () {
ConnectionSpec spec = DefaultConnectionSpec.newSpec().setCredentials("jredis".getBytes());
JRedis client = new JRedisService(spec, 10);
try {
String key = "cntr";
client.del(key);
long n = 0;
while (n < 10000000){
Thread.sleep(10 * 1000);
n = client.incr(key);
System.out.format("counter: %d\n", n);
}
client.quit();
}
catch (Exception e){
e.printStackTrace();
}
}
public static void testJRedisPipeline () throws RedisException {
ConnectionSpec spec = DefaultConnectionSpec.newSpec().setCredentials("jredis".getBytes());
JRedisPipeline pipeline = new JRedisPipeline(spec);
try {
String key = "cntr";
pipeline.sync().del(key);
long n = 0;
while (n < 10000000){
Thread.sleep(10 * 1000);
Future<Long> futureLong = null;
futureLong = pipeline.incr(key);
n = futureLong.get();
}
pipeline.quit();
}
catch (ProviderException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment