[mule-user] Using Hibernate in a component

2 views
Skip to first unread message

Philippe Suray

unread,
Oct 27, 2008, 10:02:06 AM10/27/08
to us...@mule.codehaus.org

Hello list,

 

I am using Mule 2.0.2 in a projet.

 

The goal is to download a csv file from a FTP server, create an object for each line in the csv, get some data’s from a DB and process the objects and the data’s to crate new records in the DB.

 

I need to use Hibernate to read and write to the DB, how can I setup Mule in order to have access to the Hibernate session in my component?

Here is my config file:

 

      <model>

            <service name="ftpService">

                  <inbound>

                        <quartz:inbound-endpoint jobName="FtpJob"

                             repeatInterval="60000">

                             <quartz:event-generator-job />

                        </quartz:inbound-endpoint>

                  </inbound>

                  <component

                        class="com.marketip.tmc.component.FtpComponent">

                  </component>

                  <outbound>

                        <list-message-splitter-router>

                             <vm:outbound-endpoint path="events">

                                   <payload-type-filter

                                         expectedType="com.marketip.tmc.TrafficEventList" />

                             </vm:outbound-endpoint>

                             <vm:outbound-endpoint path="flow">

                                   <payload-type-filter

                                         expectedType="com.marketip.tmc.TrafficFlowList" />

                             </vm:outbound-endpoint>

                        </list-message-splitter-router>

                       

                  </outbound>

            </service>

            <service name="eventService">

                  <inbound>

                        <vm:inbound-endpoint path="events">

                        </vm:inbound-endpoint>

                  </inbound>

                  <component

                        class="com.marketip.tmc.component.TrafficEventComponent" />

            </service>

            <service name="flowService">

                  <inbound>

                        <vm:inbound-endpoint path="flow"></vm:inbound-endpoint>

                  </inbound>

                  <component

                        class="com.marketip.tmc.component.TrafficFlowComponent" />

            </service>

      </model>

 

Thank you for your help.

 

Regards

 

 

www.market-ip.com

 

Philippe Suray

Market-IP
Chaussée de Marche, 774
BELGIUM - 5100 Naninne

philipp...@market-ip.com

tel:
fax:

+32 81 33 11 11
+32 81 33 11 10

 

 

 

 

 

 

 

 

---------------------------------------------------------------------------------------------------------

**** DISCLAIMER ****
This e-mail and any attachments thereto may contain information
which is confidential and/or protected by intellectual property rights
and are intended for the sole use of the recipient(s) named above.
Any use of the information contained herein (including, but not limited to,
total or partial reproduction, communication or distribution in any form)
by persons other than the designated recipient(s) is prohibited.
If you have received this e-mail in error, please notify the sender either
by telephone or by e-mail and delete the material from any computer.
Thank you for your cooperation.

---------------------------------------------------------------------------------------------------------

 

 

Andrew Perepelytsya

unread,
Oct 27, 2008, 10:04:58 AM10/27/08
to us...@mule.codehaus.org
A recommended design pattern in this case is to have a Hibernate DAO (spring integration works great here) and inject this DAO interface into your component via a property.

HTH,
Andrew

Philippe Suray

unread,
Oct 27, 2008, 10:15:26 AM10/27/08
to us...@mule.codehaus.org

Hello,

 

I found several information’s about this pattern, but I don’t know how to configure spring and mule to have access to my dao in the mule component.

Here is a spring config:

<beans

      xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

 

      <bean id="eventDao" class="com.marketip.tmc.dao.EventDaoImpl">

            <property name="sessionFactory">

                  <ref bean="sessionFactory"/>

            </property>

      </bean>

     

      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

            <property name="configLocation">

                  <value>classpath:hibernate.cfg.xml</value>

            </property>

            <property name="configurationClass">

                  <value>org.hibernate.cfg.AnnotationConfiguration</value>

            </property>

      </bean>

</beans>

 

I include this configuration in mule configuration with

<spring:beans>

      <spring:import resource="applicationContext.xml"/>

</spring:beans>

 

I my component, I have a private property of type EventDao, but the property is null when the component is called. How can I inject in mule the correct object defined in spring?

 

Sorry, I am new to Spring.

 

Thank you for your help.

 

 

Philippe Suray

Market-IP
Chaussée de Marche, 774
BELGIUM - 5100 Naninne

philipp...@market-ip.com

tel:
fax:

+32 81 33 11 11
+32 81 33 11 10

 

 

 

 

 

 

 

---------------------------------------------------------------------------------------------------------

**** DISCLAIMER ****
This e-mail and any attachments thereto may contain information
which is confidential and/or protected by intellectual property rights
and are intended for the sole use of the recipient(s) named above.
Any use of the information contained herein (including, but not limited to,
total or partial reproduction, communication or distribution in any form)
by persons other than the designated recipient(s) is prohibited.
If you have received this e-mail in error, please notify the sender either
by telephone or by e-mail and delete the material from any computer.
Thank you for your cooperation.

---------------------------------------------------------------------------------------------------------

 


De : Andrew Perepelytsya [mailto:aper...@gmail.com]
Envoyé : lundi 27 octobre 2008 15:05
À : us...@mule.codehaus.org
Objet : Re: [mule-user] Using Hibernate in a component

Andrew Perepelytsya

unread,
Oct 27, 2008, 10:18:56 AM10/27/08
to us...@mule.codehaus.org
How did you configure your component in Mule?

Andrew

Philippe Suray

unread,
Oct 27, 2008, 10:26:52 AM10/27/08
to us...@mule.codehaus.org

Hello Andew,

 

Here is my configuration file for Mule :

<spring:beans>

            <spring:import resource="applicationContext.xml"/>

      </spring:beans>

     

      <model>

I need to use Hibernate in TrafficEventComponent and TrafficFlowComponent.

 

Here is a snippet of code of TrafficEventComponent:

public class TrafficEventComponent implements Callable {

     

      private static final Logger log = LoggerFactory.getLogger(TrafficEventComponent.class);

     

      private EventDao eventDao = null;

     

 

      @Override

      public Object onCall(MuleEventContext eventContext) throws Exception {

           

            List<TrafficEvent> list = ((TrafficEventList)eventContext.getMessage().getPayload()).getEvents();

           

            log.info("Start processing {} events", list.size());

            TrafficEvent dbEvent = null;

            for (TrafficEvent trafficEvent : list) {

                  dbEvent = eventDao.getEvent(trafficEvent.getMessageId());

            }

            return null;

      }

}

 

Thank you.

 

 

www.market-ip.com

 

Philippe Suray

Market-IP
Chaussée de Marche, 774
BELGIUM - 5100 Naninne

philipp...@market-ip.com

tel:
fax:

+32 81 33 11 11
+32 81 33 11 10

 

 

 

 

 

 

 

 

---------------------------------------------------------------------------------------------------------

**** DISCLAIMER ****
This e-mail and any attachments thereto may contain information
which is confidential and/or protected by intellectual property rights
and are intended for the sole use of the recipient(s) named above.
Any use of the information contained herein (including, but not limited to,
total or partial reproduction, communication or distribution in any form)
by persons other than the designated recipient(s) is prohibited.
If you have received this e-mail in error, please notify the sender either
by telephone or by e-mail and delete the material from any computer.
Thank you for your cooperation.

---------------------------------------------------------------------------------------------------------

 


De : Andrew Perepelytsya [mailto:aper...@gmail.com]
Envoyé : lundi 27 octobre 2008 15:19


À : us...@mule.codehaus.org
Objet : Re: [mule-user] Using Hibernate in a component

 

How did you configure your component in Mule?

Andrew

Andrew Perepelytsya

unread,
Oct 27, 2008, 10:43:58 AM10/27/08
to us...@mule.codehaus.org
Check out http://mule.mulesource.org/display/MULE2USER/Using+Spring+Beans+as+Service+Components . You need to reference an existing spring bean when defining a Mule service.

Next, ensure your component has public getters and setters for your DAO field, otherwise it won't be injected.

Andrew

Philippe Suray

unread,
Oct 27, 2008, 11:25:20 AM10/27/08
to us...@mule.codehaus.org

I added getter and setter for my DAO field, modified my Spring configuration as mentioned in the link below, but now I have this exception:

 

ERROR 2008-10-27 16:03:56,828 eventService.9 org.mule.service.DefaultServiceExceptionStrategy -

********************************************************************************

Message               : Component that caused exception is: eventService. Message payload is of type: TrafficEventList

Type                  : org.mule.api.service.ServiceException

Code                  : MULE_ERROR--2

Payload               : com.marketip.tmc.TrafficEventList@431d9d

JavaDoc               : http://mule.mulesource.org/docs/apidocs/org/mule/api/service/ServiceException.html

********************************************************************************

Exception stack is:

1. No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here (org.hibernate.HibernateException)

  org.springframework.orm.hibernate3.SpringSessionContext:63 (null)

2. Component that caused exception is: eventService. Message payload is of type: TrafficEventList (org.mule.api.service.ServiceException)

  org.mule.component.DefaultLifecycleAdapter:214 (http://mule.mulesource.org/docs/apidocs/org/mule/api/service/ServiceException.html)

********************************************************************************

Root Exception stack trace:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

      at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)

      at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)

      at com.marketip.tmc.dao.EventDaoImpl.getEvent(EventDaoImpl.java:34)

      at com.marketip.tmc.component.TrafficEventComponent.processEvents(TrafficEventComponent.java:20)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

      at java.lang.reflect.Method.invoke(Method.java:597)

      at org.mule.model.resolvers.AbstractEntryPointResolver.invokeMethod(AbstractEntryPointResolver.java:147)

      at org.mule.model.resolvers.ReflectionEntryPointResolver.invoke(ReflectionEntryPointResolver.java:127)

      at org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:50)

      at org.mule.component.DefaultLifecycleAdapter.intercept(DefaultLifecycleAdapter.java:202)

      at org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:84)

      at org.mule.component.AbstractJavaComponent.doOnCall(AbstractJavaComponent.java:75)

      at org.mule.component.AbstractComponent.onCall(AbstractComponent.java:96)

      at org.mule.model.seda.SedaService$ComponentStageWorker.run(SedaService.java:536)

      at org.mule.work.WorkerContext.run(WorkerContext.java:310)

      at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1061)

      at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:575)

      at java.lang.Thread.run(Thread.java:619)

 

********************************************************************************

 

My DAO is defined as this:

public class EventDaoImpl extends HibernateDaoSupport implements EventDao {

 

Must I define a transaction manager? If yes, how can I do that?

 

Thank you.

     


De : Andrew Perepelytsya [mailto:aper...@gmail.com]
Envoyé : lundi 27 octobre 2008 15:44


À : us...@mule.codehaus.org
Objet : Re: [mule-user] Using Hibernate in a component

 

Check out http://mule.mulesource.org/display/MULE2USER/Using+Spring+Beans+as+Service+Components . You need to reference an existing spring bean when defining a Mule service.

Reply all
Reply to author
Forward
0 new messages