代码之家  ›  专栏  ›  技术社区  ›  ScArcher2

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

  •  13
  • ScArcher2  · 技术社区  · 14 年前

    我想设置一个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");
    
    5 回复  |  直到 14 年前
        1
  •  10
  •   Claus Ibsen    14 年前

    是的,您可以在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

    http://www.manning.com/ibsen

        2
  •  12
  •   mokshino    11 年前

    您可以使用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>
    
        3
  •  2
  •   Alpar Amit    7 年前

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

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

    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");
    
    • timer 对我来说,第一条路线的部件是关键缺失部分。与 repeatCount=1 它在启动时只触发一次,并导致调用bean方法。如果需要多次调用该方法,它还支持调用速率或延迟。
    • 这个 @Handler 注释用作标记,因此方法名称不必在路由配置中显式显示
    • 这个 direct
        4
  •  1
  •   Greg    12 年前

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

    @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;
        }
    }
    
        5
  •  0
  •   Yogesh    8 年前

    因为我的路由是使用 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使用同一个接口或抽象类,那么您可能需要在使用之前做一些逻辑 .getClass()