Carla
Carla

Reputation: 3380

from JPA to XML file to Camel


I have arranged for the following Camel route which marshals Entities from the DB to XML files:

<camelContext trace="true"
    xmlns="http://camel.apache.org/schema/spring">

<dataFormats>
    <jaxb contextPath="com.mycompany.model" id="jaxb-xml" />
</dataFormats>

    <route>

        <from uri="jpa:com.mycompany.model.User?consumeDelete=false&amp;consumer.namedQuery=User.findAll" />    
         <marshal ref="jaxb-xml"/>
        <to uri="file:/home/carla/fileout" />
    </route>
</camelContext>

It works but the problem is that the consumer keeps polling so adding unwanted additional XML copies in the fileout folder. What could be the most appropriate way to execute just once ? Thanks!

Upvotes: 0

Views: 260

Answers (2)

Jonathan Schoreels
Jonathan Schoreels

Reputation: 1720

From Camel doc

If you would rather perform some update on the entity to mark it as processed (such as to exclude it from a future query) then you can annotate a method with @Consumed which will be invoked on your entity bean when the entity bean when it has been processed (and when routing is done). From Camel 2.13 onwards you can use @PreConsumed which will be invoked on your entity bean before it has been processed (before routing).

Upvotes: 0

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

You can stop your route using such processor:

public class StopRouteProcessor implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.getContext().stopRoute("jpaConsumer");
    }
}

Try to modify your bundle context like below:

<camelContext trace="true"
    xmlns="http://camel.apache.org/schema/spring">

<dataFormats>
    <jaxb contextPath="com.mycompany.model" id="jaxb-xml" />
</dataFormats>

    <route id="jpaConsumer">
        <from uri="jpa:com.mycompany.model.User?consumeDelete=false&amp;consumer.namedQuery=User.findAll" />    
        <marshal ref="jaxb-xml"/>
        <to uri="file:/home/carla/fileout" />
        <process ref="stopProcessor"/>
    </route>          
</camelContext>

<bean id="stopProcessor" class="com.mycompany.StopRouteProcessor"/>

Upvotes: 1

Related Questions