Gearman使用示例
最近的一个旧项目重构过程中,使用到了gearman这个开源项目,简单来讲,这是一个类似MQ的异步系统,一边派发任务,一边处理任务(有类似MQ中的消息发送方与接收方),目前支持java,php等多种语言,缺点是存在单点问题(server的HA官方没有提供方案,需要二次开发)。
下面是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");
}
}
}
Gearman使用示例的更多相关文章
- 分布式的任务分发框架-Gearman
官方文档:http://gearman.org/getting-started/ 安装方法和示例都有,可以详细看一下. Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相比,G ...
- gearman安装及初次使用
官网: http://gearman.org/ 一篇文章: 利用Gearman实现异步任务处理 一.问题分析 问题:在性能测试过程中,发现用户管理平台在进行图片上传时,性能不佳. 分析:经过代码分析 ...
- 用 Gearman 分发 PHP 应用程序的工作负载
尽管一个 Web 应用程序的大部分内容都与表示有关,但它的价值与竞争优势却可能体现在若干专有服务或算法方面.如果这类处理过于复杂或拖沓,最好是进行异步执行,以免 Web 服务器对传入的请求没有响应.实 ...
- 分布式任务系统gearman的python实战
Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在调用其它语言的函数的系统.Gearman是一个分发任务的程序框架,可以 ...
- 分布式计算框架Gearman原理详解
什么是Gearman? Gearman提供了一个通用的应用程序框架,用于将工作转移到更适合于工作的其他机器或流程.它允许你并行工作,负载平衡处理,并在语言间调用函数.它可用于从高可用性网站到传输数据库 ...
- gearman(异步计算)学习
Gearman是什么? 它是分布式的程序调用框架,可完成跨语言的相互调 用,适合在后台运行工作任务.最初是2005年perl版本,2008年发布C/C++版本.目前大部分源码都是(Gearmand服务 ...
- gearman入门初步
原文地址:http://blog.sina.com.cn/s/blog_5f54f0be0101btsi.html PHP 没有提供直接的并发功能.要实现并发,必须: function asy ...
- gearman知识文章
一篇文章: Gearman介绍.调研.测试与原理分析 gearman是什么? 它是分布式的程序调用框架,可完成跨语言的相互调用,适合在后台运行工作任务.最初是2005年perl版本,2008年发布C/ ...
- php异步任务处理: gearman
Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡 准备软件包 gearmand-1.1.12.tar.gz gearman-1. ...
随机推荐
- 你真的会玩SQL吗?让人晕头转向的三值逻辑
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- MyCat源码分析系列之——BufferPool与缓存机制
更多MyCat源码分析,请戳MyCat源码分析系列 BufferPool MyCat的缓冲区采用的是java.nio.ByteBuffer,由BufferPool类统一管理,相关的设置在SystemC ...
- 完美解决,浏览器下拉显示网址问题 | 完美解决,使用原生 scroll 写下拉刷新
在 web 开发过程中我们经常遇到,不想让用户下拉看到我的地址,也有时候在 div 中没有惯性滚动,就此也出了 iScroll 这种关于滚动条的框架,但是就为了一个体验去使用一个框架好像又不值得,今天 ...
- express路由探析(续)
上一篇分析了express的路由机制,这次主要补充一些没有说到的东西. 之前说到,Router是中间件容器,Route是路由中间件,他们各自维护一个stack数组,里面存放layer,layer是封装 ...
- BaaS API 设计规范
上个月写了一个团队中的 BaaS API 的设计规范,给大家分享下: 目录 1. 引言... 4 1.1. 概要... 4 1.2. 参考资料... 4 1.3. 阅读对象... 4 1.4. 术语解 ...
- Node学习笔记(三):基于socket.io web版你画我猜(二)
上一篇基础实现的功能是客户端canvas作图,导出dataURL从而实现图片信息推送,下面具体讲下服务端的配置及客户端的配置同步 首先先画一个流程图,讲下大概思路 <canvas id=&quo ...
- Xamarin for Visual Studio V3.11.431 于 2015.4.3-2015.4.17 最新发布(Win & Mac)
Beta Release: April 3 edited April 17 in Visual Studio Released versions: Windows Xamarin.VisualStud ...
- ThreadPool.QueueUserWorkItem的用法
代码: ThreadPool.SetMaxThreads(, ); ThreadPool.QueueUserWorkItem((obj) => { MessageBox.Show("执 ...
- C# 复制指定节点的所有子孙节点到新建的节点下
XML结构: 新建一个mask_list节点,一个procedure节点,将上面的mask_list和procedure节点的所有子孙节点添加到新建的mask_list和procedure节点 Xml ...
- 智软科技医疗器械GSP监管软件通过多省市药监局检查
提供医疗器械GSP监管软件,通过多省市药监局检查,符合2016年最新GSP监管条例的要求. 企业客户列表 温岭市万悦医疗器械有限公司 杭州市上善医疗器械有限公司 武汉明德生物科技股份有限公司 http ...