Apache camel 在web应用程序中使用apachecamel而不使用Spring?

Apache camel 在web应用程序中使用apachecamel而不使用Spring?,apache-camel,Apache Camel,我正在探索ApacheCamel在web应用程序中的使用,以实现一些集成模式 我发现的唯一例子是使用Spring 是否有不使用弹簧的样品 感谢您的帮助Spring经常被用来作为bean保存Camel上下文 可以在托管bean的任何其他成员中随意使用Camel-like。这可能比使用spring有点难,这取决于您的webapp中的架构 通常,您可以在webapp中创建或重用一些单例bean(有不同的方法,例如@singletonEJB,或者一些DI框架,例如GUICE(和spring…)。 然后用

我正在探索ApacheCamel在web应用程序中的使用,以实现一些集成模式

我发现的唯一例子是使用Spring

是否有不使用弹簧的样品


感谢您的帮助

Spring经常被用来作为bean保存Camel上下文

可以在托管bean的任何其他成员中随意使用Camel-like。这可能比使用spring有点难,这取决于您的webapp中的架构

通常,您可以在webapp中创建或重用一些单例bean(有不同的方法,例如
@singleton
EJB,或者一些DI框架,例如GUICE(和spring…)。 然后用CamelContext ctx这样的CamelContext创建一个实例变量;然后在construcutor/singleton构造函数中,ctx=newdefaultcamelcontext()

现在您已经运行了camel,只需继续使用Java DSL向其添加组件和路由。camel的几个组件都是半依赖于Spring的。例如JMS组件、Spring WS和其他组件。此外,在没有Spring的情况下,将camel插入servlet容器可能会有困难。但这取决于您使用的用例为了它

在这里阅读上下文生命周期

我想添加对这方面的支持已经有一段时间了。但是我从来没有试过这样做,也没有记录一个票证。所以今天我记录了一个票证,所以将来我们终于可以使用serlvet侦听器来引导Camel。关于票证的更多详细信息。

为了做到这一点,你应该创建一个侦听器类
MyCamelContextInitializing实现javax.servlet.ServletContextListener的侦听器
——它将包含驼峰初始化逻辑

您可以在
WEB-INF/WEB.xml

<web-app>
    <listener>
        <listener-class>com.cheese.MyCamelContextInitialisingListener</listener-class>
    </listener>
<web-app>
MyRouteBuilder
将是您定义的
RouteBuilder
实现,它使用Camel Java DSL定义路由逻辑


无需弹簧。

非常简单,只需执行以下操作:

<display-name>My Web Application</display-name>



<!-- you can configure any of the properties on CamelContext, eg setName will be configured as below -->



    <context-param>
        <param-name>name</param-name>
        <param-value>MyCamel</param-value>
      </context-param>

  <!-- location of Camel route xml files -->
  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    <!-- define the routes as a resource from the classpath by prefixing the value with classpath: -->
    <!-- note: instead of using a XML file we can also define the routes in Java code in a RouteBuilder class -->
    <param-value>classpath:camel-config.xml</param-value>
  </context-param>

  <!-- the listener that kick-starts Camel -->
  <listener>
    <listener-class>org.apache.camel.component.servletlistener.CamelServletContextListener</listener-class>
  </listener>

  <!-- Camel servlet used in the Camel application -->
  <servlet>
    <servlet-name>CamelServlet</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
我的Web应用程序
名称
我的骆驼
路由生成器MyRoute
类路径:camel-config.xml
org.apache.camel.component.servletlistener.CamelServletContextListener
骆驼队
org.apache.camel.component.servlet.CamelHttpTransportServlet
1.

晚会迟到了,但我想发布最新的回复。我最近创建了一个名为Harnese的轻量级框架,它使在Camel中创建web服务变得超级容易,而不需要任何Spring依赖项。它具有几个漂亮的功能,包括:

  • 将Camel对象即插即用插入“线束”,该线束提供管线创建、注册表管理和测试所需的锅炉板代码

  • 用很少的代码快速创建新的业务逻辑的能力。这是因为在Camel和harnese之间已经提供了您可能需要的大部分功能

  • 通过使用*.properties文件,应用程序可以注入模拟、不同的处理器,甚至重新连接业务逻辑

  • 一种在不关闭应用程序或影响应用程序可能正在执行的任何其他操作的情况下重新配置、删除甚至添加业务逻辑的方法

可以找到框架核心库。这是线束文件以及一些辅助工具的位置


可以找到参考实现。这是一个功能齐全的Camel microservice,可以作为模板,让您的骆驼之旅到达它需要去的地方。

我添加了这样一个新组件,它将成为Camel 2.11的一部分。还有一个新的示例:Camel示例servlet tomcat no spring来展示这一点。正在工作关于现在添加文档。链接到github上的示例:当你从其他地方复制粘贴代码示例时,你应该给出一个免责声明-现在看起来你试图声称这是你的工作。当然,我很糟糕,忘记了源代码,下次我将链接源代码。感谢大家的提醒,这个功能已经存在于native Camel中,这将d减少需要第三方库来完成这项工作的必要性。
<display-name>My Web Application</display-name>



<!-- you can configure any of the properties on CamelContext, eg setName will be configured as below -->



    <context-param>
        <param-name>name</param-name>
        <param-value>MyCamel</param-value>
      </context-param>

  <!-- location of Camel route xml files -->
  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    <!-- define the routes as a resource from the classpath by prefixing the value with classpath: -->
    <!-- note: instead of using a XML file we can also define the routes in Java code in a RouteBuilder class -->
    <param-value>classpath:camel-config.xml</param-value>
  </context-param>

  <!-- the listener that kick-starts Camel -->
  <listener>
    <listener-class>org.apache.camel.component.servletlistener.CamelServletContextListener</listener-class>
  </listener>

  <!-- Camel servlet used in the Camel application -->
  <servlet>
    <servlet-name>CamelServlet</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>