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. ...
随机推荐
- SignalR系列续集[系列6:使用自己的连接ID]
目录 SignalR系列目录 前言 老规矩,前言~,在此先道个歉,之前的1-5对很多细节问题都讲的不是很详细,也有很多人在QQ或者博客问我一些问题 所以,特开了这个续集.. - -, 讲一些大家在开发 ...
- js正则表达式校验正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Linux下安装Hadoop完全分布式(Ubuntu12.10)
Hadoop的安装非常简单,可以在官网上下载到最近的几个版本,最好使用稳定版.本例在3台机器集群安装.hadoop版本如下: 工具/原料 hadoop-0.20.2.tar.gz Ubuntu12.1 ...
- 高性能 TCP/UDP/HTTP 通信框架 HP-Socket v4.1.2
HP-Socket 是一套通用的高性能 TCP/UDP/HTTP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP/HTTP 通信系统,提供 C/ ...
- linux(十三)__vsftpd服务器
rpm -qa |grep vsftpd yum search vsftpd yum install vsftpd 查看是否已经启动: service vsftpd status 启动: servic ...
- Idea SpringMVC+Spring+MyBatis+Maven调整【转】
Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...
- ES6笔记(一):ES6所改良的javascript“缺陷”
块级作用域 ES5没有块级作用域,只有全局作用域和函数作用域,由于这一点,变量的作用域甚广,所以一进入函数就要马上将它创建出来.这就造成了所谓的变量提升. ES5的"变量提升"这一 ...
- [转] IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法
IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法 源文件:http://www.cnblogs.com/yuejin/p/3385584.html ...
- no identity found Command /usr/bin/codesign failed with exit code 1 报错解决方法
stackoverflow 的解决方法是 xcode->preference->account->view detail -> refresh the provisioning ...
- ios native工程集成react-native的demo
react-native看到了给现有工程添加react-native环境的时候碰到一个问题: 如何往工程中添加 package.json文件,以及node_modules是怎么来的? 我开始的时候以为 ...