【问题标题】:Apache Camel web service using spring使用 Spring 的 Apache Camel Web 服务
【发布时间】:2015-09-04 12:09:26
【问题描述】:

我对 Apache Camel 非常陌生,如果我在任何时候错了,请不要介意。 我有两个关于 apache camel 的问题。

  1. 在互联网上的 apache camel 示例中,我只能看到访问 Web 服务并将其路由到另一个。如何调用 WebService、解组对象并在我的应用程序中使用,而不是路由到另一个。
  2. 在示例中,为了调用骆驼上下文,我们必须调用

CamelContext 上下文 = 新 DefaultCamelContext();

context.start()

context.stop()

春天有没有其他方法可以在应用程序上下文中创建一个 Singleton 对象并在企业项目的我的服务类中自动装配 bean?

此外,如果有人可以向我指出任何可以帮助我的资源,例如 pdf 或网站,那将非常有帮助。

【问题讨论】:

    标签: web-services apache-camel integration


    【解决方案1】:

    只需将骆驼上下文声明为普通 bean

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee="http://www.springframework.org/schema/jee"
        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">
    
        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
            <package>my.package.with.routebuilders</package>
        </camelContext>
    

    并将其连接到您的服务类中

        @Autowired
        private CamelContext camelContext;
    

    【讨论】:

    • 我还必须提供camelContext.start() 和camelContext.stop() 方法吗?或者我可以直接使用camelContext?
    • 没有必要。 Camel 上下文将从 spring 上下文启动开始,并在 spring 上下文停止时停止
    【解决方案2】:

    到第一个:

    您可以创建一个处理器来处理来自您的网络服务的数据。您的处理器可以连接到您的路由构建器。您的路线构建器可能如下所示:

    public class MyRouteBuilder extends SpringRouteBuilder {
        @Autowired
        private MyProcessor myProcessor;
    
        @Override
        public void configure() throws Exception {
            from("xy")
            .process(myProcessor);
        }
    }
    

    您的处理器:

    public class MyProcessor implements Processor {
        public void process(Exchange exchange) throws Exception {
            Message msg = exchange.getIn();
            //do something with your message
        }
    }
    

    当然,您必须为您的处理器创建一个 bean。你可以通过注释它并启用spring组件扫描或在你的spring-context.xml中定义它:

    <bean class="com.package.of.your.processor.MyProcessor" />
    

    到第二次:

    就像@Sergey 所说的那样。你可以在你的 spring 配置中实例化你的上下文。 在我的示例中,您的 springconfig 将如下所示:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee="http://www.springframework.org/schema/jee"
        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">
    
        <bean class="com.package.of.your.processor.MyProcessor" />
    
        <bean id="myRouteBuilder" class="com.package.of.your.routbuilder.MyRouteBuilder" />
    
        <camel:camelContext id="camelContext">
            <camel:routeBuilder ref="myRouteBuilder" />
        </camel:camelContext>
    </beans>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多