001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.examples.hellohttp; 020 021import org.apache.reef.client.DriverConfiguration; 022import org.apache.reef.client.DriverLauncher; 023import org.apache.reef.client.DriverServiceConfiguration; 024import org.apache.reef.client.LauncherStatus; 025import org.apache.reef.runtime.local.client.LocalRuntimeConfiguration; 026import org.apache.reef.tang.Configuration; 027import org.apache.reef.tang.Configurations; 028import org.apache.reef.tang.exceptions.BindException; 029import org.apache.reef.tang.exceptions.InjectionException; 030import org.apache.reef.util.EnvironmentUtils; 031import org.apache.reef.webserver.HttpHandlerConfiguration; 032import org.apache.reef.webserver.HttpServerReefEventHandler; 033import org.apache.reef.webserver.ReefEventStateManager; 034 035import java.util.logging.Logger; 036 037/** 038 * Example to run HelloREEF with a webserver. 039 */ 040public final class HelloREEFHttp { 041 /** 042 * Number of milliseconds to wait for the job to complete. 043 */ 044 public static final int JOB_TIMEOUT = 60 * 1000; // 60 sec. 045 private static final Logger LOG = Logger.getLogger(HelloREEFHttp.class.getName()); 046 047 /** 048 * @return the driver-side configuration to be merged into the DriverConfiguration to enable the HTTP server. 049 */ 050 public static Configuration getHTTPConfiguration() { 051 final Configuration httpHandlerConfiguration = HttpHandlerConfiguration.CONF 052 .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerReefEventHandler.class) 053 .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerShellCmdtHandler.class) 054 .build(); 055 final Configuration driverConfigurationForHttpServer = DriverServiceConfiguration.CONF 056 .set(DriverServiceConfiguration.ON_EVALUATOR_ALLOCATED, ReefEventStateManager.AllocatedEvaluatorStateHandler.class) 057 .set(DriverServiceConfiguration.ON_CONTEXT_ACTIVE, ReefEventStateManager.ActiveContextStateHandler.class) 058 .set(DriverServiceConfiguration.ON_TASK_RUNNING, ReefEventStateManager.TaskRunningStateHandler.class) 059 .set(DriverServiceConfiguration.ON_DRIVER_STARTED, ReefEventStateManager.StartStateHandler.class) 060 .set(DriverServiceConfiguration.ON_DRIVER_STOP, ReefEventStateManager.StopStateHandler.class) 061 .build(); 062 return Configurations.merge(httpHandlerConfiguration, driverConfigurationForHttpServer); 063 } 064 065 /** 066 * @return the configuration of the HelloREEF driver. 067 */ 068 public static Configuration getDriverConfiguration() { 069 return DriverConfiguration.CONF 070 .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(HttpShellJobDriver.class)) 071 .set(DriverConfiguration.DRIVER_IDENTIFIER, "HelloHTTP") 072 .set(DriverConfiguration.ON_DRIVER_STARTED, HttpShellJobDriver.StartHandler.class) 073 .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, HttpShellJobDriver.AllocatedEvaluatorHandler.class) 074 .set(DriverConfiguration.ON_EVALUATOR_FAILED, HttpShellJobDriver.FailedEvaluatorHandler.class) 075 .set(DriverConfiguration.ON_CONTEXT_ACTIVE, HttpShellJobDriver.ActiveContextHandler.class) 076 .set(DriverConfiguration.ON_CONTEXT_CLOSED, HttpShellJobDriver.ClosedContextHandler.class) 077 .set(DriverConfiguration.ON_CONTEXT_FAILED, HttpShellJobDriver.FailedContextHandler.class) 078 .set(DriverConfiguration.ON_TASK_COMPLETED, HttpShellJobDriver.CompletedTaskHandler.class) 079 .set(DriverConfiguration.ON_CLIENT_MESSAGE, HttpShellJobDriver.ClientMessageHandler.class) 080 .set(DriverConfiguration.ON_CLIENT_CLOSED, HttpShellJobDriver.HttpClientCloseHandler.class) 081 .set(DriverConfiguration.ON_DRIVER_STOP, HttpShellJobDriver.StopHandler.class) 082 .build(); 083 } 084 085 /** 086 * Run Hello Reef with merged configuration 087 * 088 * @param runtimeConf 089 * @param timeOut 090 * @return 091 * @throws BindException 092 * @throws InjectionException 093 */ 094 public static LauncherStatus runHelloReef(final Configuration runtimeConf, final int timeOut) 095 throws BindException, InjectionException { 096 final Configuration driverConf = Configurations.merge(HelloREEFHttp.getDriverConfiguration(), getHTTPConfiguration()); 097 return DriverLauncher.getLauncher(runtimeConf).run(driverConf, timeOut); 098 } 099 100 /** 101 * main program 102 * 103 * @param args 104 * @throws InjectionException 105 */ 106 public static void main(final String[] args) throws InjectionException { 107 final Configuration runtimeConfiguration = LocalRuntimeConfiguration.CONF 108 .set(LocalRuntimeConfiguration.NUMBER_OF_THREADS, 3) 109 .build(); 110 final LauncherStatus status = runHelloReef(runtimeConfiguration, HelloREEFHttp.JOB_TIMEOUT); 111 } 112}