views:

34

answers:

0

I'm trying to build a service that can be start and stop automatically when start/stop jboss. The following is the service structure of my service:

public class SnmpAgent extends ServiceMBeanSupport implements SnmpAgentHandler,SnmpAgentMBean
public interface SnmpAgentMBean extends org.jboss.system.ServiceMBean

And my jboss-service.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<service>
    <mbean code="com.alcatel.mcdp.alarm.tools.SnmpAgent"
         name="alcatel.mcdp:name=SnmpAlarmAgent,service=Snmp,type=adaptor">
        <depends>alcatel.mcdp:name=McdpAlarmService</depends>
    </mbean>

</service>

I have written startService, stopService in SnmpAgent.java to override the method startService, stopService

public void startService()throws Exception{
        //System.out.println("start service");
        logger.debug("start service");
        //Configfile();
        startAgent();
        Thread.currentThread().join();
    }
    public void stopService() throws Exception{
        logger.debug("stop service");
        stopAgent();

    }

The calling method startAgent and stopAgent is:

public void startAgent() throws Exception
    {
      logger.info("startAgent");
      stopAgent();
      String portstr = ":" + port + " ....";
      //System.out.println("Starting SNMP Agent on " + InetAddress.getLocalHost() + portstr);
      SnmpPeer peer = new SnmpPeer(InetAddress.getLocalHost(), this.port);
      SnmpParameters params = peer.getParameters();
     //params.setVersion(SnmpSMI.SNMPV2);
      params.setVersion(this.snmpVersion);
      //String readcom = "public";
      //params.setReadCommunity("public");
      params.setReadCommunity(this.readCommunity);
      String writecom = "public";
      params.setWriteCommunity(writecom);
      //m_session = new SnmpAgentSession(this, peer);
      this.snmpsession = new SnmpAgentSession((SnmpAgentHandler)this, peer);
    }

    public  void stopAgent()
    {
       if (m_session != null)
         {
         ms_agent.m_session.close();
        ms_agent = null;
    }

    }

My problem is: startService really works when jboss start. And snmp agent will listen on the port. But stopService doesn't works and I can't see any log. Can any body help me with this? I can't find the problem.

Problem solved: Thanks to skaffman's help, the problem is solved. Thanks to Andreas too. The correction codes is Edited above