A small example of ACE service configurator

Source: Internet
Author: User
Tags signal handler

After a long time, I finally saw the attention picture presented by the ACE service configurator framework in Linux. This framework is truly shocking. This example replaces the interface functions used by the service without shutting down the service, and the service provider constantly reads SVC in the timer. conf configuration file content, and then reconfig service. It is not a clever way to do this in the timer, but it vividly shows the service
The configurator can be reconfigured in this way. A more reasonable way to reconfigure services should be based on signals. If you want to reconfigure the service, send a signal to the service program so that it can be reconfigured according to SVC. conf. The following is a complete production process for this small example.
First, we need to generate two DLL, each of which declares an ace_service_object class and implements the inherited init/suspend/resume/fini interfaces, then, modify the DLL used by the Service through the configuration file.
1. Create a DLL
Because the DLL is used only when the program is running, and the Linux dynamic library does not need to be imported into the database, I put the class declaration and implementation in a CPP file. Many documents about service aggregator will mention the use of ace_wrappers/bin/generate_export_file.pl to generate a header file. I did the same at the beginning and found that this header file has nothing to do with it, it is not included. This file may be useful in windows.

The implementation files of the two DLL files are as follows:

//// // My_dll.cpp // # include "ACE/service_object.h" # include "stdio. H "class mydynamicobj: Public ace_service_object {public: mydynamicobj () {}; virtual ~ Mydynamicobj () {}; virtual int Init (INT argc, ace_tchar * argv []) {printf ("mydynamicobj: init ------ \ n"); Return 0 ;} virtual int suspend () {printf ("mydynamicobj: suspend ----- \ n"); Return 0 ;}virtual int resume () {printf ("mydynamicobj :: resume ----- \ n "); Return 0;} virtual int Fini () {printf (" mydynamicobj: fini ----- \ n "); Return 0 ;}; mydynamicobj OBJ; // here you can also provide a static function to generate an instance of mydynamicobj // you need to use the ace _ Factory_define this macro // ya_my_dll.cpp // # include" ACE/service_object.h "# include" stdio. H "class mydynamicobj: Public ace_service_object {public: mydynamicobj () {}; virtual ~ Mydynamicobj () {}; virtual int Init (INT argc, ace_tchar * argv []) {printf ("yet another mydynamicobj: init ------ \ n"); Return 0 ;} virtual int suspend () {printf ("yet another mydynamicobj: suspend ----- \ n"); Return 0 ;}virtual int resume () {printf ("yet another mydynamicobj :: resume ----- \ n "); Return 0;} virtual int Fini () {printf (" yet another mydynamicobj: fini ----- \ n "); Return 0 ;}}; mydynamicobj OBJ;

Compile and generate two DLL files as follows:
G ++-shared-O libmydll. So my_dll.cpp-libraries
G ++-shared-O libyamydll. So ya_my_dll.cpp-Example

2. Compile the Service Startup Function

//// // Test_dynamic_config.cpp // # include "ace /OS. H "# include" ACE/service_config.h "# include" ACE/event_handler.h "# include" ACE/reactor. H "class time_handler: Public ace_event_handler {public: // method which is called back by the reactor when timeout occurs. virtual int handle_timeout (const ace_time_value & TV, const void * Arg) {// The timer needs to be reset. Otherwise, it will be deleted once it is run! // Stupid! Schedule_timer the third parameter is delay, and the fourth parameter is the scheduled interval // If the fourth parameter is not specified, the default value of the interval is 0, in this way, the timer can only play a role once and then it will be deleted, so here we need to constantly reset ace_reactor: instance ()-> schedule_timer (this, null, ace_time_value (2); ace_debug (lm_info, "regular Timeout !!! \ N "); ace_service_config: reconfigure (); Return 0 ;}}; int main (INT argc, char * argv []) {// instantiate and start up the signal handler. this is uses to // handle re-configuration events. time_handler th; If (ace_service_config: open (argc, argv) =-1) ace_error_return (lm_error, "% P \ n", "ace_service_config: Open "), -1); ace_reactor: instance ()-> schedule_timer (& th, null, ace_time_value (2); // set timer to go off with delay while (1) ace_reactor:: instance ()-> handle_events ();}


Generate a test program:
G ++-O test_dynamic_config test_dynamic_config.cpp-Example
It can be seen that the specific dynamic link library used by the Service and the Service has nothing to do with it.

3. Write SVC. conf
Write the following content to SVC. conf:
Dynamic mydynamicobj service_object * libmydll. So: OBJ ""

Suspend mydynamicobj

Resume mydynamicobj

Remove mydynamicobj

Run test_dynamic_config and the output is as follows:
Mydynamicobj: init ------
Mydynamicobj: suspend -----
Mydynamicobj: Resume -----
Mydynamicobj: fini -----

Change the content of SVC. conf:
Dynamic mydynamicobj service_object * libyamydll. So: OBJ ""

Suspend mydynamicobj

Resume mydynamicobj

Remove mydynamicobj

The output is as follows:
Yet another mydynamicobj: init ------
Yet another mydynamicobj: suspend -----
Yet another mydynamicobj: Resume -----
Yet another mydynamicobj: fini -----

In this way, the dynamic link library used is replaced dynamically.

Tip: http://blog.163.com/ecy_fu/blog/static/44451262009570274905/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.