[ 
https://issues.apache.org/jira/browse/CAMEL-18196?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17556385#comment-17556385
 ] 

David Url commented on CAMEL-18196:
-----------------------------------

I understand that camel needs to be started, however this is handled by spring 
(SpringBootCamelContext), which also initializes my Producer POJO, and also 
initializes another component which starts sending as soon as it was created.
So i am wondering about the "best practice "approach to block/wait until it is 
safe to use the Producer POJO. Because checking the state of the CamelContext 
kinda violetes the idea behind "Hiding the Camel APIs From Your Code".

I'm happy to submit such a documentation PR, but I'm not confident that i can 
make a helpful proposal, beyond adding a reference to the lifecycle docs.

> POJO-Producing: java.lang.IllegalStateException: Producer has not been started
> ------------------------------------------------------------------------------
>
>                 Key: CAMEL-18196
>                 URL: https://issues.apache.org/jira/browse/CAMEL-18196
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core, documentation
>    Affects Versions: 3.17.0
>            Reporter: David Url
>            Priority: Major
>
> I was following the POJO-Producing seciton in the manual in order to have 
> transport agnostic spring beans that produce events. 
> (https://camel.apache.org/manual/pojo-producing.html#_hiding_the_camel_apis_from_your_code)
> My setup looks like this:
> {code}
> @Component
> public class RecorderSender {
>    public static final String ENDPOINT_NAME = 
> "direct:recorderSender?block=true&synchronous=true";
>    private ProducerEndpoint<byte[]> endpoint;
>    @Produce(RecorderSender.ENDPOINT_NAME)
>    public void setEndpoint(final ProducerEndpoint<byte[]> endpoint) {
>       this.endpoint = endpoint;
>    }
>    public void write(final byte[] payload) {
>       this.endpoint.send(payload);
>    }
> }
> @InOnly
> public interface ProducerEndpoint<T> {
>    void send(T object);
> }
> {code}
> However once my spring bean is initialized it starts calling 
> {{RecorderSender.write(byte[])}} at a time where the CamelContext is not yet 
> initialized, and I get this exception:
> {noformat}
> Exception in thread "connector-1" java.lang.IllegalStateException: Producer 
> has not been started: 
> Producer[direct://recorderSender?block=true&synchronous=true]
>       at 
> org.apache.camel.processor.EventNotifierProducer.process(EventNotifierProducer.java:54)
>       at 
> org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)
>       at 
> org.apache.camel.support.DefaultAsyncProducer.process(DefaultAsyncProducer.java:41)
>       at 
> org.apache.camel.component.bean.AbstractCamelInvocationHandler$1.call(AbstractCamelInvocationHandler.java:189)
>       at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
>       at 
> org.apache.camel.component.bean.AbstractCamelInvocationHandler.doInvoke(AbstractCamelInvocationHandler.java:207)
>       at 
> org.apache.camel.component.bean.AbstractCamelInvocationHandler.invokeProxy(AbstractCamelInvocationHandler.java:169)
>       at 
> org.apache.camel.component.bean.CamelInvocationHandler.doInvokeProxy(CamelInvocationHandler.java:43)
>       at 
> org.apache.camel.component.bean.AbstractCamelInvocationHandler.invoke(AbstractCamelInvocationHandler.java:81)
>       at jdk.proxy2/jdk.proxy2.$Proxy99.send(Unknown Source)
>       at com.example.RecorderSender.write(RecorderSender.java:45)
>       at 
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
>       at 
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
>       at java.base/java.lang.Thread.run(Thread.java:833)
> {noformat}
> I assumed that the properties {{block}} and {{synchronous}} would ensure that 
> the call is blocking, regardless of the initialization state of the 
> CamelContext. 
> I'm not sure whether this is actually a bug, or intentional behavior. But it 
> would be nice to have the POJO Producing guide mentioning this behavior, or 
> hinting at a better solution.



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to