2016-11-29 5 views
0

Ich bin neu bei Apache Camel und versuche, eine einfache SNMP-Trap zu erhalten.Einfache Apache Camel SNMP-Trap

Ich habe das Maven-Projekt mit camel-core und org.apache.servicemix.bundles.snmp4j eingerichtet.

Ich habe keine SNMP-Beispiele nicht in der Lage gewesen zu finden, aber auf der Grundlage anderer Beispiele, die ich mit dieser Hauptklasse gekommen sind:

public class Main { 

    public static Processor myProcessor = new Processor() { 
     public void process(Exchange arg0) throws Exception { 
      // save to database 
     } 
    }; 

    public static void main(String[] args) { 

     CamelContext context = new DefaultCamelContext(); 
     context.addComponent("snmp", new SnmpComponent()); 

     RouteBuilder builder = new RouteBuilder() { 
      public void configure() { 
       from("snmp:127.0.0.1:162?protocol=udp&type=TRAP").process(myProcessor); 
      } 
     }; 

     try { 
      context.addRoutes(builder); 
      context.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

} 

Allerdings, wenn ich es in Eclipse als Java-Anwendung ausführen es tritt nur nach läuft für eine halbe Sekunde. Ich war es erwartet am Laufen zu halten und zu hören 127.0.0.1:162 ...

Jede Hilfe ist sehr geschätzt

Antwort

0

Eine Möglichkeit, mindestens eine Falle zu holen und zu System.out drucken ist wie folgt:

public class SNMPTrap { 

    private Main main; 

    public static void main(String[] args) throws Exception { 
     SNMPTrap snmpTrap = new SNMPTrap(); 
     snmpTrap.boot(); 
    } 

    @SuppressWarnings("deprecation") 
    public void boot() throws Exception { 

     main = new Main(); 
     main.bind("snmp", new SnmpComponent()); 
     main.addRouteBuilder(new MyRouteBuilder()); 
     main.addMainListener(new Events()); 

     System.out.println("Starting SNMPTrap. Use ctrl + c to terminate the JVM.\n"); 
     main.run(); 
    } 

    private static class MyRouteBuilder extends RouteBuilder { 
     @Override 
     public void configure() throws Exception { 
      from("snmp:127.0.0.1:162?protocol=udp&type=TRAP").process(myProcessor) 
       .bean("snmp"); 
     } 
    } 

    public static Processor myProcessor = new Processor() { 
     public void process(Exchange trap) throws Exception { 
      System.out.println(trap.getIn().getBody(String.class)); 

      // Save to DB or do other good stuff 
     } 
    }; 

    public static class Events extends MainListenerSupport { 

     @Override 
     public void afterStart(MainSupport main) { 
      System.out.println("SNMPTrap is now started!"); 
     } 

     @Override 
     public void beforeStop(MainSupport main) { 
      System.out.println("SNMPTrap is now being stopped!"); 
     } 
    } 
} 

Allerdings werde ich gewarnt, dass Main, die Teil von Camel Core ist, jetzt veraltet ist.