Created
June 29, 2010 23:04
-
-
Save alphazero/457960 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* 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 org.jredis.ri.alphazero.v2; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.Future; | |
import org.jredis.RedisException; | |
import org.jredis.connector.ConnectionSpec; | |
import org.jredis.protocol.Command; | |
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec; | |
import org.jredis2.JRedisNew; | |
import org.jredis2.JRedisNew.Monitor.Event; | |
import org.jredis2.JRedisNew.Topic.Listener; | |
import org.jredis2.JRedisNew.Topic.Message; | |
public class UsingTheNewAPI { | |
public static void main (String[] args) { | |
new UsingTheNewAPI().run(); | |
} | |
// ======================================================================== | |
// Test the new semantics | |
// ======================================================================== | |
private void run () { | |
ConnectionSpec spec = DefaultConnectionSpec.newSpec().setCredentials("jredis".getBytes()); | |
JRedisNew jredis = new JRedisNewSupport(spec); | |
try { | |
// admin | |
useAdminApi(jredis); | |
// transactions | |
useTransactions(jredis); | |
// event monitor | |
useMonitorApi(jredis); | |
// topic subscriptions | |
useTopicApi(jredis); | |
// synchronous request/reply semantics | |
useSync(jredis); | |
// asynchronous future response semantics | |
useAsync(jredis); | |
} | |
catch (RedisException e) { | |
} | |
} | |
// ======================================================================== | |
// use the Admin API | |
// ======================================================================== | |
public void useAdminApi(JRedisNew jredis) throws RedisException { | |
jredis.admin().slaveofnone(); | |
} | |
// ======================================================================== | |
// use the Monitor API | |
// ======================================================================== | |
/** | |
* Starts the monitor, waits a minute, and then stops the monitor. | |
* @param jredis | |
* @throws RedisException | |
*/ | |
public void useMonitorApi(JRedisNew jredis) throws RedisException { | |
JRedisNew.Monitor.Callback callback = newMonitorCallback(); | |
final JRedisNew.Monitor monitor = jredis.monitor(callback, (Object[])null); | |
// monitor.callback will be getting event callbacks until this thread stops the monitor ... | |
new Thread(new Runnable(){ | |
public void run() { | |
try { Thread.sleep(1000 * 60); /* wait a minute .. */ } | |
catch (InterruptedException e) { /* no op */ } | |
finally { monitor.stop(); } | |
} | |
}).start(); | |
} | |
// ======================================================================== | |
// use the Topic API | |
// ======================================================================== | |
/** | |
* Subscribes to a topic, waits a minute, and then unsubscribes. | |
* @param jredis | |
* @throws RedisException | |
*/ | |
public void useTopicApi(JRedisNew jredis) throws RedisException { | |
JRedisNew.Topic.Listener listener = newTopicListener(); | |
final JRedisNew.Topic topic = jredis.subscribe("my-topic".getBytes(), listener, (Object[])null); | |
// listener will be getting message callbacks until this thread unsubscribes ... | |
new Thread(new Runnable(){ | |
public void run() { | |
try { Thread.sleep(1000 * 60); /* wait a minute .. */ } | |
catch (InterruptedException e) { /* no op */ } | |
finally { topic.unsubscribe(); } | |
} | |
}).start(); | |
} | |
// ======================================================================== | |
// use the Transaction API | |
// ======================================================================== | |
public void useTransactions(JRedisNew jredis) throws RedisException { | |
JRedisNew.Tx tx = jredis.beginTx((Object[])null); | |
tx.exec(Command.LPOP, "foo-list"); | |
tx.exec(Command.DECR, "foo-cntr"); | |
tx.commit(); | |
} | |
// ======================================================================== | |
// use the Sync semantics API | |
// ======================================================================== | |
public void useSync(JRedisNew jredis) throws RedisException { | |
Long cntr = (Long) jredis.sync().exec(Command.INCR, "sync-cntr"); | |
System.out.format("sync-cntr: %d\n", cntr); | |
} | |
// ======================================================================== | |
// use the Async semantics API | |
// ======================================================================== | |
public void useAsync(JRedisNew jredis) throws RedisException { | |
Future<Object> future_cntr = null; | |
try { | |
future_cntr = jredis.async().exec(Command.INCR, "async-cntr"); | |
Long fcntrv = (Long) future_cntr.get(); | |
System.out.format("async-cntr: %d\n", fcntrv); | |
} | |
catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
catch (ExecutionException e) { | |
e.printStackTrace(); | |
} | |
} | |
// ======================================================================== | |
// Support classes for the async callback interfaces | |
// ======================================================================== | |
public JRedisNew.Topic.Listener newTopicListener() { | |
return new Listener() { | |
public void onMessage (Message msg) { | |
System.out.format("On message: %s\n", msg); | |
} | |
}; | |
} | |
public JRedisNew.Monitor.Callback newMonitorCallback() { | |
return new JRedisNew.Monitor.Callback() { | |
public void onEvent (Event event) { | |
System.out.format("On event: %s\n", event); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment