Camel: Route from direct to processor

12,263

I would add after your conetx.startRoute("folder-jms-route");

ProducerTemplate pt = conetx.createProducerTemplate();
pt.send("direct:start", ex);

-- Look at http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultProducerTemplate.html for more info

send(String endpointUri, Exchange exchange)
Sends the exchange to the given endpoint 

Notice: that if the processing of the exchange failed with an Exception it is not thrown from this method, but you can access it from the returned exchange using Exchange.getException().
Share:
12,263
user_1234567_java
Author by

user_1234567_java

Updated on June 04, 2022

Comments

  • user_1234567_java
    user_1234567_java almost 2 years

    I have a Spring DSL route like:

    <bean id="sendMsgProc" class="com.tc.infrastructure.utils.jms.SendMessageProcessor"/>   
    
       <camel:camelContext id="folder-jms" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">
        <camel:propertyPlaceholder id="jmsProps" location="classpath:jms-jndi.properties"/>
            <route id="folder-jms-route" autoStartup="true">
               <!-- <from uri="{{jms.output.folder}}"/> -->
               <from uri="direct:start"/>  
              <!--  <to uri="bean:camelMsgBean"/> -->
               <camel:process ref="sendMsgProc"/>
               <to uri="{{jms.in.send}}"/> 
            </route>
        </camel:camelContext> 
    

    And my main class which starts context like:

    SpringCamelContext conetx = (SpringCamelContext)camel.initContextCamel("camel-context.xml", "folder-jms");
                Exchange ex = new DefaultExchange(conetx);
                ex.getIn().setBody(executionTasks.entrySet().iterator().next().getValue(), CamelMessage.class);
                conetx.start();
                conetx.startRoute("folder-jms-route");
    
                Thread.sleep(10000);
                conetx.stopRoute("folder-jms-route");
                conetx.stop();
    

    And I have a processor to get my object form exchange like:

    public class SendMessageProcessor implements Processor {
    
    
        //This processor exist for set headers into sending message
        public void process(Exchange exchange) throws Exception 
        {
            System.out.println("adasdasd");
            CamelMessage message = (CamelMessage)exchange.getIn().getBody(CamelMessage.class);
            System.out.println("Message with correlationId get for exchange " + message.getMsgCorrelationId());
            System.out.println("Body" + message.getBody());
            }
    }
    

    I do set to Exchange in Camel the object from Map like:

    public class CamelMessage extends Message {
    
    
        private Map<String, Object> headersMap;
        private StringBuffer body;
        private String msgCorrelationId;
    
                    public CamelMessage(File msgPath, String msgCorrelationId)
            {
                super.setMsgPath(msgPath);
                this.msgCorrelationId = msgCorrelationId;
            }
    
             public CamelMessage(String correlationID, Map<String, Object> headers, String body)
            {
                setMsgCorrelationId(correlationID);
                setHeadersMap(headers);
                setBody(body);
            }
    
        public Map<String, Object> getHeadersMap() {
            return headersMap;
        }
        protected void setHeadersMap(Map<String, Object> headersMap) {
    
            if(headersMap == null)
                   headersMap = new HashMap<String, Object>();
    
            this.headersMap = headersMap;
        }
    
    
        public String getBody() {
            return body.toString();
        }
        protected void setBody(String body) {
            if(this.body == null)
                this.body = new StringBuffer();
    
            this.body.append(body);
        }
    
    
    
        public String getMsgCorrelationId() {
            return msgCorrelationId;
        }
        private void setMsgCorrelationId(String msgCorrelationId) {
            this.msgCorrelationId = msgCorrelationId;
        }
    }
    

    I can't understand why my Camel Processor doesnt work(doesn't trigger automaticaly). And I expected to get my Object which I setted in exchange camel with all field filled up.

    Please help.