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

    snmp4j trap receiver api processPdu cannot access satabase

    frank frank Newbie
      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;
          }
      }