从定期运行的spring任务中提取对象状态

时间:2014-08-14 12:15:01

标签: java spring concurrency

我安排了一个Spring任务调度程序实现,它从数据库中获取更新值对象的列表。因此,每次调用任务时,它都负责获取增量对象。该任务每10分钟左右运行一次。任务运行器的输出用于运行自定义Hadoop作业。该任务通过注释初始化,因此我没有以编程方式明确触发任务。

我的XML配置

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">       
   <context:annotation-config />
    <context:component-scan base-package="com.bofa.ecom.search.scheduler" />
    <bean id="configurationScheduler" class="com.bofa.ecom.search.scheduler.ConfigurationScheduler" scope="prototype"/>
    <task:annotation-driven proxy-target-class="true"/>
    <task:scheduled-tasks>
      <task:scheduled ref="configurationScheduler" method="refreshConfiguration" fixed-rate="5000" />
    </task:scheduled-tasks>
</beans>

我的调度程序界面

package testing;

    import org.apache.hadoop.conf.Configuration;
    import org.springframework.beans.factory.DisposableBean;

    public interface IConfigurationScheduler extends DisposableBean {

      void refreshConfiguration();

      Configuration getConfiguration();

    }

Concrete implementation

package testing;

import org.apache.hadoop.conf.Configuration;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ConfigurationScheduler implements IConfigurationScheduler {


  private static final long serialVersionUID = 1L;

  private final Configuration configuration = new Configuration();

  @Override
  public Configuration getConfiguration() {
    return this.configuration;
  }

  /**
   * The implementation is responsible for executing the following tasks.
   * @return
   */
  // Refresh every 5 seconds after a 2 second delay.
  @Scheduled(fixedDelay=2000)
  @Async
  @Override
  public void refreshConfiguration() {
    // Fetch the data from the database and create a map of configuration metadata.
   // Iterate through map key set and invoke Configuration#set(String, String) within 
   // the loop.
   // **HOW DO I ACCESS THE CONFIGURATION OBJECT?**
  }

  @Override
  public void destroy() throws Exception {
    this.configuration.clear();
  }
}

在我的主要方法中,我做了类似的事情

public static void main(String[] args) {
    System.out.println("test");
    ApplicationContext unusedContext =
          new FileSystemXmlApplicationContext("C:\\Users\\karkuma\\workspace\\test\\src\\conf\\scheduler-conf.xml");

  }

我的问题

  1. 初始化应用程序上下文后立即触发调度程序。 main方法无法访问任务运行器实例。还有其他办法吗?
  2. 我使用Spring自定义事件,侦听器和applciation发布者创建了一个解决方法,但似乎没有任何效果。
  3. 我想我正在寻找的是Callable<T>实现,我可以调用并以固定速率和延迟运行。

0 个答案:

没有答案