Apache Camel - 19 - Lifecycle(Camel 中的生命周期)

版权声明:本文为博主原创文章,可以转载,但请注明出处。 https://blog.csdn.net/Simba_cheng/article/details/81208496

个人原因,Apache Camel的学习滞后了,这个是挤出中午休息时间整理出来的。

Apache Camel 中的生命周期

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo

如果觉得还行,麻烦点个Star

Camel Lifecycle

Camel uses a simple lifecycle interface called Service which has a single start() and stop() method.

Various classes implement Service such as CamelContext along with a number of Component and Endpoint classes.

When you use Camel you typically have to start the CamelContext which will start all the various components and endpoints and activate the routing rules until the context is stopped again.

Camel生命周期

Camel使用一个名为Service的简单生命周期接口,它有一个start()和stop()方法。

各种类实现了诸如CamelContext之类的Service以及许多Component和Endpoint类。

当您使用Camel时,您通常必须启动CamelContext,它将启动所有各种组件和端点并激活路由规则,直到再次停止上下文。

CamelContext Lifecycle

The CamelContext provides methods to control its lifecycle:

    • start

    • stop

    • suspend Camel 2.5

    • resume Camel 2.5

The operations is paired: start/stop and suspend/resume.

Stop is performing a Graceful shutdown which means all its internal state, cache, etc is cleared. And the routes is being stopped in a graceful manner to ensure messages is given time to complete. If you start a CamelContext after a stop, then its performing a cold start, recreating all the state, cache etc. again.

Instead you can use the suspend/resume operations. They will keep the CamelContext warm and only suspend/stop routes using the same Graceful shutdown feature. This ensures messages is given time to complete.

End users is encouraged to use suspend/resume if you are temporary stopping a Camel application.

All these operations is available in JMX as well, so you can control Camel from a management console.

CamelContext生命周期

CamelContext提供了控制其生命周期的方法:

    • 开始

    • 停止

    • 暂停Camel 2.5

    • 恢复Camel 2.5

操作配对:启动/停止和暂停/恢复。

Stop正在执行Graceful关闭,这意味着它的所有内部状态,缓存等都被清除。 并且正在以优雅的方式停止路由以确保消息有时间完成。 如果在停止后启动CamelContext,则执行冷启动,再次重新创建所有状态,缓存等。

相反,您可以使用暂停/恢复操作。 它们将使CamelContext保持温暖,并且仅使用相同的Graceful关闭功能暂停/停止路由。 这可确保消息有时间完成。

如果您临时停止Camel应用程序,建议最终用户使用暂停/恢复。

所有这些操作也可以在JMX中使用,因此您可以从管理控制台控制Camel。

CamelContext接口是Camel框架的核心,它负责处理路由中的消息 。

看下源码中是怎么定义CamelContext的:

Interface used to represent the CamelContext used to configure routes and the policies to use during message exchanges between endpoints.

CamelContext接口,用于表示配置路由以及在端点之间的消息交换期间使用的策略。

The CamelContext offers the following methods to control the lifecycle:

start() - to start (important: the start method is not blocked, see more details here)

stop() - to shutdown (will stop all routes/components/endpoints etc and clear internal state/cache)

suspend() - to pause routing messages

resume() - to resume after a suspend

CamelContext提供以下方法来控制生命周期:

start() - to start(重要:start方法没有被阻塞,请参阅这里的详细信息)

stop() - to shutdown(停止所有路由/组件/端点等并清除内部状态/缓存)

suspend() -暂停路由消息。

resume()- 暂停后恢复

Notice: stop() and suspend() will gracefully stop/suspend routes ensuring any messages in progress will be given time to complete. See more details at ShutdownStrategy.

注意:stop()和suspend()将优雅停止/挂起(暂停)路由,确保正在进行的任何消息都有时间完成。请参阅ShutdownStrategy的更多细节。

If you are doing a hot restart then it's advised to use the suspend/resume methods which ensure a faster restart but also allows any internal state to be kept as is. The stop/start approach will do a cold restart of Camel, where all internal state is reset.

如果你正在进行热启动,那么建议使用suspend / resume方法,以确保更快的重启,但也允许任何内部状态保持原样。停止/启动方法将执行Camel的冷启动,其中所有内部状态都被重置。

End users are advised to use suspend/resume. Using stop is for shutting down Camel and it's not guaranteed that when it's being started again using the start method that Camel will operate consistently.

建议最终用户使用暂停/恢复。使用stop是为了关闭Camel,它不能保证当使用start方法重新启动时Camel会持续运行。

Service lifecycle

A service (org.apache.camel.Service) in Camel adheres to the following lifecycle states as illustrated in the diagram below:

服务生命周期

Camel中的服务(org.apache.camel.Service)遵循以下生命周期状态,如下图所示:

Notice: A service can optimally support suspend/resume by the org.apache.camel.SuspendableService. This means not all services in Camel supports suspension. It's encouraged that consumers support suspension which allows to suspend/resume routes.

注意:服务可以通过org.apache.camel.SuspendableService以最佳方式支持挂起/恢复。 这意味着并非Camel中的所有服务都支持暂停。 鼓励消费者支持暂停,允许暂停/恢复路线。

The org.apache.camel.impl.ServiceSupport is a good base class to extend for custom services as it offers the basic functionally to keep track of state. You implement your custom logic in the doStart, doStop, doSuspend, doResume methods.

org.apache.camel.impl.ServiceSupport是一个很好的基类,可以扩展自定义服务,因为它提供了基本的功能来跟踪状态。 您可以在doStart,doStop,doSuspend,doResume方法中实现自定义逻辑。

Routes lifecycle

Routes in Camel have the following operations to control its lifecycle

  • start
  • stop
  • suspend
  • resume
  • remove (previously named shutdown)

The remove operation will remove the route, for example in JMX the route will then be unregistered and its gone. So only use remove if you really want to remove the route. The route must have been stopped before you can remove.

The start and resume operations in JMX checks the state beforehand. So if a route is stopped and you click resume, it will know to invoke start. And likewise if a route has been suspended and you click start it knows to resume instead. This makes management a bit easier.

If a route is suspended then it keeps its resources and all their JMX metrics alive. Where as stopping a route will graceful stop the route, and clear its resources, and as well their JMX metrics. If you want to temporary "pause" a route, then consider using suspend/resume over stop/start.

If a route consumer does not support suspension, it will fallback and stop the route instead.

Routes(路由) 生命周期

Camel中的路由具有以下操作来控制其生命周期

  • start
  • stop
  • suspend
  • resume
  • remove (以前命名为关机)

remove操作将删除该路由,例如在JMX中,该路由将被取消注册并且其消失。因此,如果您确实要删除路线,请仅使用删除。必须先停止路线才能删除。

在start与resume操作在JMX事先检查状态。因此,如果路由停止并且您单击resume,它将知道要调用start。同样,如果一条路线被暂停,你点击start它就知道了resume。这使管理更容易一些。

如果路由被挂起,那么它会保留其资源及其所有JMX指标。在哪里停止路线将优雅地停止路线,并清除其资源,以及他们的JMX指标。如果您想暂时“暂停”路线,请考虑使用暂停/恢复停止/启动。

如果路线消费者不支持暂停,它将回退并停止路线。

参考来源:

http://camel.apache.org/lifecycle.html

《Apache Camel Developer's Cookbook》

猜你喜欢

转载自blog.csdn.net/Simba_cheng/article/details/81208496