[Enterprise Extensions only]
  Previous topic

Creating CORBA server main code (server.cpp), adding code to shutdown the server and release resources used

Use this task to create code for a CORBA server, to shut down the server and release the resources that it used.

This task is one step of the parent task to create the CORBA server main code, as described in Creating a CORBA server main code (server.cpp).

To create code to shut down a CORBA server, complete the following steps

  1. Edit the server source file, servantServer.cpp, and add a release_resources method, as shown in the following code extract:
    // This function releases resources used throughout the program.
    
    void release_resources( ::CORBA::BOA_ptr bp, ::CORBA::ImplementationDef_ptr 
      imp, ::CORBA::ORB_ptr op )
    {
      // Release the various resources we have allocated.
      bp->deactivate_impl( imp );
      ::CORBA::release( bp );
      ::CORBA::release( op );
      ::CORBA::release( imp );
    }

    This method is called at the end of the server's main method after other shutdown processing has been completed. (You add a call to this method in the next step.) The method takes as input pointers to the object adapter, the server's implementation repository entry, and the ORB. It deactivates the implementation repository entry then releases the resources used by the server.

  2. Edit the server source file, servantServer.cpp, and add code to the main method to respond to the server being shutdown (when the execute_request_loop is forced to return), as shown in the following code extract:
    void main( int argc, char *argv[] )
    {
    
    ...
      // Go into an infinite loop, servicing ORB requests as they are
      // received. execute_request_loop() will return when an external command,
      // WSStopServer, is executed. 
      stat = bp->execute_request_loop( ::CORBA::BOA::SOMD_WAIT );
    
      cout << "execute_request_loop has returned!" << endl;
    
      // Terminate the server.
    
      // Unbind the servant object from the object naming context.
      cout << "Unbinding the servant object" << endl;
      try
      {
        objectNameContext->unbind( *nc );
      }
      catch( ::CORBA::SystemException &ex; )
      {
        cerr << "ERROR: SystemException minor = " << ex.minor() << 
          " and id = " << ex.id();
        cerr << " was received when calling unbind()" << endl;
      }
      // Remove the logger naming context.
     try
      {
        objectNameContext->destroy();
      }
      catch( ::CosNaming::NamingContext::NotEmpty e )
      {
        cerr << "ERROR: destroy threw NotEmpty" << endl;
      }
    
      release_resources( bp, imp, op );
      cout << "Exiting servantServer..." << endl;
      cout.flush();
    }

    This code completes the following actions:

    1. Unbinds the servant object from the object naming context.
    2. Removes the object naming context.
    3. Calls a release_resources method to releases resources used by the server.

This task adds code that shutdowns a CORBA server and releases the resources that it used, when the server's execute_request_loop() is forced to return. The loop returns when a shutdown request has been made by a separate server shutdown program.

  Previous topic