[SNMP4J] Adding MOChangeListener to scalar

Eugene R. Snider gene at cvtt.net
Wed Dec 12 20:40:30 CET 2007


I was able to launch the agent in a thread from an Eclipse IDE menu 
action. I had to override the run() and stop() methods from the 
BaseAgent class. Also, in order to make the thread restartable, I had to 
add initTransportMapping into the run method since I wasn't smart enough 
to figure out how to reopen the socket.

Call SNMPAgent.getInstance().run() in the thread and call the stop 
method when the thread is cancelled or interrupted. Should work okay.

Gene

public class SNMPAgent extends BaseAgent {
    // make it a singleton; probably a matter of taste.
    private static SNMPAgent instance = null;

    private SNMPAgent() {
        super();
    }
    public static synchronized SNMPAgent getInstance() {
        if (instance == null) {
          // here is the place to be clever in invoker the super 
contructor with boot counter, config file and command processor args
            instance = new SNMPAgent();
        }
        return instance;
    }



    // blah, blah, blah

    protected void initTransportMappings() throws IOException {
        if (transportMappings == null) {
            transportMappings = new TransportMapping[1];
        }
        Address addr = GenericAddress.parse(address);
        TransportMapping tm = TransportMappings.getInstance()
                .createTransportMapping(addr);
        if( tm == null ) {
            throw new IOException("Can't initialize transport mapping 
for address "+address);
        }
        transportMappings[0] = tm;
    }

    public void run() {
        if (agentState < STATE_INIT_FINISHED) {
            local_start();
            sendColdStartNotification();
        }
        if (this.requestPool == null) {
            requestPool = ThreadPool.create("RequestPool", 4);
            // 
super.getAgent().setThreadPool(ThreadPool.create("RequestPool",
            // 4));
            super.getAgent().setThreadPool(requestPool);
        }
        initSnmpSession();
        try {
            session.listen();
        } catch (IOException iox) {
            Logger.getLogger(SNMPAgent.class).error(iox);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.snmp4j.agent.BaseAgent#sendColdStartNotification()
     */
    @Override
    protected void sendColdStartNotification() {
        // TODO Auto-generated method stub
        notificationOriginator.notify(new OctetString("public"),
                SnmpConstants.coldStart, new VariableBinding[0]);
    }

    /* (non-Javadoc)
     * @see org.snmp4j.agent.BaseAgent#stop()
     */
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        try {
            transportMappings[0].close();
            requestPool.stop();
            requestPool = null;
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            super.stop();
        }
    }

    // blah, blah, blah
}

Liat Gamliel wrote:
> Hi Frank,
>
> Thanks for your quick answer!
>
> I looked at the testAgent example and saw that the snmp agent is a
> single thread that runs all the time.
>
> So I have another question: our application is using J2EE container and
> we are using ResourceAdapter for getting 
> a new thread from the application server. Is it possible to use SNMP4j
> agent in J2EE application and call to all functions in the
> main(TestAgent.java) before
> running the thread using ResourceAdapter ?
>
> Thanks again for your help,
> Best regards,
> Liat.
>  
>
> -----Original Message-----
> From: snmp4j-bounces at agentpp.org [mailto:snmp4j-bounces at agentpp.org] On
> Behalf Of fock at agentpp.com
> Sent: Tuesday, December 11, 2007 11:44 AM
> To: snmp4j at agentpp.org; Liat Gamliel
> Subject: RE: [SNMP4J] Adding MOChangeListener to scalar
>
> Hi Liat,
>
> You can place the MOChangeListener implementation where
> you want. This is your design decision, which depends
> on the overall structure of your instrumentation.
>
> Best regards,
> Frank
>
>
> Hi,
>  
> I'm trying to build my own SNMP agent and want to add
> addMOChangeListener to my scalar.
>  
> I need a little help to understand how I can use the MOChangeListener
> and operate something according to the new value in this scalar(OID).
>  
> Where I should write the MOChangeListener handling - Is it need to be in
> the ifMib.java or in testAgent.java?
>  
> If someone has a code example, It will be very helpful to me.
>  
> Thanks,
>  
> Liat Gamliel.
>  
> e-mail: liat.gamliel at ecitele.com
>  
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
>   



More information about the SNMP4J mailing list