You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by amitmahesh <la...@yahoo.com> on 2015/12/21 09:55:01 UTC

entity manager is injected as null in a camel bean but jpa: endpoint works fine


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
        http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
        http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
       ">

    <tx:annotation-driven transaction-manager="jpaTxManager"/>

    <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
       <property name="transactionManager" ref="jpaTxManager"/>
    </bean>

    <bean id="jpaTxManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="p_amit"/>
    </bean>

    <bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://127.0.0.1:61616" />
    </bean>

    <bean id="pooledConnectionFactory"
class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="maxConnections" value="10" />
        <property name="maximumActiveSessionPerConnection" value="10" />
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="pooledConnectionFactory" />
    </bean>


    <bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
        <property name="connectionFactory" ref="pooledConnectionFactory" />
        <property name="transacted" value="true" />
        <property name="concurrentConsumers" value="15" />
        <property name="deliveryPersistent" value="true" />
        <property name="requestTimeout" value="10000" />
        <property name="cacheLevelName"  value="CACHE_CONSUMER" />
    </bean>

    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> 
      <property name="configuration" ref="jmsConfig" />
    </bean>

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

  <route>  
        <from uri="jms:queue:REQUESTQ_BANK1?concurrentConsumers=1&amp;
                                            maxConcurrentConsumers=1&amp;
                                            asyncConsumer=true"/>
        <transform>
            <simple>${in.body} ****** DATA ENRICHEMENT OVER HERE ******
</simple>
        </transform>

        <bean ref="myDataEnrichment" method="doTransform"/>

        <setHeader headerName="JMSCorrelationID">
            <simple resultType="String">${headers.JMSMessageID}</simple> 
        </setHeader>

        <to uri="jms:queue:RESPONSEQ_BANK1"/>

        <setHeader headerName="JMSCorrelationID">
            <simple resultType="String">${headers.JMSMessageID}</simple>
        </setHeader>  

        <to uri="jms:queue:DBCheckPointQ" />
  </route>

 <route>
        <from uri="jms:queue:DBCheckPointQ?concurrentConsumers=1&amp;
                                            maxConcurrentConsumers=1&amp;
                                            asyncConsumer=true"/>

         <transform>
           <method ref="orderBean" method="generateOrder"/>
         </transform>

        <bean ref="orderService" method="insertOrder" />
        

  </route>
</camelContext>

  <bean id="orderBean" class="entity.OrderBean" /> 
  <bean id="myDataEnrichment" class="service.PinBlockVerification" /> 

  <bean id="orderService" class="service.OrderService"> 
         <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>

</beans>

    @Component
public class OrderService {   

    @PersistenceContext(unitName="p_amit")
    EntityManager entityManager;


    EntityManagerFactory entityManagerFactory;


      @Transactional
      public void insertOrder(Order order) {

          System.out.println("****** Entity Manager ********** == " +
entityManager);
//        System.out.println("****** Entity Manager Factory********** == " +
entityManagerFactory);
//        entityManagerFactory.createEntityManager().persist(order);
           entityManager.persist(order);      
      }

      public void testService(String body) 
      {
          System.out.println( " \n ****** AMIT TESTING  SERVICE **********
== \n" + body);
      }   
}


<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="p_amit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
    
<class>entity.Order</class>
<properties>
      <property name="javax.persistence.jdbc.driver"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> 
      <property name="javax.persistence.jdbc.url"
value="jdbc:sqlserver://172.25.0.32;databaseName=PRE30" /> 
      <property name="javax.persistence.jdbc.user" value="sa" /> 
      <property name="javax.persistence.jdbc.password" value="sqlserver#123"
/> 
      <property name="hibernate.connection.autocommit" value="false" />
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="none" />
      <property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />
</properties> 
  </persistence-unit>
  </persistence>




--
View this message in context: http://camel.465427.n5.nabble.com/entity-manager-is-injected-as-null-in-a-camel-bean-but-jpa-endpoint-works-fine-tp5775308.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: entity manager is injected as null in a camel bean but jpa: endpoint works fine

Posted by amitmahesh <la...@yahoo.com>.

So effectively you are suggesting to always use jpa: uri in the camel route
for all my database access part, and to avoid injecting entity manager in a
camel bean. Means I will have to keep multiple intermediate data processing
beans in camel route to generate/consume entity beans data.



--
View this message in context: http://camel.465427.n5.nabble.com/entity-manager-is-injected-as-null-in-a-camel-bean-but-jpa-endpoint-works-fine-tp5775308p5775867.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: entity manager is injected as null in a camel bean but jpa: endpoint works fine

Posted by SaurabhNayar <Na...@gmail.com>.
The EntityManager will not automatically join the transaction by using
@Transactional annotation.

You will have to write your transaction management code - and make entity
manager participate in the transaction. Kind of beats the purpose of using
Camel - if you have to take all that pain.



--
View this message in context: http://camel.465427.n5.nabble.com/entity-manager-is-injected-as-null-in-a-camel-bean-but-jpa-endpoint-works-fine-tp5775308p5775857.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: entity manager is injected as null in a camel bean but jpa: endpoint works fine

Posted by amitmahesh <la...@yahoo.com>.

Hello Saurabh,
    Even I was worried about the transaction handling part if I inject
entity manager from Spring instead of using the jpa://  endpoint supported
by the Camel JPA component. Our developers felt that jpa:// was helpful only
for Simple CRUD operations but in our case we do a lot of complex SQL and
joins so we wanted some way to inject entity manager in some DAO where we
can write our DB specific code. Pls suggest how to use JPA Component inside
some Camel bean. Currently we were able to get a handle to entity manager
using @PersistenceContext annotation inside a bean which is called from a
camel route and all data access methods in that bean are annotated as
@Transactional.



--
View this message in context: http://camel.465427.n5.nabble.com/entity-manager-is-injected-as-null-in-a-camel-bean-but-jpa-endpoint-works-fine-tp5775308p5775783.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: entity manager is injected as null in a camel bean but jpa: endpoint works fine

Posted by SaurabhNayar <Na...@gmail.com>.
Be aware of the fact that the entity manager that is getting imported is a
Spring resource and may not be present in Camel Registry.

You may have tough time joining the existing Camel transactions - if you
have transactions.

Don't know your business requirements - but better to use Camel JPA
component instead of custom JPA code.



--
View this message in context: http://camel.465427.n5.nabble.com/entity-manager-is-injected-as-null-in-a-camel-bean-but-jpa-endpoint-works-fine-tp5775308p5775768.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: entity manager is injected as null in a camel bean but jpa: endpoint works fine

Posted by amitmahesh <la...@yahoo.com>.

I switched to a older version of spring and this issue got resolved
automatically, now I use version 4.1.8 




--
View this message in context: http://camel.465427.n5.nabble.com/entity-manager-is-injected-as-null-in-a-camel-bean-but-jpa-endpoint-works-fine-tp5775308p5775619.html
Sent from the Camel - Users mailing list archive at Nabble.com.