You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "richie.riviere@gmail.com" <ri...@gmail.com> on 2013/12/11 23:05:15 UTC

JPA @Consumed

Hi guys,

I have a route which selects from the database using a JPA entity bean. I
then do some processing on the bean in the route and would like to mark the
bean as processed when the route is complete.

Seems like a pretty typical scenario for me to use @Consumed in my entity
bean. The select works, the processing works but for some reason I don't get
the updates i do in @consumed reflected in the database.

Can someone give me a tip as to why this is not happening.

thanks

Sorry for the long code extract but my route looks like this...

		from("direct:itemExport")
		
		//route generics
		.routeId("itemExportMagento")
		.autoStartup("{{item.export.magento}}")	
		.startupOrder(2) 
		.shutdownRoute(ShutdownRoute.Defer)
		.errorHandler(deadLetterChannel("log:dead")
			.maximumRedeliveries(5)
			.retryAttemptedLogLevel(LoggingLevel.ERROR))
		
		//route specifics
	
.to("jpa://org.apache.camel.auski.etl.entity.ImportPayloadEntity?consumer.namedQuery=queryImportPayloads&consumeDelete=false&delay=3000&consumeLockEntity=false")
		.to("itemExportMagentoFromSourceHeaderProcessor") 		
		.to("bean:serviceBean?method=login")
		.to("itemExportMagentoFromLoginBodyProcessor")		
		.split().method("splitterService","magentoUowSplit")
		.to("itemExportMagentoActivityCodeProcessor")		
		
		// check activity code
		.choice()
		    .when(header(CamelConstants.HEADER_KEY_ITEM_ACTIVITY_CODE)
		    .endsWith(MinderConstants.ITEM_ADD))	
		    	.to("bean:serviceBean?method=createSimpleProduct")
		    .when(header(CamelConstants.HEADER_KEY_ITEM_ACTIVITY_CODE)
		    .endsWith(MinderConstants.ITEM_UPDATE))
		        .to("bean:serviceBean?method=updateSimpleProduct")
		    .when(header(CamelConstants.HEADER_KEY_ITEM_ACTIVITY_CODE)
		    .endsWith(MinderConstants.ITEM_DELETE))
		        .to("bean:serviceBean?method=deleteSimpleProduct")
		     .otherwise()
		     	.to("log:xml?level=ERROR").stop();	

My org.apache.camel.auski.etl.entity.ImportPayloadEntity bean actually
contains another entity bean named
org.apache.camel.auski.etl.entity.ItemEntity. So what I am doing in the
route is selecting import payload entities and then splitting each one up to
process item entities one by one.

Inside createSimpleProduct, updateSimpleProduct and deleteSimpleProduct I
then call markConsumed on the item entity bean and the import entity bean
which are both marked with @Consumed to execute.

	@Consumed
	public void markConsumed() {
		this.setProcessedInd("Y");
		this.setProcessedDatetime(new Date());
	}	

These fields are not getting set in my db. I also know that it is not trying
to run any sql to update these fields as I have a log of all the sql
executed during execution.

If you have any ideas as to why this is not working I would appreciate it.



--
View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JPA @Consumed

Posted by "richie.riviere@gmail.com" <ri...@gmail.com>.
I just had a thought whilst walking my dog. In the past when I have got JPA
updating the database it has been in a TypeConverter class and what I had to
do was .....

JpaTemplate template =
exchange.getIn().getHeader(CamelConstants.HEADER_KEY_JPA_TEMPLATE,
JpaTemplate.class);
template.persist(item);	


So having a think about that I went into my type converter and tried to grab
this header but to my surprise it wasn't there. 

Will be thinking about this more... ideas are welcome.

thanks



--
View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640p5744641.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JPA when is @Consumed called - timing of it

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Dec 12, 2013 at 10:30 PM, richie.riviere@gmail.com
<ri...@gmail.com> wrote:
> Actually I think I know the issue here. I'm guessing is probably related to
> the timing that the @consume method is called.
>
> In my route I don't set the workingProcessedIndicator on the ItemEntity
> until the very end of the route using my Bean createSimpleItem method.
> Perhaps when the @consume method is called that part of the route has not
> executed yet.
>
> In the documentation it says...
>
> @Consumed which will be invoked on your entity bean when the entity bean
> when it has been processed (and when routing is done)
>
> Does this mean it executes the entire route before running this method?
>

Yes its invoked at the very very end.

There is a new @PreConsumed or something like that annotation that is
invoked on the very very beginning :)
Its coming in Camel 2.13
http://camel.apache.org/camel-2130-release.html



>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640p5744699.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Make your Camel applications look hawt, try: http://hawt.io

Re: JPA when is @Consumed called - timing of it

Posted by "richie.riviere@gmail.com" <ri...@gmail.com>.
Actually I think I know the issue here. I'm guessing is probably related to
the timing that the @consume method is called.

In my route I don't set the workingProcessedIndicator on the ItemEntity
until the very end of the route using my Bean createSimpleItem method.
Perhaps when the @consume method is called that part of the route has not
executed yet.

In the documentation it says...

@Consumed which will be invoked on your entity bean when the entity bean
when it has been processed (and when routing is done)

Does this mean it executes the entire route before running this method?



--
View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640p5744699.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JPA @Consumed

Posted by "richie.riviere@gmail.com" <ri...@gmail.com>.
Abother question about @Consumed. Using the strategy as described in my last
post, what I am doing in selecting an ImportPayloadEntity, splitting this
entity which has a list of ItemEntities into individual ItemEntity objects
and processing each ItemEntity object.

During the processing of each ItemEntity object I call...

        item.setWorkingProcessedInd("Y");
        item.setWorkingProcessedDatetime(new Date());  

Then once the route is finish in the ImportPayloadEntity bean I do something
like this...

	@Consumed
	public void markConsumed() {
		boolean errors = false;
    	Collection<ItemEntity> itemEntities = this.getItems();
    	for (ItemEntity itemEntity: itemEntities){
    		if (itemEntity.getProcessedInd().equals(DaoConstants.ERROR)){
    			errors = true;
    		}
    		itemEntity.setProcessedInd(itemEntity.getWorkingProcessedInd());
    	
itemEntity.setProcessedDatetime(itemEntity.getWorkingProcessedDatetime());
    	}
    	this.setProcessedInd(errors ? DaoConstants.ERROR :
DaoConstants.PROCESSED);
		this.setProcessedDatetime(new Date());
	}


This leads to the ImportPayloadEntity record in the db being updated. But
where this strategy fails is that when i do...
    		itemEntity.setProcessedInd(itemEntity.getWorkingProcessedInd());
    	
itemEntity.setProcessedDatetime(itemEntity.getWorkingProcessedDatetime());

itemEntity.getWorkingProcessedInd() and
itemEntity.getWorkingProcessedDatetime() are both null. In other words it
has not remembered the values I set on the objects in my route.

It seems somewhere along the line my ItemEntity objects are not being passed
by reference. Am I right?
What would be a good way to fix this?

thanks






--
View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640p5744651.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JPA @Consumed

Posted by "richie.riviere@gmail.com" <ri...@gmail.com>.
Maybe the pattern is to just have ImportPayloadEntity @Consumed loop through
it's collection of ItemEntity classes and mark them all as processed. I just
tried this and it worked (although I'm not sure it's the right pattern).

thanks



--
View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640p5744650.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JPA @Consumed

Posted by "richie.riviere@gmail.com" <ri...@gmail.com>.
Thanks Williem,

Changing my route to ...

.pollEnrich("jpa://org.apache.camel.auski.etl.entity.ImportPayloadEntity?consumer.namedQuery=queryImportPayloads&consumeDelete=false&delay=3000&consumeLockEntity=false")

worked!!!

But still a bit of a problem.. The ImportPayloadEntity contains a list of
ItemEntity beans (one to many jpa relationship) and they also have a flag on
them (processed_ind) which I am trying to update. Is there some setting you
can use to do something similar for the nested ItemEntity beans? 

I'm going to have a read of the documentation on this setting now to see if
I can find anything but any help is welcome.

thanks again



--
View this message in context: http://camel.465427.n5.nabble.com/JPA-Consumed-tp5744640p5744646.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JPA @Consumed

Posted by Willem Jiang <wi...@gmail.com>.
Can you change the below to(“jpa…”) to pollEnrich(“jpa…”)?
In this way the @Cousumed can be called.

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com(http://willemjiang.blogspot.com/) (English)
http://jnn.iteye.com(http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On December 12, 2013 at 6:05:43 AM, richie.riviere@gmail.com (richie.riviere@gmail.com) wrote:
> > .to("jpa://org.apache.camel.auski.etl.entity.ImportPayloadEntity?consumer.namedQuery=queryImportPayloads&consumeDelete=false&delay=3000&consumeLockEntity=false")  
>