java gearman_Gearman使用示例

java gearman_Gearman使用示例最近的一个旧项目重构过程中 使用到了 gearman 这个开源项目 简单来讲 这是一个类似 MQ 的异步系统 一边派发任务 一边处理任务 有类似 MQ 中的消息发送方与接收方 目前支持 java php 等多种语言 缺点是存在单点问题 server 的 HA 官方没有提供方案 需要二次开发 下面是 java 语言的示例 注 gearman 的 java 客户端实例有好几个版本 不同的版本之间相差巨大 建议使用官方推荐的最新版

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

下面是java语言的示例:

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

一、spring配置

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2

3 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

4 xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

View Code

二、任务发送方(也称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”);

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211569.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:07
下一篇 2026年3月18日 下午10:08


相关推荐

  • 【LINQ语句】LINQ语句

    【LINQ语句】LINQ语句LINQ LanguageInte 是 C 和 VB NET 中的统一查询语法 用于保存和检索不同来源的数据 它集成在 C 或 VB 中 从而消除了编程语言和数据库之间的不匹配 以及为不同类型的数据源提供单个查询接口 例如 SQL 是一种结构化查询语言 用于保存和检索数据库中的数据 同样的 LINQ 是一个用 C 和 VB NET 构建的结构化查询语法 用于保存和检索

    2026年3月18日
    2
  • matlab输出语句fprintf格式,matlab输出语句fprintf

    matlab输出语句fprintf格式,matlab输出语句fprintfmatlab 中怎么输出一个变量的值 MATLAB 输出变量方法很多 主要包括以下几类 1 语句后面不加分号 这是直接输出数值的比较简单的方法 2 disp a 直接在命令窗口显示 a 变量 这种方法输出和第一种差不多 3 fprintf a f a 格式控制输出 输出 a 然后再显示输出的变量 如下图 4 save dir name 变量名 保存输出到某个文件中去 可以将数值保存

    2026年3月18日
    1
  • 读写TGA文件

    偶尔会遇到处理TGA文件的需求,封装成类以后再用到会很方便。    类的名字叫做myTGA,提供以下功能:    1:读取文件;    2:保存文件到指定目录;    3:获取图像信息(宽,高,深度/像素占用比特数,像素通道数);    4:访问像素;    5:转换到AUX_RGBImageRec 格式;    6:设计优良的结构易于扩展(目前只支

    2022年4月6日
    53
  • 图像gamma校正

    图像gamma校正                  图像gamma校正1.为什么要进行Gamma校正 (Gamma Correction,伽玛校正):所谓伽玛校正就是对图像的伽玛曲线进行编辑,以对图像进行非线性色调编辑的方法,检出图像信号中的深色部分和浅色部分,并使两者比例增大,从而提高图像对比度效果。计算机绘图领域惯以此屏幕输出电压与对应亮度的转换关系曲线,称为伽玛曲线…

    2022年6月16日
    62
  • QCustomPlot使用手册(一)

    QCustomPlot使用手册(一)QCustomPlot是一个基于Qt的画图和数据可视化C++控件。QCustomPlot致力于提供美观的界面,高质量的2D画图、图画和图表,同时为实时数据可视化应用提供良好的解决方案。

    2022年10月16日
    5
  • 如何养一只安全的“小龙虾”?

    如何养一只安全的“小龙虾”?

    2026年3月12日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号