2016-11-29 17 views
0

私はApache Camelの初心者で、簡単なSNMPトラップを受け取ろうとしています。シンプルなApache Camel SNMPトラップ

私はMavenプロジェクトをcamel-coreとorg.apache.servicemix.bundles.snmp4jでセットアップしました。

私は任意のSNMPの例を見つけることができたが、私は、このメインクラスが出ている他の例をもとにしていない:

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(); 
     } 

    } 

} 

ただし、Javaアプリケーションは、それだけで後に出るときに、私はEclipseでそれを実行すると半分の時間走っています。私は

...それは実行し続けることを期待して127.0.0.1:162を聞いていたすべてのヘルプは大歓迎され、少なくともトラップをピックアップし、System.outに印刷する

答えて

0

一つの方法はそうのようなものです:

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!"); 
     } 
    } 
} 

しかし、私は現在、Camelコアの一部であるメインが廃止されていると警告しています。