Search in sources :

Example 1 with TaskConfig

use of org.apache.samza.config.TaskConfig in project samza by apache.

the class TaskFactoryUtil method createStreamApplication.

/**
   * Returns {@link StreamApplication} if it's configured, otherwise null.
   * @param config Config
   * throws {@link ConfigException} if there is misconfiguration of StreamApp.
   * @return {@link StreamApplication} instance
   */
public static StreamApplication createStreamApplication(Config config) {
    ApplicationConfig appConfig = new ApplicationConfig(config);
    if (appConfig.getAppClass() != null && !appConfig.getAppClass().isEmpty()) {
        TaskConfig taskConfig = new TaskConfig(config);
        if (taskConfig.getTaskClass() != null && !taskConfig.getTaskClass().isEmpty()) {
            throw new ConfigException("High level StreamApplication API cannot be used together with low-level API using task.class.");
        }
        String appClassName = appConfig.getAppClass();
        try {
            Class<?> builderClass = Class.forName(appClassName);
            return (StreamApplication) builderClass.newInstance();
        } catch (Throwable t) {
            String errorMsg = String.format("Failed to create StreamApplication class from the config. %s = %s", ApplicationConfig.APP_CLASS, appConfig.getAppClass());
            log.error(errorMsg, t);
            throw new ConfigException(errorMsg, t);
        }
    } else {
        return null;
    }
}
Also used : ApplicationConfig(org.apache.samza.config.ApplicationConfig) StreamApplication(org.apache.samza.application.StreamApplication) TaskConfig(org.apache.samza.config.TaskConfig) ConfigException(org.apache.samza.config.ConfigException)

Example 2 with TaskConfig

use of org.apache.samza.config.TaskConfig in project samza by apache.

the class TaskFactoryUtil method fromTaskClassConfig.

/**
   * Create {@link StreamTaskFactory} or {@link AsyncStreamTaskFactory} based on the configured task.class.
   * @param config the {@link Config}
   * @return task factory instance
   */
private static Object fromTaskClassConfig(Config config) {
    // if there is configuration to set the job w/ a specific type of task, instantiate the corresponding task factory
    String taskClassName = new TaskConfig(config).getTaskClass().getOrElse(new AbstractFunction0<String>() {

        @Override
        public String apply() {
            throw new ConfigException("There is no task class defined in the configuration. Failed to create a valid TaskFactory");
        }
    });
    log.info("Got task class name: {}", taskClassName);
    boolean isAsyncTaskClass;
    try {
        isAsyncTaskClass = AsyncStreamTask.class.isAssignableFrom(Class.forName(taskClassName));
    } catch (Throwable t) {
        throw new ConfigException(String.format("Invalid configuration for AsyncStreamTask class: %s", taskClassName), t);
    }
    if (isAsyncTaskClass) {
        return new AsyncStreamTaskFactory() {

            @Override
            public AsyncStreamTask createInstance() {
                try {
                    return (AsyncStreamTask) Class.forName(taskClassName).newInstance();
                } catch (Throwable t) {
                    log.error("Error loading AsyncStreamTask class: {}. error: {}", taskClassName, t);
                    throw new SamzaException(String.format("Error loading AsyncStreamTask class: %s", taskClassName), t);
                }
            }
        };
    }
    return new StreamTaskFactory() {

        @Override
        public StreamTask createInstance() {
            try {
                return (StreamTask) Class.forName(taskClassName).newInstance();
            } catch (Throwable t) {
                log.error("Error loading StreamTask class: {}. error: {}", taskClassName, t);
                throw new SamzaException(String.format("Error loading StreamTask class: %s", taskClassName), t);
            }
        }
    };
}
Also used : TaskConfig(org.apache.samza.config.TaskConfig) ConfigException(org.apache.samza.config.ConfigException) SamzaException(org.apache.samza.SamzaException)

Aggregations

ConfigException (org.apache.samza.config.ConfigException)2 TaskConfig (org.apache.samza.config.TaskConfig)2 SamzaException (org.apache.samza.SamzaException)1 StreamApplication (org.apache.samza.application.StreamApplication)1 ApplicationConfig (org.apache.samza.config.ApplicationConfig)1