简单的 Apache Camel SNMP 陷阱

Simple Apache Camel SNMP trap

我是 Apache Camel 的新手,正在尝试接收一个简单的 SNMP 陷阱。

我用 camel-core 和 org.apache.servicemix.bundles.snmp4j.

设置了 Maven 项目

我没能找到任何 SNMP 示例,但基于其他示例我想出了这个 Main class:

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

    }

}

然而,当我在 Eclipse 中 运行 它作为 Java 应用程序时,它只是在 运行ning 半秒后退出。我期待它保持 运行ning 并收听 127.0.0.1:162 ...

非常感谢任何帮助

一种至少捡起陷阱并打印到 System.out 的方法如下:

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 核心的一部分的 Main 现在已被弃用。