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

时间:2010-07-29 19:03:46

标签: java apache-camel

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

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

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

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

from("bean:helloBean").to("file:/data/outbox?fileName=hello.txt");

4 个答案:

答案 0 :(得分: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();
    }
}

你的骆驼路线应该是:

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

答案 1 :(得分: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

答案 2 :(得分:1)

我需要调查克劳斯的答案,但对于快速而肮脏的用户界面,我采用了不同的方法。

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

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

我做了一点POJO:

public class RouteStatus {
    private String id;
    private ServiceStatus serviceStatus;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ServiceStatus getServiceStatus() {
        return serviceStatus;
    }

    public void setServiceStatus(ServiceStatus serviceStatus) {
        this.serviceStatus = serviceStatus;
    }
}

答案 3 :(得分:0)

由于我的路线是使用CamelConfiguration的弹簧组件。我在骆驼路线中使用了我的界面。

@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()之前做一些逻辑。