3 Replies Latest reply on Oct 5, 2011 3:21 AM by nami

    snmp4j trap receiver api processPdu cannot access satabase

    frankseam
      class defined as bellow.
      I have tested that the function processPdu can be called when a trap is received ,but hibernate connection is failed when excuting "entityManager.persist(alarm)" with no prompt, because there is no data insert into database.
      I don't konw why? Can anyone help me please?

      ###############################################################
      package com.stan.eocnms.action;

      import java.util.Date;
      import com.stan.eocnms.model.Alarm;
      import java.io.IOException;
      import java.net.UnknownHostException;
      import java.util.Vector;
      import javax.persistence.EntityManager;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.AutoCreate;
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.Startup;
      import org.jboss.seam.annotations.Transactional;
      import org.jboss.seam.annotations.async.Asynchronous;
      import org.jboss.seam.async.QuartzTriggerHandle;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.async.Expiration;
      import org.jboss.seam.annotations.async.IntervalDuration;
      import org.jboss.seam.log.Log;

      import org.snmp4j.CommandResponder;
      import org.snmp4j.CommandResponderEvent;
      import org.snmp4j.MessageDispatcherImpl;
      import org.snmp4j.Snmp;
      import org.snmp4j.TransportMapping;
      import org.snmp4j.mp.MPv1;
      import org.snmp4j.mp.MPv2c;
      import org.snmp4j.mp.MPv3;
      import org.snmp4j.security.SecurityModels;
      import org.snmp4j.security.SecurityProtocols;
      import org.snmp4j.security.USM;
      import org.snmp4j.smi.Address;
      import org.snmp4j.smi.GenericAddress;
      import org.snmp4j.smi.OctetString;
      import org.snmp4j.smi.TcpAddress;
      import org.snmp4j.smi.UdpAddress;
      import org.snmp4j.smi.VariableBinding;
      import org.snmp4j.transport.DefaultTcpTransportMapping;
      import org.snmp4j.transport.DefaultUdpTransportMapping;
      import org.snmp4j.util.MultiThreadedMessageDispatcher;
      import org.snmp4j.util.ThreadPool;

      @Name("trapdemo")
      @Scope(ScopeType.APPLICATION)
      @AutoCreate
      public class TestTrapProcessor  implements CommandResponder
      {
          private MultiThreadedMessageDispatcher dispatcher;
          private Snmp snmp = null;
          private Address listenAddress;
          private ThreadPool threadPool;

          private TestTrapProcessor testTrapProcessor = null;
          /*
          @In(create=true)
          static MultiThreadedTrapReceiver multithreadedtrapreceiver;
          */

          @In EntityManager entityManager;   
          @Logger Log log;

          public void MultiThreadedTrapReceiver()
          {
              testTrapProcessor = this;
          }

          private void init() throws UnknownHostException, IOException
          {
              threadPool = ThreadPool.create("Trap", 2);
              dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
              listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", "udp:0.0.0.0/162"));
              TransportMapping transport;

              if (listenAddress instanceof UdpAddress)
              {
                  transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
              }
              else
              {
                  transport = new DefaultTcpTransportMapping((TcpAddress) listenAddress);
              }
              snmp = new Snmp(dispatcher, transport);
              snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
              snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
              snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
              USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
              SecurityModels.getInstance().addSecurityModel(usm);
              snmp.listen();
          }

          public void run()
          {
              try
              {
                  log.info("@@@@__testTrapProcessor->run");
                  init();
                  snmp.addCommandResponder(this);
              }
              catch (Exception ex)
              {
                  ex.printStackTrace();
              }
          }

          public synchronized void processPdu(CommandResponderEvent respEvnt)
          {
              if (respEvnt != null && respEvnt.getPDU() != null)
              {
                  Vector<VariableBinding> recVBs = respEvnt.getPDU().getVariableBindings();
                  for (int i = 0; i < recVBs.size(); i++)
                  {
                      VariableBinding recVB = recVBs.elementAt(i);
                      System.out.println(recVB.getOid() + " : " + recVB.getVariable());
                  }

                  System.out.println("@@@@__testTrapProcessor->processPdu");
                  Alarm alarm = new Alarm();
                  alarm.setDiscoveryDate(new Date());
                  alarm.setDiscoveryEndDate(new Date());
                  alarm.setIp("192.168.1.31");
                  alarm.setLabel("test insert alarm to db");
                  alarm.setMac("ff:ff:ff:ff:ff:ff");
                  alarm.setCreatedDate(new Date());
                  alarm.setActive(true);
                  entityManager.persist(alarm);
              }
          }

          @Asynchronous
          @Transactional
          public QuartzTriggerHandle scheduleTestTrapDemo(@Expiration Date when,
                  @IntervalDuration Long interval)
          {
                  // TODO Auto-generated method stub
                  System.out.println("@@@@__testTrapProcessor->scheduleTestTrapDemo");

                  this.run();
                  return null;
          }
      }
        • 1. Re: snmp4j trap receiver api processPdu cannot access satabase
          frankseam

          It seams like a life circle problem. The hibenate operation entityManager.persist(alarm) will not be execude immediately until TestTrapProcessor terminated. But it is a dead while actually, because this trap receiver is a background services, and function processPdu is called when a trap captured.
          But I don't know if hibenate can be executed immediately in function processPdu ?
          help!!!

          • 2. Re: snmp4j trap receiver api processPdu cannot access satabase
            frankseam

            I find a post in internet today. It is seams that the same problem  of JBoss somebody encountered.
            www.coderanch.com/t/90633/JBoss/EntityManager-flush-not-flushing
            JBoss is not storing the record in DB until the method completes, even though function entityManager.flush is called.

            • 3. Re: snmp4j trap receiver api processPdu cannot access satabase
              nami

              Hi, I am new to snmp. I am using the same init() method as shown above in macode. I want to support for 500 traps/second. But if run the code as stand alone apllication sometimes i will be able to receive all 500 and other times i am not able to receive 500 traps.


              What might be the reason for the cause and what can be done so that it would support 500 traps.


              Any suggestions or solutions or code snippets would be of great help.
              Thanks in Advance