Announcement Announcement Module
Collapse
No announcement yet.
wrapping threads in transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • wrapping threads in transaction

    Hello,

    i need to use ThreadPoolExecutor to run different kinds of tasks.
    Each task should run in newly created Thread, be independent, and be wrapped by TransactionInterceptor.

    ie
    Runnable task = new Runnable(){
    private Thread thread = new Thread();

    public void start(){
    if (thread != null){
    thread.start();
    }
    }

    public void run() {
    int i = 0;
    while(i < 5){
    try{
    thread.sleep(5000);
    }catch(InterruptedException iE){
    }
    doSmthWithDB();
    i++;
    }
    }
    };

    ThreadPoolExecutor poolExecutor = (ThreadPoolExecutor)context.getBean("threadPoolExe cutor");
    poolExecutor.execute(task);


    ------------------------------my servicesFunctions.xml:

    .
    .
    .
    <bean id="threadFactory" class="ru.rbc.mediastream.thread.TransactedThreadF actory"/>

    <bean name="threadPoolExecutor" class="java.util.concurrent.ThreadPoolExecutor">
    <constructor-arg index="0" value="5"/>
    <constructor-arg index="1" value="10"/>
    <constructor-arg index="2" value="10"/>
    <constructor-arg index="3">
    <bean id="java.util.concurrent.TimeUnit.SECONDS"
    class="org.springframework.beans.factory.config.Fi eldRetrievingFactoryBean"/>
    </constructor-arg>
    <constructor-arg index="4">
    <bean class="java.util.concurrent.ArrayBlockingQueue">
    <constructor-arg index="0" value="1"/>
    </bean>
    </constructor-arg>

    <property name="threadFactory" ref="threadFactory"/>
    </bean>


    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributeSource">
    <ref bean="transactionAttributeSource"/>
    </property>
    </bean>
    .
    .
    .
    -----------------------------------TransactedThreadFactory.java
    public class TransactedThreadFactory implements ThreadFactory, ApplicationContextAware{
    private ApplicationContext applicationContext;

    public Thread newThread(Runnable r){

    // BUT WHAT SHOULD I WRITE HERE? i may only guess

    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
    this.applicationContext = applicationContext;
    }
    }



    any positive tries will be very much appreciated

  • #2
    We have a very similar setup with one difference: What you implement in run() we have in an extra component (interface Task). The setup of the tasks is delegated to Spring and they are retrieved via
    Code:
    Task task = (Task)context.getBean("myTask");
    as well.

    Now you need a wrapper for those tasks implementing the Runnable interface. In its run() method it has to call the executing method of the Task interface, e.g. execute():

    Code:
    public class TaskWrapper implements Runnable {
    
      private final Task task;
    
      public TaskWrapper(Task task) {
        this.task = task;
      }
    
      public void run() {
        task.execute();
      }
    
    }
    For the transactional behaviour it is now easy to create transactional proxies for the tasks instead of for the threads.

    Jörg
    Last edited by Jörg Heinicke; Aug 17th, 2006, 11:34 AM.

    Comment


    • #3
      Thanks for reply
      that solved my problem. so simple.
      i thought about anything, but not that kind.

      Comment

      Working...
      X