2010-07-29 66 views
9

我想建立一個spring bean(通過接口或bean類)。我可以打電話給「啓動」一條路線。是否可以使用java接口或bean啓動駱駝路由?

在當我打電話的sayHello(「世界」)的代碼,我想它想的路線sayHello方法的返回值,將其寫入到文件中的端點,這個簡單的例子。

有誰知道這是可能的,或者怎麼去呢? 我知道我可以通過CXF公開相同的接口,並做到這一點,但我真的只想調用一個方法,而不是經歷發送jms消息或調用web服務的麻煩。

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

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

回答

11

是的,你可以使用駱駝代理/遠程處理來做到這一點。

然後,當調用的sayHello(值)的值被路由到所選擇的路由。並且來自路線的回覆從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

+0

謝謝回答我的問題連接生產者。我想我需要更新我的MEAP書籍。 TBD說第14章。這本書雖然非常有幫助! – ScArcher2 2010-07-30 13:51:39

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; 
    } 
} 
9

您可以使用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> 
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"); 
    } 
} 

這是非常簡單的情況下,如果你有使用相同的接口或抽象類中的多個豆你可能會對豆使用.getClass()之前做一些邏輯。

0

無其它回答的工作對我來說,他們都建立並且是有效的,但路線沒有觸發。

這是我最終使用的解決方案:

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組件的清晰與它的消費者