Results 1 to 3 of 3

Thread: Configured AOPs not working in Spring Batch Samples

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    4

    Default Configured AOPs not working in Spring Batch Samples

    Hi All,

    Am new to Spring Batch and even Spring AOP.

    I am executing the FootBall Sample and its working as expected. But the AOPs that were configured for logging weren't working.

    Code:
    <!--  AOP Configurations -->
    <bean id="logAdvice" class="org.springframework.batch.sample.common.LogAdvice" />
    	<bean id="eventAdvice" class="org.springframework.batch.sample.jmx.StepExecutionApplicationEventAdvice" />
    	<aop:config>
    		<aop:aspect id="moduleLogging" ref="logAdvice">
    			<aop:after
    				pointcut="execution( * org.springframework.batch.item.ItemWriter+.write(Object)) and args(item)"
    				method="doStronglyTypedLogging" />
    		</aop:aspect>
    		<aop:aspect ref="eventAdvice">
    			<aop:before
    				pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
    				method="before" />
    			<aop:after
    				pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
    				method="after" />
    			<aop:after-throwing throwing="t" 
    				pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
    				method="onError" />
    		</aop:aspect>
    	</aop:config>
    The above code is as provided in the samples. I modified LogAdvice class to use Log4j instead of Commons Logging.

    When ran the tests, doStronglyTypedLogging in LogAdvice is never called.

    For the other aop aspect, before and after methods for StepExecutionApplicationEventAdvice is invoked and able to log a message to console. But what happens after the below code is invoked ?
    Code:
    applicationEventPublisher.publishEvent(new SimpleMessageApplicationEvent(source, message));
    When & How will the toString() method be called on SimpleMessageApplicationEvent ?

    Am I missing any other configurations thats causing problem with aop logging.. Please advice.

    Thanks,
    Madan Narra

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    I think the point cut in the logging aspect is wrong - the signature of ItemWriter.write() is incorrect. Try write(..) instead of write(Object).

    The event publisher advice publishes an ApplicationEvent - there are no listeners registered by default, so it's just there as a demo of the feature. ApplicationListener is a Spring Framework feature, so ask about it in the main forum if you want to use it.
    Last edited by Dave Syer; Apr 26th, 2011 at 03:09 AM.

  3. #3
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    4

    Default

    Hi Dave,

    Thanks for the reply.

    Changing write(Object) to write(..) fixed the issue.

    Also, I got the listener setup to listen for the events.

    Below is the configuration for others who would like to setup an listener to capture the published events.

    Modify launch-context.xml and add the below code
    Code:
    <bean id="simpleMessageListener" class="org.springframework.batch.sample.jmx.SimpleMessageListener"/>
    And corresponding SimpleMessageListener.java
    Code:
    package org.springframework.batch.sample.jmx;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    
    public class SimpleMessageListener implements ApplicationListener {
    
    	@Override
    	public void onApplicationEvent(ApplicationEvent event) {
    		if(event instanceof SimpleMessageApplicationEvent) {
    			System.out.println("Message Received :: "+((SimpleMessageApplicationEvent)event).toString());
    		}
    	}
    
    }
    Thanks,
    Madan Narra

Posting Permissions

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