【问题标题】:My Apache Camel Processor is Not Working我的 Apache Camel 处理器不工作
【发布时间】:2015-04-15 21:38:13
【问题描述】:

我正在尝试使用称为ProcessorApache Camel 接口并且遇到了一些困难。我预计消息将 1)发送到 JBoss Fuse 应用程序服务器中的 ActiveMQ 队列,2)由 Camel 处理器处理,然后 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

主要完成。

【问题讨论】:

    标签: java apache-camel


    【解决方案1】:

    试试这个,

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

    【讨论】:

    • 在删除 to("jms:queue:QueueB"); 部分后,我能够让这段代码正常工作。当我有这个to 部分时,我收到这个错误:Apr 16, 2015 12:51:32 PM org.springframework.jms.listener.AbstractMessageListenerContainer invokeErrorHandler WARNING: Execution of JMS message listener failed, and no ErrorHandler has been set. java.lang.NoSuchMethodError: org.apache.camel.util.ObjectHelper.notNull(Ljava/lang/Object;Ljava/lang/String;)V... 任何想法为什么to 部分抛出错误?
    • 如果您将to 部分的代码更改为to("file:/Foo/Bar"),它可以工作。
    【解决方案2】:

    创建路由不会导致它运行——您仍然需要一个正在运行的 CamelContext,并且需要向它传递一条消息才能启动。首先尝试让它工作,只需为您的处理器使用匿名内部类:

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

    一旦成功,添加一个单独的类来实现处理器并使用它而不是匿名内部类。

    【讨论】:

    • 感谢@Dave 的帮助。我基本上尝试了这个并收到代码找不到路线的错误。我正在尝试连接到 JBoss Fuse 上的 ActiveMQ 队列 (amq)。在另一个程序中,我基本上是通过此代码 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); connectionFactory.setPassword("user"); connectionFactory.setUserName("password"); 来执行此操作的,知道如何将其合并到代码中吗?
    • 我自己对 ActiveMQ 不太熟悉,但它看起来在 Camel 世界中是相当标准的。看看这个页面:camel.apache.org/activemq.html -- 看起来你需要调用 context.addComponent() 来设置你的 ActiveMQ 队列。
    • 也就是说,您可能希望首先使上述代码正常工作,以确保基本路线正常工作;然后添加 ActiveMQ 块。我确实在本地运行了上面的代码,它对我有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多