Federico Mariani created CAMEL-24099:
----------------------------------------
Summary: camel-bean: bean implementing Service is not restarted on
route restart when used via bean() DSL
Key: CAMEL-24099
URL: https://issues.apache.org/jira/browse/CAMEL-24099
Project: Camel
Issue Type: Bug
Components: camel-bean
Affects Versions: 4.21.0
Reporter: Federico Mariani
A bean that implements {{org.apache.camel.Service}} and is used in a route via
the {{.bean(instance)}} DSL has its {{stop()}} method invoked when the route is
stopped, but {{start()}} is *never invoked again* when the route is restarted.
The bean then runs in "stopped" state (closed connections, released resources)
while the route is up.
*Cause*
{{BeanProcessor}} delegates lifecycle asymmetrically:
{code:java}
protected void doStart() throws Exception { delegate.start(); } //
state-machine method
protected void doStop() throws Exception { delegate.doStop(); } // raw method
- bypasses BaseService state
{code}
Because {{delegate.doStop()}} bypasses {{BaseService.stop()}}, the delegate's
status remains {{STARTED}} after the route stops. On route restart
{{delegate.start()}} early-returns ({{BaseService.start()}} returns immediately
when {{status == STARTED}}), so {{AbstractBeanProcessor.doStart()}} never
re-invokes {{ServiceHelper.startService(bean)}}.
The asymmetry was introduced by CAMEL-11375 (commit b3ca9219fb8c, "Optimise -
BeanProcessor - Make light-weight not as service").
*Scope*
Only the {{.bean()}} / {{<bean>}} DSL is affected ({{BeanReifier}} uses
{{BeanProcessor}} directly). {{to("bean:...")}} endpoints are masked because
{{BeanProducer.doStart/doStop}} independently start/stop the bean.
*Suggested fix*
Call {{delegate.stop()}} in {{doStop()}}. Note {{doShutdown()}} calls
{{delegate.shutdown()}} which internally calls {{stop()}} first — today that
causes a second raw {{doStop()}} on a delegate whose status is still
{{STARTED}}, so aligning {{doStop()}} also fixes that double-stop.
*Reproducer*
Attached test {{BeanLifecycleRouteRestartTest}} fails on main (4.22.0-SNAPSHOT)
with:
{noformat}
org.opentest4j.AssertionFailedError: expected: <started> but was: <stopped>
{noformat}
_This issue was found by an AI-assisted code review. Reported by Claude Code on
behalf of [~fmariani] (GitHub: Croway)._
--
This message was sent by Atlassian Jira
(v8.20.10#820010)