珍妮,非常感谢!这种方式更加优雅并且有效......这是我所做的:
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class ContainerApplication {
@Autowired
private MyContainerImplementation myContainerImplementation;
@Bean(name="yarnContainerClass")
public Class<? extends YarnContainer> getYarnContainerClass() {
return MyContainerImplementation.class;
}
@Bean(name="yarnContainerRef")
public MyContainerImplementation getYarnContainerRef() {
return myContainerImplementation;
}
@Bean(name="customContainerClass")
public String getCustomContainerClass() {
return "myhadoop.yarn.container.custom.MyContainerImplementation";
}
public static void main(String[] args) {
SpringApplication.run(ContainerApplication.class, args);
}
}
正如您所指出的,我将 MyContainerImplementation 添加到 yml 中,并且我的容器实现是由应用程序主启动的,而我没有手动运行 run 方法,因为我在 hadoop 日志中看到以下行:
LifecycleObjectSupport:started
myhadoop.yarn.container.custom.MyContainerImplementation@5e2cd950
.
.
LifecycleObjectSupport: stopped
myhadoop.yarn.container.custom.MyContainerImplementation@5e2cd950
无论如何,我还有一个问题。我想测试 ContainerStateListener 和 YarnPublisher 之类的低级纱线,但根本没有调用它们..:-( 这是我的测试自定义容器:
@Component
public class MyContainerImplementation extends AbstractYarnContainer {
private static final Log log = LogFactory.getLog(MyContainerImplementation.class);
public MyContainerImplementation() {
super();
log.info("...Initializing yarn MyContainerImplementation....");
this.setYarnEventPublisher(new DefaultYarnEventPublisher() {
@Override
public void publishContainerAllocated(Object source, Container container) {
super.publishContainerAllocated(source, container);
log.info("Yarn container allocated: "+container.getResource().getMemory());
}
});
this.addContainerStateListener(new ContainerStateListener() {
@Override
public void state(ContainerState state, Object exit) {
switch(state) {
case COMPLETED: {
log.info("...Container started successfully!...");
break;
}
case FAILED: {
log.info("...Starting of container failed!...");
break;
}
default: {
log.info("Unexpected container state...exiting!...");
}
}
}
});
}
public void runInternal() {
log.info("...Running internal method...");
}
}
我是否需要添加额外的配置才能使 ContainerStateListener 和 YarnPublisher 工作?