Archived

This forum has been archived. Please start a new discussion on GitHub.

How to trap signal in ICE::Service Class

When I use ICE::Service in my program , I can't trap signal in my program.
I know if my class inherit ICE::Application, I can use :

if (interrupted())
cerr << appName() << ": terminating" << endl;
return 0;

to receive signal;

but when my Class inherit ICE::Service, I can't receive signal ,when I use kill -9 myprogram or CTL+C to stop my program , I want to use this method to stop my program and I can call some clear function to release my threads and orther resource , How can I do to set parameter or other?

Comments

  • I have implement stop , shutdown and interrupt function, but It's no response.

    class DBService : public Ice::Service
    {

    public:

    DBService();
    ~DBService();

    protected:
    virtual bool start(int, char * []);

    virtual bool stop();
    virtual bool shutdown();
    virtual void interrupt();
    private:

    static log4cxx::LoggerPtr logger;
    Ice::ObjectAdapterPtr _adapter;
    };
  • My OS is redhat linux advanced Server 3.0 and Fedora core 1
  • I have test that ICE::Application can't trap signal also;

    The following is my code :
    int pnote::DBApplication::run(int, char * [])
    {
    int isSuccess;

    isSuccess = 0;
    try
    {

    // init database
    pnote::PnoteDBPtr dbPtr = pnote::PnoteDB::newInstance();

    dbPtr->newDBEnv(communicator());
    pnote::PhoneListMapPtr phonelistMapPtr = dbPtr->getPhoneListMap();


    _adapter = communicator()->createObjectAdapter("DBDaemon");
    Ice::ObjectPtr object = new pnote::PhoneListHomeI;
    _adapter->add(object, ::Ice::stringToIdentity("phonelist"));

    //shutdownOnInterrupt();
    //holdInterrupt();
    _adapter->activate();
    communicator()->waitForShutdown();

    } catch (const Ice::Exception & e)
    {
    isSuccess = 1;
    LOG4CXX_ERROR(logger,_T(" Ice start error,error is: " << e));
    //cerr << e << endl;
    } catch (const char * msg) {
    isSuccess = 1;
    LOG4CXX_ERROR(logger,_T(" Ice start error,error is: " << msg));
    //cerr << msg << endl;
    }

    if (interrupted())
    {
    LOG4CXX_ERROR(logger,_T("Received signal,shutting down"));
    }


    return 0;
    }
  • I also use IceUtil::CtrlCHandler to trap signal,but it's also can't trap signal,
    what's wrong? the following is my code:

    void signalHook(int Signal)
    {
    cout <<"trap signale now" << endl;

    pnote::PnoteDB::newInstance()->getCommunicator()->shutdown();
    }

    int
    main(int argc, char * argv[])
    {

    try
    {
    //hook signal
    IceUtil::CtrlCHandler handle(signalHook);
    } catch (IceUtil::CtrlCHandlerException& ex)
    {
    cerr << " catch IceUtil::CtrlCHandlerException:" << ex << endl;
    }

    //boost::scoped_ptr<pnote::DBApplication> dbServicePtr(new pnote::DBApplication());
    boost::scoped_ptr<pnote::DBService> dbServicePtr(new pnote::DBService());
    int iSuccess = dbServicePtr->main(argc, argv);

    //pnote::DBApplication app;
    //int iSuccess = app.main(argc, argv);



    return iSuccess;
    }
  • bernard
    bernard Jupiter, FL
    I recommend you use the $ICE_HOME/test/IceUtil/ctrlCHandler test to check that the CtrlCHandler works properly in your environment.

    Ice::Service and Ice::Application create their own CtrlCHandler, and each process should have only one CtrlCHandler, so be careful to instantiate only one of these objects.

    Finally, double check that your CtrlCHandler object is created (or Service::run, Application::main is called) before the creation of any other thread. If you create a thread before the CtrlCHandler creation, its signals won't be masked properly ... it may receive the CTRL-C (or similar) signal and end your process immediately.

    Cheers,
    Bernard
  • Thanks ,It's okay
    I have put logger thread before than ICE::Service. Now I put it after ICE::Service ,it can trap signal now.

    Thanks very much!