可以使用java接口或bean启动驼峰路由吗?

可以使用java接口或bean启动驼峰路由吗?,java,apache-camel,Java,Apache Camel,我想设置一个Springbean(通过接口或bean类)。我可以打电话“开始”一条路线 在这个简单的示例中,当我从代码中调用sayHello(“world”)时,我希望它将sayHello方法的返回值路由到将其写入文件的端点 有人知道这是否可能,或者如何进行吗? 我知道我可以通过CXF公开相同的接口并使其工作,但我真的只想调用一个方法,而不是麻烦地发送jms消息或调用webservice public interface Hello{ public String sayHello(Stri

我想设置一个Springbean(通过接口或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");

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

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

查看这些链接


《骆驼行动手册》第14章对此进行了更详细的介绍:

我需要研究克劳斯的答案,但为了快速而肮脏的用户界面,我采用了不同的方法

我使用的是SpringMVC3.1.X,在我的应用程序中有一个管理控制台来管理各种项目。我编写了一个控制器来显示路由及其状态,并根据需要提供启动和停止路由的链接。下面是一些代码:

@Controller
public class CamelController {
    private static final Log LOG = LogFactory.getLog(CamelController.class);

    @Autowired
    @Qualifier("myCamelContextID")
    private CamelContext camelContext;

    @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
    public String dashboard(Model model) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("camel context is suspended : " + camelContext.isSuspended());
        }

        List<Route> routes = camelContext.getRoutes();
        List<RouteStatus> routeStatuses = new ArrayList<RouteStatus>();
        for (Route r : routes) {
            RouteStatus rs = new RouteStatus();
            rs.setId(r.getId());
            rs.setServiceStatus(camelContext.getRouteStatus(r.getId()));
            routeStatuses.add(rs);
        }

        model.addAttribute("routeStatuses", routeStatuses);

        return "dashboard";
    }

    @RequestMapping(value = "/dashboard/{routeId}/start", method = RequestMethod.GET)
    public String startRoute(@PathVariable String routeId) {
        try {
            camelContext.startRoute(routeId);
            if (LOG.isDebugEnabled()) {
                LOG.debug("camel context is starting route [" + routeId + "]");
            }
        } catch (Exception e) {
            LOG.error("failed to start camel context [" + camelContext + "]");
        }

        return "redirect:/dashboard";
     }

    @RequestMapping(value = "/dashboard/{routeId}/stop", method = RequestMethod.GET)
    public String stopRoute(@PathVariable String routeId) {
        try {
            camelContext.stopRoute(routeId);
            if (LOG.isDebugEnabled()) {
                LOG.debug("camel context is stopping route [" + routeId + "]");
            }
        } catch (Exception e) {
            LOG.error("failed to stop camel context [" + camelContext + "]");
        }
        return "redirect:/dashboard";
        }
    }
}

您可以使用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();
    }
}
您的骆驼路线应该如下所示:

<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>

因为我的路由是使用spring组件的。我做了以下操作,以便在camel路由中使用我的接口

@Component
public class SomeRoute extends RouteBuilder {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void configure() throws Exception {
        from("direct:someroute")
        .bean(applicationContext.getBean(SomeInterface.class).getClass(), "someAbstractMethod")
        .to("direct:otherroute");
    }
}

这是一个非常简单的例子,如果您有多个bean使用同一个接口或抽象类,那么在bean上使用
.getClass()
之前,您可能必须先执行一些逻辑。

其他回答都不适用于我,它们都是构建的并且有效的,但是路由没有触发

这就是我最终使用的解决方案:

import org.apache.camel.Handler;

public class Hello{

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

   @Handler   
   public void sayHello() {
       producer.sendBody("hello")
   }
}

from("timer:hubspotContacts?repeatCount=1").bean(Hello.class);
from("direct:start").to("log:hello");
  • 第一条路线的部件是我丢失的关键部件。使用
    repeatCount=1
    它在启动时只触发一次,并导致调用bean方法。如果需要多次调用该方法,它还支持调用速率或延迟
  • @Handler
    注释用作标记,因此方法名称不必在路由配置中显式显示
  • 组件将生产者与其消费者连接起来

感谢您回答我的问题。我想我需要更新我的MEAP手册。对于第14章,它说待定。这本书很有帮助!
import org.apache.camel.Handler;

public class Hello{

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

   @Handler   
   public void sayHello() {
       producer.sendBody("hello")
   }
}

from("timer:hubspotContacts?repeatCount=1").bean(Hello.class);
from("direct:start").to("log:hello");