Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6702865
  • 博文数量: 702
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13243
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
文章分类

全部博文(702)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(179)

2014年(129)

2013年(123)

2012年(61)

分类: 云计算

2017-12-05 19:46:24

官方的DEMO如下:
http://gearman.info/libgearman/examples.html

闲言少述,直接上源码:
client

  1. /*
  2.  * File:
  3.  *    simple-gearman-client.cpp
  4.  * Auth:
  5.  * Hank(hongkuiyan@yeah.net)
  6.  * Desc:
  7.  * Example code to show how to send a string to a function called "test_function" .
  8.  * Cypt:
  9.  * Gearman server and library Version 1.1.12
  10.  * Compile:
  11.  * gcc -o simple-gearman-client simple-gearman-client.cpp -I/usr/local/gearman/include -L/usr/local/gearman/lib -lgearman
  12.  * Usage:
  13.  * ./simple-gearman-client
  14.  */

  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <libgearman/gearman.h>

  20. using namespace std;

  21. int main(int argc, char* argv[])
  22. {
  23.     gearman_return_t gearRet;
  24.     gearman_client_st* gearClient;
  25.     
  26.     char* gearSvrHost=(char*)"10.10.13.8", *gearSvrPort=(char*)"4730";

  27.     gearClient = gearman_client_create(NULL);
  28.     gearman_client_set_options(gearClient, GEARMAN_CLIENT_FREE_TASKS);
  29.     gearman_client_set_timeout(gearClient, 15000);

  30.     gearRet = gearman_client_add_server(gearClient, gearSvrHost, atoi(gearSvrPort));
  31.     if (gearman_failed(gearRet))
  32.     {
  33.         return EXIT_FAILURE;
  34.     }

  35. #if 1
  36.     
  37.     for (int i=0; i < 5; i++)
  38.     {
  39.         gearRet = gearman_client_do_background(gearClient,
  40.                     "test_function",
  41.                     NULL,
  42.                     "param1 param2 345",
  43.                     (size_t)strlen("param1 param2 345"),
  44.                     NULL);
  45.         if (gearRet == GEARMAN_SUCCESS)
  46.         {
  47.             fprintf(stdout, "Work success!\n");
  48.         }
  49.         else if (gearRet == GEARMAN_WORK_FAIL)
  50.         {
  51.             fprintf(stderr, "Work failed\n");
  52.         }
  53.         else if (gearRet == GEARMAN_TIMEOUT)
  54.         {
  55.             fprintf(stderr, "Work timeout\n");
  56.         }
  57.         else
  58.         {
  59.             fprintf(stderr, "%d,%s\n", gearman_client_errno(gearClient), gearman_client_error(gearClient));
  60.         }
  61.     }

  62. #endif


  63. #if 0    
  64.     gearman_argument_t gearValue = gearman_argument_make(0, 0,
  65.         "Reverse Me", strlen("Reverse Me"));
  66.     gearman_task_st* gearTask = gearman_execute(gearClient,
  67.         "reverse", strlen("reverse"),
  68.         NULL, 0,
  69.         NULL,
  70.         &gearValue, 0);
  71.     
  72.     /* If gearman_execute() can return NULL on error */
  73.     if (gearTask == NULL)
  74.     {
  75.         fprintf(stderr, "Error: %s\n", gearman_client_error(gearClient));
  76.         gearman_client_free(gearClient);
  77.         return EXIT_FAILURE;
  78.     }

  79.     /* Make sure the task was run successfully */
  80.     if (gearman_success(gearman_task_return(gearTask)))
  81.     {
  82.         /* Make use of value */
  83.         gearman_result_st *gearResult= gearman_task_result(gearTask);
  84.         printf("%.*s\n", (int)gearman_result_size(gearResult), gearman_result_value(gearResult));
  85.     }
  86. #endif

  87.     gearman_client_free(gearClient);

  88.     return EXIT_SUCCESS;
  89. }


worker

  1. /*
  2.  * File:
  3.  *    simple-gearman-worker.cpp
  4.  * Auth:
  5.  * Hank(hongkuiyan@yeah.net)
  6.  * Desc:
  7.  * Example code to show how to receive a string to a function called "test_function"
  8.  * Cypt:
  9.  * Gearman server and library Version 1.1.12
  10.  * Compile:
  11.  * gcc -o simple-gearman-worker simple-gearman-worker.cpp -I/usr/local/gearman/include -L/usr/local/gearman/lib -lgearman
  12.  * Usage:
  13.  * ./simple-gearman-worker
  14.  */
  15.  
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <iostream>

  22. #include <libgearman/gearman.h>
  23.  
  24. using namespace std;

  25. static void *test_function(gearman_job_st *gearJob,
  26.     void* context,
  27.     size_t* resultSize,
  28.     gearman_return_t* gearRet);


  29. int main(int argc, char* argv[])
  30. {
  31.     gearman_return_t gearRet;
  32.     gearman_worker_st* gearWorker;

  33.     char* gearSvrHost=(char*)"10.10.13.8", *gearSvrPort=(char*)"4730";
  34.     char* gearContext = new char(50);

  35.     gearWorker = gearman_worker_create(NULL);
  36.     if (gearWorker == NULL)
  37.     {
  38.         cout << "ERROR: " << gearman_worker_error(gearWorker) << endl;    
  39.         return EXIT_FAILURE;    
  40.     }    

  41.     gearRet = gearman_worker_add_server(gearWorker, gearSvrHost, atoi(gearSvrPort));
  42.     if (gearman_failed(gearRet))
  43.     {
  44.         cout << "ERROR: " << gearman_worker_error(gearWorker) << endl;    
  45.         return EXIT_FAILURE;
  46.     }

  47.     gearRet = gearman_worker_add_function(gearWorker,
  48.         "test_function",
  49.         0,
  50.         test_function,
  51.         gearContext);
  52.     if (gearman_failed(gearRet))
  53.     {
  54.         cout << "ERROR: " << gearman_worker_error(gearWorker) << endl;    
  55.         return EXIT_FAILURE;
  56.     }

  57.     cout << "waiting for job ... " << endl;

  58.     while (1)
  59.     {
  60.         gearRet = gearman_worker_work(gearWorker);
  61.         if (gearman_failed(gearRet))
  62.         {
  63.             cout << "ERROR: " << gearman_worker_error(gearWorker) << endl;
  64.             break;
  65.         }
  66.     }

  67.     delete gearContext;
  68.     gearman_worker_free(gearWorker);

  69.     return EXIT_SUCCESS;

  70. }

  71. static void *test_function(gearman_job_st *gearJob,
  72.     void *context,
  73.     size_t *resultSize,
  74.     gearman_return_t *gearRet)
  75. {
  76.     cout << "\n\njob :" << gearman_job_handle(gearJob) << endl;
  77.     cout << "context :" << (char*)context << endl;

  78.     char *jobWorkload = (char*)gearman_job_workload(gearJob);
  79.     size_t jobWorkloadSize = gearman_job_workload_size(gearJob);


  80.     char *jobRequest = new char[1024];
  81.     jobRequest = (char*)malloc(1024);
  82.     if (jobRequest == NULL)
  83.     {
  84.         cout << "new jobRequest failed" << errno << endl;
  85.         *gearRet = GEARMAN_WORK_FAIL;
  86.         return NULL;
  87.     }

  88.     snprintf((char*)jobRequest, jobWorkloadSize+1, (char*)jobWorkload);
  89.     cout<<"job = "<< gearman_job_handle(gearJob) << " jobWorkloadSize=" << jobWorkloadSize << " jobRequest=" <<jobRequest << endl;

  90.     char *jobResult = new char[1024];
  91.     if (jobResult == NULL)
  92.     {
  93.         cout <<"new jobResult failed" << errno << endl;
  94.         *gearRet = GEARMAN_WORK_FAIL;
  95.         return NULL;
  96.     }


  97.     sprintf((char*)jobResult, "%s->jobResult", (char*)jobRequest);
  98.     size_t jobResultSize = strlen((char*)jobResult);
  99.     cout <<"job = "<< gearman_job_handle(gearJob) << " jobResultSize=" << jobResultSize << " jobResult=" << jobResult << endl;


  100.     delete []jobResult;
  101.     delete []jobRequest;

  102.     *gearRet = GEARMAN_SUCCESS;
  103.     return NULL;
  104. }

阅读(637) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册