Results 1 to 2 of 2

Thread: Wrapping an ApplicationListener in a Transaction

  1. #1
    Join Date
    Oct 2005
    Location
    Arlington, VA
    Posts
    14

    Default Wrapping an ApplicationListener in a Transaction

    I have an ApplicationListener that makes use of a DAO, and I want the onApplicationEvent method to be performed in a Transaction.

    In my config file I have something like:

    Code:
    <bean id="myListener" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="proxyInterfaces">
            <value>
            org.springframework.context.ApplicationListener
            </value>
        </property>
         <property name="target">
              <bean class="mypackage.MyListener">
                  <property name="myDAO" ref="myDAO"/>
              </bean>
         </property>
         <property name="transactionAttributes">
             <props>
                 <prop key="onApplicationEvent">
                  PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
                 </prop>
             </props>
         </property>
     </bean>
    But this fails, presumably because the class that is the ApplicationListener is not the class being registered, but a nested property.

    So, my question is, how can I make this work?

  2. #2
    Join Date
    Aug 2004
    Location
    New Zealand
    Posts
    13

    Default

    Don't use ApplicationContext to publish events. Create a 'Notifier' class, implement ApplicationContextAware and ApplicationEventPublisher.
    In the publishEvent method simply passthrough to the ApplicationEventPublisher method.
    Always use this class to publish events in your application.
    use the 'publishEvent' method and the ApplicationEventPublisher interface in your TransactionProxyFactoryBean.

    For example:
    public class Notifier implements ApplicationContextAware, ApplicationEventPublisher {
    ApplicationContext context;
    DataAccess dao;
    public Notifier(DataAccess dao) {
    this.dao = dao;
    }
    public void setApplicationContext(ApplicationContext context) throws BeansException {
    this.context = context;
    }
    public void publishEvent(ApplicationEvent event) {
    context.publishEvent(event);
    }
    }

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

    <bean id="notifierTarget" class="nz.co.syntax.Notifier">
    <constructor-arg ref="dao"/>
    </bean>

    <bean id="notifier" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="proxyInterfaces" value="org.springframework.context.ApplicationEven tPublisher"/>
    <property name="target" ref="notifierTarget"/>
    <property name="transactionAttributes">
    <props>
    <prop key="publishEvent"> PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
    </prop>
    </props>
    </property>
    </bean>


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

    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    ApplicationEventPublisher publisher = (ApplicationEventPublisher) ctx.getBean("notifier");
    publisher.publishEvent(...);




    cheers

    Dave
    Dave Jenkins
    Wellington
    New Zealand

Similar Threads

  1. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  2. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  3. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •