You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by ebinsingh <eb...@VerizonWireless.com> on 2011/12/15 16:43:44 UTC

convert Exchange Headers to JPA Entity

Hi All,

I am trying to convert the certain (not all) Headers values available in
Exchange to an JPA Entity and put the same in the database.
I want to log these details in the database and am using the intercept api.

Is there a way to do this (i.e convert the Exchange headers into a JPA
Entity)

Appreciate your help.

Thanks & regards,
Ebe

--
View this message in context: http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5077846.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: convert Exchange Headers to JPA Entity

Posted by Achim Nierbeck <bc...@googlemail.com>.
I suggest writing a bean that does it.
Taking the Headers with the @Headers annotation
and returning your entity object which can be persisted by the jpa
component.

regards, Achim

2011/12/15 ebinsingh <eb...@verizonwireless.com>

> Hi All,
>
> I am trying to convert the certain (not all) Headers values available in
> Exchange to an JPA Entity and put the same in the database.
> I want to log these details in the database and am using the intercept api.
>
> Is there a way to do this (i.e convert the Exchange headers into a JPA
> Entity)
>
> Appreciate your help.
>
> Thanks & regards,
> Ebe
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5077846.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 

Apache Karaf <http://karaf.apache.org/> Committer & PMC
OPS4J Pax Web <http://wiki.ops4j.org/display/paxweb/Pax+Web/> Committer &
Project Lead
blog <http://notizblog.nierbeck.de/>

Re: convert Exchange Headers to JPA Entity

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Dec 15, 2011 at 10:20 PM, ebinsingh
<eb...@verizonwireless.com> wrote:
> I am having trouble with fitting in the jpa entity. Not able to find the
> correct syntax to do it.
>
> I tried the below, but no data went into the database. The println's do
> print out the entity data.
>
>        @Override
>        public void process(Exchange exchange) throws Exception {
>                MarsTracerEntity entity = new MarsTracerEntity();
>                if(null != exchange.getIn().getHeader("CamelFileNameOnly"))
>
> entity.setInputFileName((String)exchange.getIn().getHeader("CamelFileNameOnly"));
>                if(null != exchange.getIn().getHeader("CamelBatchSize"))
>
> entity.setBatchSize((Integer)exchange.getIn().getHeader("CamelBatchSize"));
>                if(null != exchange.getIn().getHeader("breadcrumbId"))
>
> entity.setOutputFileName((String)exchange.getIn().getHeader("breadcrumbId"));
>
>                System.out.println("InputFilename  5555 : "+entity.getInputFileName());
>                System.out.println("outputFilename  2222 : "+entity.getOutputFileName());
>                System.out.println("Batch  1111 : "+entity.getBatchSize());
>
>                exchange.getIn().setBody(entity);
>
>        JpaEndpoint endpoint = (JpaEndpoint) exchange.getContext()
>
> .getEndpoint("jpa://org.apache.camel.processor.interceptor.JpaTraceEventMessage?persistenceUnit=tracer");
>        JpaTemplate jpaTemplate = endpoint.getTemplate();
>        jpaTemplate.persist(entity);
>        }
>

You do not need to use Camel for *everything*. If you need to use JPA,
then you can just use plain JPA API.


>
> Entity def:
>
> @Entity
> @Table(name="MarsTrace")
> public class MarsTracerEntity implements Serializable{
>
>
> The intercept config in the Camel Context.
>
>                <intercept>
>                        <process ref="marsTraceProcessor"/>
>                </intercept>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5078708.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: convert Exchange Headers to JPA Entity

Posted by ebinsingh <eb...@VerizonWireless.com>.
Not sure if only one processor in allowed withing a Camel context.

Having the below intercept code which would call a different Processor
"marsTraceProcessor" to create a JPA Entity is actually messing up the
Processor within the actual route that processes the message.

		<intercept>
			<process ref="marsTraceProcessor"/>
			 <to
uri="jpa://org.apache.camel.processor.interceptor.JpaTraceEventMessage?persistenceUnit=tracer"/>  
		</intercept>


The out put of the below processor seems to be messed up with the JPA Entity
created in the intercept Processor.
		<route>
			<from uri="file:C:\\camelProject\\data\\inbox\\mars"/>
			<process ref="marsProcessor"/>
		</route>



*Intecept Processor code:*

public class MarsTraceProcessor implements Processor {
	private static Logger log = Logger.getLogger(MarsTraceProcessor.class);
	@Override
	public void process(Exchange exchange) throws Exception {
		MarsTracerEntity entity = new MarsTracerEntity();
		if(null != exchange.getIn().getHeader("CamelFileNameOnly"))
		
entity.setInputFileName((String)exchange.getIn().getHeader("CamelFileNameOnly"));
		if(null != exchange.getIn().getHeader("CamelBatchSize"))
		
entity.setBatchSize((Integer)exchange.getIn().getHeader("CamelBatchSize"));
		if(null != exchange.getIn().getHeader("breadcrumbId"))
		
entity.setOutputFileName((String)exchange.getIn().getHeader("breadcrumbId"));
		
		log.info("InputFilename  5555 : "+entity.getInputFileName());
		log.info("outputFilename  2222 : "+entity.getOutputFileName());
		log.info("Batch  1111 : "+entity.getBatchSize());
		
		exchange.getIn().setBody(entity);
	}

}



*Processor within Route:*

public class MarsProcessor implements Processor {
	private static Logger log = Logger.getLogger(MarsProcessor.class);

	@Override
	public void process(Exchange exchange) throws Exception {
		
		MarsParser jniParser = new MarsParser();
		List<ParsedDataBean> marsData = jniParser.parseMarsRecords(exchange);
		ProducerTemplate prod = exchange.getContext().createProducerTemplate();
		for(ParsedDataBean data: marsData){
			log.info("File name : "+data.getFileName());
			exchange.getIn().setBody(data.getData());
			exchange.getIn().setHeader(Exchange.FILE_NAME,data.getFileName());
			exchange.getIn().setHeader(Exchange.BATCH_SIZE, data.getSize());
			//log.info("File name : "+exchange.getIn().getBody());
			prod.send("seda:marsDataProcessingQueue", exchange);
		}
	}
}



--
View this message in context: http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5080368.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: convert Exchange Headers to JPA Entity

Posted by Achim Nierbeck <bc...@googlemail.com>.
If I did see that right, you are mixing a processor and a route 
definition here.
Your Processor does look ok until you set the body.
The endpoint shouldn't be part of the class.
More like the following:

  from("direct:start")
         .process(yourProcessorHere)
         
.to("jpa://org.apache.camel.processor.interceptor.JpaTraceEventMessage?persistenceUnit=tracer");

regards, Achim

Am 15.12.2011 22:20, schrieb ebinsingh:
> I am having trouble with fitting in the jpa entity. Not able to find the
> correct syntax to do it.
>
> I tried the below, but no data went into the database. The println's do
> print out the entity data.
>
> 	@Override
> 	public void process(Exchange exchange) throws Exception {
> 		MarsTracerEntity entity = new MarsTracerEntity();
> 		if(null != exchange.getIn().getHeader("CamelFileNameOnly"))
> 		
> entity.setInputFileName((String)exchange.getIn().getHeader("CamelFileNameOnly"));
> 		if(null != exchange.getIn().getHeader("CamelBatchSize"))
> 		
> entity.setBatchSize((Integer)exchange.getIn().getHeader("CamelBatchSize"));
> 		if(null != exchange.getIn().getHeader("breadcrumbId"))
> 		
> entity.setOutputFileName((String)exchange.getIn().getHeader("breadcrumbId"));
> 		
> 		System.out.println("InputFilename  5555 : "+entity.getInputFileName());
> 		System.out.println("outputFilename  2222 : "+entity.getOutputFileName());
> 		System.out.println("Batch  1111 : "+entity.getBatchSize());
> 		
> 		exchange.getIn().setBody(entity);
> 		
>          JpaEndpoint endpoint = (JpaEndpoint) exchange.getContext()
>
> .getEndpoint("jpa://org.apache.camel.processor.interceptor.JpaTraceEventMessage?persistenceUnit=tracer");
>          JpaTemplate jpaTemplate = endpoint.getTemplate();
>          jpaTemplate.persist(entity);
> 	}
>
>
> Entity def:
>
> @Entity
> @Table(name="MarsTrace")
> public class MarsTracerEntity implements Serializable{
>
>
> The intercept config in the Camel Context.
>
> 		<intercept>
> 			<process ref="marsTraceProcessor"/>
> 		</intercept>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5078708.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


-- 
--
*Achim Nierbeck*


Apache Karaf<http://karaf.apache.org/>  Committer&  PMC
OPS4J Pax Web<http://wiki.ops4j.org/display/paxweb/Pax+Web/>    Committer&  Project Lead
blog<http://notizblog.nierbeck.de/>


Re: convert Exchange Headers to JPA Entity

Posted by ebinsingh <eb...@VerizonWireless.com>.
I am having trouble with fitting in the jpa entity. Not able to find the
correct syntax to do it.

I tried the below, but no data went into the database. The println's do
print out the entity data.

	@Override
	public void process(Exchange exchange) throws Exception {
		MarsTracerEntity entity = new MarsTracerEntity();
		if(null != exchange.getIn().getHeader("CamelFileNameOnly"))
		
entity.setInputFileName((String)exchange.getIn().getHeader("CamelFileNameOnly"));
		if(null != exchange.getIn().getHeader("CamelBatchSize"))
		
entity.setBatchSize((Integer)exchange.getIn().getHeader("CamelBatchSize"));
		if(null != exchange.getIn().getHeader("breadcrumbId"))
		
entity.setOutputFileName((String)exchange.getIn().getHeader("breadcrumbId"));
		
		System.out.println("InputFilename  5555 : "+entity.getInputFileName());
		System.out.println("outputFilename  2222 : "+entity.getOutputFileName());
		System.out.println("Batch  1111 : "+entity.getBatchSize());
		
		exchange.getIn().setBody(entity);
		
        JpaEndpoint endpoint = (JpaEndpoint) exchange.getContext()
        
.getEndpoint("jpa://org.apache.camel.processor.interceptor.JpaTraceEventMessage?persistenceUnit=tracer");        
        JpaTemplate jpaTemplate = endpoint.getTemplate();
        jpaTemplate.persist(entity);
	}


Entity def:

@Entity
@Table(name="MarsTrace")
public class MarsTracerEntity implements Serializable{


The intercept config in the Camel Context.

		<intercept>
			<process ref="marsTraceProcessor"/>
		</intercept>

--
View this message in context: http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5078708.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: convert Exchange Headers to JPA Entity

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
You can write a processor (implements Processor), and in the 
process(Exchange exchange) method you select the headers, and populate 
the out message with the value/format that you need to fit your JPA entity.

Regards
JB

On 12/15/2011 04:43 PM, ebinsingh wrote:
> Hi All,
>
> I am trying to convert the certain (not all) Headers values available in
> Exchange to an JPA Entity and put the same in the database.
> I want to log these details in the database and am using the intercept api.
>
> Is there a way to do this (i.e convert the Exchange headers into a JPA
> Entity)
>
> Appreciate your help.
>
> Thanks&  regards,
> Ebe
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/convert-Exchange-Headers-to-JPA-Entity-tp5077846p5077846.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com