1

我正在用 spring-batch 编写一个应用程序/测试,其中包含 hadoop mapreduce 作业。

对于 spring-batch 容器,一切都很好:我在执行单元测试之前加载了 Applicationcontext 特定的配置。

(来自 spring-data hadoop 示例的代码)http://static.springsource.org/spring-hadoop/docs/current/reference/html/batch-wordcount.html

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/launch-context.xml")
public class WordCountWorkflowTest {

      @Autowired
      private ApplicationContext ctx;

当我需要从 main 方法加载 Applicationcontext 时,我发现建议通过以下方式执行此操作:

public class Main {
      public static void main(String[] arguments) {
          ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
      }
}

现在我正在寻找一种从 Hadoop Mapper 类中启动 Applicationcontext 的方法。我的问题:类本身将注入 bean。我认为通常你有另一个类或对象为你启动 Applicationcontext。

以下方法似乎对我有用(但我不知道是否有一些陷阱或更好的方法,例如带有注释):

public class ToHBaseMapper extends
        Mapper<LongWritable, Text, Text, IntWritable> {

    private static final String[] CONFIGS = new String[] { "/mapReduce-context.xml" };

    private static AbstractApplicationContext ctx;

    // Executed when class is loaded
    static {
        ctx = new ClassPathXmlApplicationContext(CONFIGS);
        ctx.registerShutdownHook();
    }
    ...

TIA,R

4

0 回答 0