繁体   English   中英

我的Apache Camel处理器无法正常工作

[英]My Apache Camel Processor is Not Working

我正在尝试使用称为ProcessorApache Camel接口,并且遇到了一些困难。 我希望消息发送到1)发送到JBoss Fuse应用程序服务器中的ActiveMQ队列,2)由骆驼处理器处理,然后3)发送到源代码中指定的其他队列。 现在发生的是主打印中的SOP语句和“日志记录”中的一些错误消息,但是没有任何内容从程序发送到队列。

这是我的代码:

/* create a Camel processor */ 

package foo;


import org.apache.camel.Processor; 
import org.apache.camel.Exchange; 
import org.apache.camel.builder.RouteBuilder;


public class MyOwnProcessor implements Processor { 


    //main 
    public static void main(String[] args) { 

        System.out.println("Starting main method in MyOwnProcessor.java");

        RouteBuilder builder = new RouteBuilder() { 
            public void configure() { 
                from("QueueA").processRef("MyOwnProcessor").to("QueueB");
            }
        };

        System.out.println("main is done.");

    } //end main 

    public void process(Exchange exchange) { 
        System.out.println("Hello the process was executed.");

        String s = exchange.getIn().getBody(String.class);
        exchange.getIn().setBody("The body of the message is: " + s); 

    } //end process method 




} //end class

这是当前输出:

在MyOwnProcessor.java中启动main方法

SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为不操作(NOP)记录器实现SLF4J:有关更多详细信息,请参见http://www.slf4j.org/codes.html#StaticLoggerBinder

主要完成。

尝试这个,

public static void main(String args[]) throws Exception {
    // create CamelContext
    CamelContext context = new DefaultCamelContext();

    // connect to embedded ActiveMQ JMS broker
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
            "tcp://localhost:61616");
    context.addComponent("jms",
            JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

    // add our route to the CamelContext
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {
            from("jms:queue:QueueA")
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    String s = exchange.getIn().getBody(String.class);
                    System.out.println("The body of the message is: " + s); 
                }
            }).to("jms:queue:QueueB");
        }
    });

    // start the route and let it do its work
    context.start();
    Thread.sleep(10000);

    // stop the CamelContext
    context.stop();
}

创建路由不会使它运行-您仍然需要运行的CamelContext,并且需要向其传递一条消息以使事情开始。 尝试首先使它开始工作,仅对Processor使用匿名内部类:

public static void main(String[] args) throws Exception {

    CamelContext context = new DefaultCamelContext();

    RouteBuilder builder = new RouteBuilder() {
        public void configure() {
            from("direct:source").process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    System.out.println("Success!");
                }
            });
        }
    };

    context.addRoutes(builder);

    ProducerTemplate template = context.createProducerTemplate();
    context.start();
    template.sendBody("direct:source", "test");
}

一旦可行,添加一个单独的实现Processor的类,并使用它代替匿名内部类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM