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必知必会--Tips
01 - 如何获取SQL命令帮助信息 官方手册 help 或 help command MariaDB [(none)]> help General information about Mari ...
- 嵌入式开发中常见3个的C语言技巧
Hey,大家好!我是CrazyCatJack.今天我来说几个在嵌入式开发中常用的C语言技巧吧.也许你曾经用过,也许你只是见到过但是没有深入理解.那么今天好好补充下吧^_^ 1.指向函数的指针 指针不光 ...
- Interview website
https://www.interviewcake.com http://www.leetcode.com
- Cocoapods无法使用/安装失败/失效解决方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #666666 } sp ...
- PHP中this,self,parent三个关键字
this,self,parent三个关键字从字面上比较好理解,分别是指这.自己.父亲. this是指向当前对象的指针(姑且用C里面的指针来看吧)self是指向当前类的指针parent是指向父类的指针( ...
- LinQ to SQL用法详解
LinQ是指集成化查询语言,通过映射将数据库内的表名变为C#的类名,将列名作为属性名,将表的关系作为类的成员对象.O--M--R O-Object对象(李昌辉)R-Relation关系M-Mappin ...
- .Net导出pdf文件,C#实现pdf导出
最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本, ...
- Linux命令-文件文本操作grep
文件文本操作 grep 在文件中查找符合正则表达式条件的文本行 cut 截取文件中的特定字段 paste 附加字段 tr 字符转换或压缩 sort 调整文本行的顺序,使其符合特定准则 uniq 找出重 ...
- Scala override
var 变量不能在子类中重写,除非父类是抽象类 在抽象类中var变量不能赋初值 abstract class Person{ val name="" def name1=" ...
- 做一个 App 前需要考虑的几件事
做一个 App 前需要考虑的几件事 来源:limboy的博客 随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...