是否可以使用java接口或bean启动camel路由?

ScA*_*er2 13 java apache-camel

我想设置一个spring bean(通过接口或bean类).我可以打电话来"开始"一条路线.

在这个简单的例子中,当我从代码中调用sayHello("world")时,我希望将sayHello方法的返回值路由到将其写入文件的端点.

有谁知道这是否可行,或者如何解决这个问题?我知道我可以通过CXF公开相同的接口并使其工作,但我真的只想调用一个方法,而不是经历发送jms消息或调用webservice的麻烦.

public interface Hello{
   public String sayHello(String value);
}

from("bean:helloBean").to("file:/data/outbox?fileName=hello.txt");
Run Code Online (Sandbox Code Playgroud)

mok*_*ino 12

您可以使用ProducerTemplate:

import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Component
public class HelloImpl implements Hello {

    @Produce(uri = "direct:start")
    private ProducerTemplate template;

    @Override
    public Object sayHello(String value) throws ExecutionException, InterruptedException {
        Future future = template.asyncSendBody(template.getDefaultEndpoint(), value);
        return future.get();
    }
}
Run Code Online (Sandbox Code Playgroud)

你的骆驼路线应该是这样的:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mycompany.camel"/>

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:start"/>
            <to uri="log:com.mycompany.camel?level=DEBUG"/>
        </route>
    </camelContext>

</beans>
Run Code Online (Sandbox Code Playgroud)


Cla*_*sen 10

是的,您可以在Camel中使用代理/远程处理来执行此操作.

然后,当您调用sayHello(value)时,该值将被路由到所选路由.并且路线的回复是从sayHello方法返回的.

请参阅这些链接
- http://camel.apache.org/spring-remoting.html
- http://camel.apache.org/hiding-middleware.html
- http://camel.apache.org/using-camelproxy. HTML

"骆驼在行动"一书的第14章更详细地介绍了这一点:http: //www.manning.com/ibsen