首页 > 代码库 > kestrel用java来利用

kestrel用java来利用

kestrel用的是scala,java无法直接使用。如想用java来运行kestrel,需要利用Xmemcached库。
 
创建queue_kestrel.java
# cd
# mkdir test
# export source_dir=~/test
# vi queue_kestrel.java
 
-----------------------------------------------------
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.KestrelCommandFactory;
import net.rubyeye.xmemcached.utils.AddrUtil;
 
public class queue_kestrel {
public static void main(String[] args) throws Exception {
XMemcachedClientBuilder builder=null;
MemcachedClient client=null;
int expirationTime=0;
String queueName="test";
String host="192.168.172.99";
String port = "22133";
builder=new XMemcachedClientBuilder(AddrUtil.getAddresses(host+":"+port));
builder.setCommandFactory(new KestrelCommandFactory());
try {
client=builder.build();
client.set(queueName,expirationTime,"hello");
String res=client.get(queueName);
System.out.println(res);
} catch (java.io.IOException e){}
}
}
----------------------------------------------------
 
准备必要的库。
xmemcached
# wget http://xmemcached.googlecode.com/files/xmemcached-1.3.7-bin-with-dependencies.tar.gz
# tar zxvf xmemcached-1.3.7-bin-with-dependencies.tar.gz
# cp xmemcached-1.3.7/xmemcached-1.3.7.jar xmemcached-1.3.7/lib/slf4j-api-1.5.7.jar $source_dir
 
slf4j
# wget http://www.slf4j.org/dist/slf4j-1.6.6.zip
# unzip slf4j-1.6.6.zip
# cp slf4j-1.6.6/slf4j-jdk14-1.6.6.jar $source_dir
 
kestrel
# cp /usr/local/kestrel/kestrel_2.9.1-2.2.0.jar $source_dir
 
运行
启动kestrel服务器。
# java -jar /usr/local/kestrel/kestrel_2.9.1-2.2.0.jar &
 
编译queue_kestrel.java并运行。
# cd $source_dir
# javac -cp ./xmemcached-1.3.7.jar:./slf4j-api-1.5.7.jar:./slf4j-jdk14-1.6.6.jar:./kestrel_2.9.1-2.2.0.jar: queue_kestrel.java
# java -cp ./xmemcached-1.3.7.jar:./slf4j-api-1.5.7.jar:./slf4j-jdk14-1.6.6.jar:./kestrel_2.9.1-2.2.0.jar: queue_kestrel

kestrel用java来利用