您的位置:首页 > 编程语言 > C语言/C++

windows平台下Gearman的一个Worker和Client实例,C++实现

2015-07-12 23:43 609 查看
由于最近工作需要,要涉及Gearman的Client和worker的开发工作,所以研究了一下环境搭建和Client、Worker的开发。

环境搭建工作就不多说了,网上资料很多,不管是部署在linux上(常规方式)还是通过cygwin方式部署到windows上,都是可行的。

linux方式部署安装步骤大家可以参考这个文章:
http://blog.chinaunix.net/uid-27177626-id-4325909.html
cygwin部署到windows上可是可行的,又需要可以参考这篇文章:
http://www.cnblogs.com/zhujinghui/p/4175650.html
下面切入正题:

在Gearman部署完成后,我突然发现,Gearman只有C驱动而且是linux下的。换句话说如果直接在windows下搞个C的Gearman Worker是不可行的。但是幸好windows下有C#驱动,因此对于不熟悉linux开发的C++程序员,或者当前项目主要代码都在win下,linux不方便复用,需要工作量的朋友,可以直接借鉴下面代码。

Gearman C#驱动下载地址,驱动源码和示例都有,需要dll文件的可以自己编译:http://download.csdn.net/detail/huozhouhftze/8891971

下面Worker是通过托管C++来调用Gearman C#驱动实现功能的。代码是C#驱动的一个Demo,我删除掉了一个异步处理反转字符串演示接口,将C#语法改成了托管C++代码,并且已经和C#的Client Demo程序联调通过测试。

#include "stdafx.h"

using namespace System;

#using "..\\Debug\\Newtonsoft.Json.dll"
#using "..\\Debug\\Twingly.Gearman.dll"

class Worker
{
public:

static void DoWork()
{
try
{
Twingly::Gearman::GearmanThreadedWorker^ worker = gcnew Twingly::Gearman::GearmanThreadedWorker();
String^ host = "20.20.20.131";
worker->AddServer(host, 4730);
worker->SetClientId("my-threaded-worker");
worker->RegisterFunction<String^, String^>("reverse",
gcnew Twingly::Gearman::GearmanJobFunction<String^, String^>(&Worker::DoReverse),
gcnew Twingly::Gearman::DataDeserializer<String^>(&Twingly::Gearman::Serializers::UTF8StringDeserialize),
gcnew Twingly::Gearman::DataSerializer<String^>(&Twingly::Gearman::Serializers::UTF8StringSerialize));

Console::WriteLine("Press enter to start work loop, and press enter again to stop");
Console::ReadLine();

worker->StartWorkLoop();

Console::ReadLine();

worker->StopWorkLoop();

Console::WriteLine("Press enter to quit");
Console::ReadLine();
}
catch (System::Exception^ ex)
{
Console::WriteLine("Got exception: {0}", ex);
return;
}
}

static void DoReverse(Twingly::Gearman::IGearmanJob<String^, String^>^ job)
{
Console::WriteLine("Got job with handle: {0}, function: {1}", job->Info->JobHandle, job->Info->FunctionName);

String^ str = job->FunctionArgument;

array<wchar_t>^ strArray = str->ToCharArray();
Array::Reverse(strArray);
String^ reversedStr = gcnew String(strArray);

Console::WriteLine("  Reversed: {0}", reversedStr);

job->Complete(reversedStr);
}
};

int main(array<System::String ^> ^args)
{
Worker::DoWork();
return 0;
}


上面Worker工程文件下载:http://download.csdn.net/detail/huozhouhftze/8893289 其中包含的dll均为debug版,需要release版的可以取上面的C#源代码自己编译。

顺便附上一个Client实例吧,自己用着方便

#include "stdafx.h"

using namespace System;

#using "..\\Debug\\Newtonsoft.Json.dll"
#using "..\\Debug\\Twingly.Gearman.dll"

using namespace Twingly::Gearman;

class Client
{
public:

static void DoClient()
{
GearmanClient^ client = gcnew GearmanClient();
String^ host = "20.20.20.131";
client->AddServer(host, 4730);

CreateJobs(client, 1);

Threading::Thread::Sleep(6000);
}

static void CreateJobs(GearmanClient^ client, int jobCount)
{
for (int i = 0; i < jobCount; i++)
{
try
{
String^ result = client->SubmitJob<String^, String^>("reverse", String::Format("{0}: Hello World", i),
gcnew DataSerializer<String^>(&Serializers::UTF8StringSerialize),
gcnew DataDeserializer<String^>(&Serializers::UTF8StringDeserialize));
Console::WriteLine("Job result: {0}", result);
}
catch (Exceptions::NoServerAvailableException^ ex)
{
Console::WriteLine("Got exception: {0}", ex->Message);
}
}
}
};

int main(array<System::String ^> ^args)
{
Client::DoClient();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: