Announcement Announcement Module
Collapse
No announcement yet.
Wrapping an ApplicationListener in a Transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment

    Working...
    X