OverviewThe Processing Unit example is located at <GigaSpaces Root>\cpp\examples\CppService and <GigaSpaces Root>\cpp\examples\CppServiceOpenSpaces.
C++ Processing Unit ImplementationImplementing CppWorkerTo create your own CppWorker you should inherit your class from ICppWorker and implement the following methods:
The rest of the interface is for deploying the service and we just need to change the name of the service to our new name. #include <windows.h> #endif class CppService : public ICppWorker { public: CppService(): m_callback(0),m_isRunning(true){} virtual ~CppService() {} virtual const char* className() { return "CppService"; } virtual bool Initialize(IWorkerPeer* Host); virtual CommandObjectPtr run(CommandObjectPtr); virtual bool Destroy(); private: IWorkerPeer* m_callback; bool m_isRunning; }; bool CppService::Initialize(IWorkerPeer* Host) { m_callback = Host; return true; } CommandObjectPtr CppService::run(CommandObjectPtr Object) { genericVector replyParams = Object->getParameters(); SpaceProxyPtr proxy; SpaceFinder finder; std::cout<< "----The C++ service CppService STARTED -----" << std::endl; long long proxyId = any_cast<long long>(replyParams[0]); std::cout<< "----The ID: " << proxyId << std::endl; try { proxy = finder.attach( proxyId, false); std::cout<< "----Find Proxy ------ " << std::endl; } catch(FinderException& ) { std::cout << "Caught FinderException" << std::endl; } catch(XAPException &) { std::cout << "Caught generic XAP Exception" << std::endl; } /************************************************************/ /* */ /* At this point CPP is alive and running and one may */ /* use any of the proxy API in order to interact with */ /* the space and perform calculations or general */ /* business logic. */ /* */ /************************************************************/ try { Message messageTemplate; proxy->snapshot(&messageTemplate); std::cout<< "-- Snapshot Done" << std::endl; while(m_isRunning) { std::cout << "*****Worker Running***** " << std::endl; MessagePtr msg( new Message() ); msg->content = "Hello World"; msg->id = 1; // Fill index std::cout<< "-- Before Write Message Done" << std::endl; proxy->write(msg.get(), NULL_TX, Lease::FOREVER); std::cout<< "-- Write Message Done" << std::endl; MessagePtr result ( proxy->read(&messageTemplate, NULL_TX, 0) ); std::cout<< "-- Read Message Done" << std::endl; proxy->take(&messageTemplate, NULL_TX, 0); std::cout<< "-- Take Message Done" << std::endl; ACE_OS::sleep(1); } } catch(XAPException &e) { e.toLogger(); } genericVector params; CommandObjectPtr ptr( new CommandObject("Results",params)); return ptr; } /****************************************************************/ /* Called upon Destruction of the Space */ /****************************************************************/ bool CppService::Destroy() { m_isRunning = false; std::cout<< "---- Destroy -----" << std::endl; return true; } #if defined(_WIN32) #define EXPORT __declspec(dllexport) #else #define EXPORT #endif extern "C" EXPORT ICppWorker* getWorkerByClass(const char* className) { std::cout<< "---- className: " << className << std::endl; return (0 == strcmp(className, "CppService")) ? new CppService : 0; } Processing Unit Configuration FileThe Processing Unit configuration file (<GigaSpaces Root>\cpp\examples\CppServiceOpenSpaces\src\META-INF\spring\pu.xml) includes the following: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os-core="http://www.openspaces.org/schema/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.openspaces.org/schema/core http://www.openspaces.org/schema/core/openspaces-core.xsd"> <!-- Spring property configurer which allows us to use system properties (such as user.name). --> <bean id="propertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <!-- A bean representing a space (an IJSpace implementation). --> <os-core:space id="space" url="/./space" /> <!-- OpenSpaces simplified space API built on top of IJSpace/JavaSpace. --> <os-core:giga-space id="gigaSpace" space="space" /> <!-- The cpp worker example. For creating cpp worker we always use the CXXBean. We define the cpp worker we want to run by setting the workerName property. --> <bean id="cpp" class="com.gigaspaces.javacpp.openspaces.CXXBean"> <property name="gigaSpace" ref="gigaSpace" /> <property name="workerName" value="CppService" /> </bean> </beans> Building and Running the ExampleBuilding the Example on WindowsTo open the example use the example.sln located in <GigaSpaces Root>\cpp\examples. Select the Release configuration and rebuild the solution. Running the Example
The configuration files are located in <GigaSpaces Root>\cpp\examples\CppServiceOpenSpaces directory. build deploy-local-cppexample The following output will be displayed: D:\GigaSpacesXAP6.5M3\cpp\examples\CppServiceOpenSpaces>build.bat deploy-local-cppexample JAVA_HOME environment variable is set to C:\Java\jdk1.6.0_02 in "<GigaSpaces Root>\bin\setenv.bat" Environment set successfully from D:\GigaSpacesXAP6.5M3 Buildfile: build.xml build: dist: copy-local-cppexample: [delete] Deleting directory D:\GigaSpacesXAP6.5M3\deploy\cppexample [mkdir] Created dir: D:\GigaSpacesXAP6.5M3\deploy\cppexample [copy] Copying 2 files to D:\GigaSpacesXAP6.5M3\deploy\cppexample deploy-local-cppexample: [java] Feb 4, 2008 2:50:37 AM org.openspaces.pu.container.servicegrid.deploy.Deploy buildOperationalString [java] INFO: Deploying [cppexample] with name [cppexample] and groups [gigaspaces-6.5XAP] [java] Feb 4, 2008 2:50:38 AM org.openspaces.pu.container.servicegrid.deploy.ServiceFinder find [java] INFO: Searching for com.gigaspaces.grid.gsm.GSM in groups [gigaspaces-6.5XAP] [java] Feb 4, 2008 2:50:43 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions [java] INFO: Loading XML bean definitions from resource loaded from byte array [java] Feb 4, 2008 2:51:00 AM org.openspaces.pu.container.servicegrid.deploy.Deploy buildOperationalString [java] INFO: SLA Not Found in PU. Using Default SLA. BUILD SUCCESSFUL Total time: 27 seconds The gsc console will have the following output: ---- className: CppService
Loaded D:\GigaSpacesXAP6.5M3\lib\ServiceGrid\native\CppService.dll
----The C++ service CppService STARTED -----
----The ID: 0
----Find Proxy ------
-- Snapshot Done
*****Worker Running*****
-- Before Write Message Done
-- Write Message Done
-- Read Message Done
-- Take Message Done
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |