[
https://issues.apache.org/jira/browse/CAMEL-7561?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14051311#comment-14051311
]
Claus Ibsen commented on CAMEL-7561:
------------------------------------
btw if you want Camel to manage the lifecycle of your ProducerTemplate, you can
add it as a service
{code}
context.addService(template);
{code}
Then the template is stopped when Camel stops.
Though when you start Camel again, you would need to re-create a new template,
or start the old one again.
> ProducerCache used in DefaultProducerTemplate uses stale references after
> context restart
> -----------------------------------------------------------------------------------------
>
> Key: CAMEL-7561
> URL: https://issues.apache.org/jira/browse/CAMEL-7561
> Project: Camel
> Issue Type: Bug
> Affects Versions: 2.13.1
> Reporter: Vitalii Tymchyshyn
> Assignee: Claus Ibsen
>
> We've got ProducerTemplate injected into our beans that are used to call
> direct routes. Unfortunately after camel context restart we are starting to
> get
> DirectConsumerNotAvailableException. It looks like it's because of cache
> used. The workaround is to restart a template itself, but it's used in beans
> that don't know about context restarts.
> Here is a test, only testTemplateRestart works now:
> {noformat}
> public class ContextRestartTest {
> CamelContext camelContext;
> ProducerTemplate template;
> @Before
> public void makeContext() throws Exception {
> camelContext = new DefaultCamelContext();
> camelContext.addRoutes(new RouteBuilder(){
> @Override
> public void configure() throws Exception {
> from("direct:test").to("log:test");
> }
> });
> camelContext.start();
> template = camelContext.createProducerTemplate();
> }
> @After
> public void stopContext() throws Exception {
> template.stop();
> camelContext.stop();
> }
> @Test
> public void testDefaultURI() throws Exception {
> template.setDefaultEndpointUri("direct:test");
> template.sendBody("test");
> camelContext.stop();
> camelContext.start();
> template.sendBody("test");
> }
> @Test
> public void testGivenURI() throws Exception {
> template.sendBody("direct:test", "test");
> camelContext.stop();
> camelContext.start();
> template.sendBody("direct:test", "test");
> }
> @Test
> public void testTemplateRestart() throws Exception {
> template.sendBody("direct:test", "test");
> camelContext.stop();
> template.stop();
> template.start();
> camelContext.start();
> template.sendBody("direct:test", "test");
> }
> }
> {noformat}
--
This message was sent by Atlassian JIRA
(v6.2#6252)