gearman Java Client




import java.util.concurrent.ExecutionException;

import org.gearman.client.GearmanClient;
import org.gearman.client.GearmanClientImpl;
import org.gearman.client.GearmanJob;
import org.gearman.client.GearmanJobImpl;
import org.gearman.client.GearmanJobResult;
import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.util.ByteUtils;

class PrintThread extends Thread{
public void run(){
ClientTest test=new ClientTest("192.168.101.143", 4730);
System.out.println(test.reverse("hello"));
}
}
public class ClientTest {
GearmanClient client;
GearmanNIOJobServerConnection con;
public ClientTest(String host, int port) {
con = new GearmanNIOJobServerConnection(host, port);
client = new GearmanClientImpl();
client.addJobServer(con);//添加一个任务
}

public String reverse(String input) {
String function = ReverseFunction.class.getCanonicalName();//调用函数
ReverseFunction rf=new ReverseFunction();
//String reverseValue=rf.reverseValue(input);
byte[] data = ByteUtils.toUTF8Bytes(input);
String value = "";

GearmanJob job = GearmanJobImpl.createJob(function, data, null);
client.submit(job);

try {
GearmanJobResult rs = job.get();
value = ByteUtils.fromUTF8Bytes(rs.getResults());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;

}
public void shutdown()
{
if(null==client){
System.out.println("no client to shutdown");
}
client.shutdown();
}
public static void main(String[] args) throws Exception {
String host="192.168.101.143";
int port =4730;
String input="woyo";
ClientTest clientTest=new ClientTest(host, port);
/* for(int i=0;i<10;i++){
Thread t = new PrintThread();
t.start();
Thread.sleep(1000);
}*/
input=clientTest.reverse(input);
// System.out.println(input);
clientTest.shutdown();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值