Package org.osforce.spring4me.task

Source Code of org.osforce.spring4me.task.AbstractTask

package org.osforce.spring4me.task;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
*
* @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
* @since 0.1.0
* @create May 16, 2011 - 3:54:00 PM
* <a href="http://www.opensourceforce.org">开源力量</a>
*/
public abstract class AbstractTask implements Task {

  protected PlatformTransactionManager transactionManager;

  public AbstractTask() {
  }

  @Autowired
  public void setTransactionManager(
      PlatformTransactionManager transactionManager) {
    this.transactionManager = transactionManager;
  }

  @Async
  public void doAsyncTask(final Map<Object, Object> context) {
    new TransactionTemplate(transactionManager)
    .execute(new TransactionCallbackWithoutResult(){
      @Override
      protected void doInTransactionWithoutResult(TransactionStatus status) {
          try {
            doTask(context);
          } catch (Exception e) {
            e.printStackTrace();
          }
      }
    });
  };

  public void doSyncTask(final Map<Object, Object> context) {
    try {
      doTask(context);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  protected abstract void doTask(final Map<Object, Object> context) throws Exception;

}
TOP

Related Classes of org.osforce.spring4me.task.AbstractTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.