Solved

Spring AOP for objects instantiated outside of the application context

Posted on 2008-09-30
7
1,282 Views
Last Modified: 2013-11-11
Hi,

I have a simple app. I've got a TaskExecutorService class which instantiated several Runnable's and gives them to the TaskExecutor.

I've managed to wire things with Spring. however what I need is to weave a logger advise before and after the run() method of my Runnable class.

The problem is the task class which implements Runnable is NOT instantiated by Spring, instead it is instantiated by the TaskExecutorService outside the Spring container.

Can somebody tell me how can I weave an advise for objects instantiated OUTSIDE the Spring context? I could not how to make use of ProxyFactory.

Thanks in advance


package springexec;
 

public class HelloWorldCountDownTask implements Runnable {
 

    private String name;

    private int count = 4;
 

    public HelloWorldCountDownTask(String name) {

        this.name = name;

    }        

    

    public void run() {

        while( count > 0 ){

            count--;

            if (count == 0){

                System.out.println(name + " says 'Hello World!'");

            } else {

                System.out.println(name + " : " + count);

                Thread.yield();

            }

        }

    }

}
 

package springexec;
 

import org.springframework.core.task.TaskExecutor;
 

public class TaskExecutorExample {
 

    private TaskExecutor taskExecutor;
 

    public TaskExecutorExample(TaskExecutor taskExecutor) {

        this.taskExecutor = taskExecutor;

    }

    

    public void executeTasks(){

        this.taskExecutor.execute(new HelloWorldCountDownTask("Anna"));

        this.taskExecutor.execute(new HelloWorldCountDownTask("Beth"));

        this.taskExecutor.execute(new HelloWorldCountDownTask("Charlie"));

        this.taskExecutor.execute(new HelloWorldCountDownTask("Daniel"));
 

    }

}
 

package springexec;
 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.core.task.TaskExecutor;
 

public class SyncTaskExecutorExample {
 

    public static void main(String args[]){

        ApplicationContext ctx = new ClassPathXmlApplicationContext("springexec/exec.xml");

        

        TaskExecutor taskExecutor = (TaskExecutor) ctx.getBean("syncTaskExecutor",TaskExecutor.class);

        TaskExecutorExample example = new TaskExecutorExample(taskExecutor);

        example.executeTasks();

    }

}
 

<?xml version="1.0" encoding="UTF-8"?>

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

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

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

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

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

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

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd

       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    

    <bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

          <property name="corePoolSize" value="5"/>

          <property name="maxPoolSize" value="10"/>

          <property name="queueCapacity" value="25"/>

    </bean>

          

    <bean id="syncTaskExecutor" class="org.springframework.core.task.SyncTaskExecutor"/>

    

    <bean id="timerTaskExecutor" class="org.springframework.scheduling.timer.TimerTaskExecutor">

        <property name="delay" value="3000"/>

        <property name="timer" ref="timer"/>

    </bean>

    

    <bean id="timer" class="java.util.Timer"/>

    

    <bean id="logger" class="springexec.Logger"/>
 

</beans>

Open in new window

0
Comment
Question by:apollonius_tyana
  • 3
7 Comments
 
LVL 12

Expert Comment

by:marklorenz
ID: 22766797
If you mean Spring AOP, you cannot use it to work outside the Spring framework/container.  In this case, you could consider using AspectJ.
0
 
LVL 1

Author Comment

by:apollonius_tyana
ID: 22774378
By outside I mean objects that have to be instantiated within new() rather than getBean(). If you look at the example you'll see there's a Runnable class which is instantiated by the Executor. What I need is a logging aspect around the Runnable object. How would I do it in Spring configuration?
0
 
LVL 1

Author Comment

by:apollonius_tyana
ID: 23049174
Do not close the question. No people in EE site able to anwer the question does not mean the question is abondened.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 23096641
Do you have to use spring logger?

I would do something like this if I am understanding your requirements correctly:

public void run() {
        // log start of run here
        // http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/Logger.html

        while( count > 0 ){
            count--;
            if (count == 0){
                System.out.println(name + " says 'Hello World!'");
            } else {
                System.out.println(name + " : " + count);
                Thread.yield();
            }
        }

        // log end of run here    
    }
0
 
LVL 1

Accepted Solution

by:
apollonius_tyana earned 0 total points
ID: 23243474
Actually the issue is not the logging itself. I was wondering if I could inject AOP from Spring configuration. But it seems like it's not possible unless I somehow extend or modify Spring TaskExecutor, which means I have to deal with container issues which I wanted to avoid.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 47
allStar challenge 1 41
endX challenge 2 50
Eclipse IDE - Cannot copy/paste from console output 8 70
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now