Gearman使用示例

最近的一个旧项目重构过程中,使用到了gearman这个开源项目,简单来讲,这是一个类似MQ的异步系统,一边派发任务,一边处理任务(有类似MQ中的消息发送方与接收方),目前支持java,php等多种语言,缺点是存在单点问题(server的HA官方没有提供方案,需要二次开发)。

官网地址:http://www.gearman.org

下面是java语言的示例:

注:gearman的java客户端实例有好几个版本,不同的版本之间相差巨大,建议使用官方推荐的最新版,地址为https://github.com/gearman/java-service

一、spring配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="gearmanImpl" class="org.gearman.impl.GearmanImpl"></bean> <bean id="gearmanServer" class="org.gearman.impl.server.remote.GearmanServerRemote">
<constructor-arg index="0" ref="gearmanImpl"/>
<constructor-arg index="1">
<bean class="java.net.InetSocketAddress">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="4730"/>
</bean>
</constructor-arg>
</bean> <bean id="gearmanClient" class="org.gearman.impl.client.ClientImpl">
<constructor-arg index="0" ref="gearmanImpl"/>
</bean> <bean id="gearmanWorker" class="org.gearman.impl.worker.GearmanWorkerImpl">
<constructor-arg index="0" ref="gearmanImpl"/>
</bean> </beans>

二、任务发送方(也称Client)

import org.gearman.GearmanJobEvent;
import org.gearman.GearmanJobReturn;
import org.gearman.GearmanServer;
import org.gearman.impl.client.ClientImpl;
import org.gearman.impl.server.remote.GearmanServerRemote;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.UnsupportedEncodingException; /**
* Created by 菩提树下的杨过 on 6/12/16.
*/
public class DemoClient { public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-gearman-test.xml");
GearmanServer gearmanServer = ctx.getBean(GearmanServerRemote.class);
ClientImpl client = ctx.getBean(ClientImpl.class);
client.addServer(gearmanServer);
client.submitBackgroundJob("demoTask", "jimmy1".getBytes());//asynchronous commit GearmanJobReturn jobReturn = client.submitJob("demoTask", "jimmy2".getBytes());//synchronous commit while (!jobReturn.isEOF()) {
//next job event
GearmanJobEvent event = jobReturn.poll();
switch (event.getEventType()) {
case GEARMAN_JOB_SUCCESS: //job execute success
System.out.println(new String(event.getData(), "utf-8"));
break;
case GEARMAN_SUBMIT_FAIL: //job submit fail
case GEARMAN_JOB_FAIL: //job execute fail
System.err.println(event.getEventType() + ": "
+ new String(event.getData(), "utf-8"));
default:
}
}
client.shutdown(); } }

  

三、任务处理方(也称Worker)

import org.gearman.GearmanFunction;
import org.gearman.GearmanFunctionCallback;
import org.gearman.GearmanServer;
import org.gearman.GearmanWorker;
import org.gearman.impl.server.remote.GearmanServerRemote;
import org.gearman.impl.worker.GearmanWorkerImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class DemoWorker implements GearmanFunction { public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-gearman-test.xml");
GearmanServer gearmanServer = ctx.getBean(GearmanServerRemote.class);
GearmanWorker worker = ctx.getBean(GearmanWorkerImpl.class);
worker.addFunction("demoTask", new DemoWorker());
worker.addServer(gearmanServer);
} @Override
public byte[] work(String function, byte[] data, GearmanFunctionCallback callback) throws Exception {
if (data != null) {
String param = new String(data);
System.out.println("demoWorker => param :" + param);
return ("return value:" + param).getBytes("utf-8");
} else {
return "not receive data!".getBytes("utf-8");
}
}
}

  

上一篇:java 导出Word(POI)


下一篇:action func用法记记