Search in sources :

Example 6 with ConfigException

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

the class TestTaskFactoryUtil method testCreateStreamApplicationWithTaskClass.

@Test
public void testCreateStreamApplicationWithTaskClass() throws Exception {
    Config config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
        }
    });
    StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNotNull(streamApp);
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put("task.class", "org.apache.samza.testUtils.TestAsyncStreamTask");
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
        }
    });
    try {
        TaskFactoryUtil.createStreamApplication(config);
        fail("should have failed with invalid config");
    } catch (ConfigException ce) {
    // expected
    }
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put("task.class", "no.such.class");
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
        }
    });
    try {
        TaskFactoryUtil.createStreamApplication(config);
        fail("should have failed with invalid config");
    } catch (ConfigException ce) {
    // expected
    }
}
Also used : HashMap(java.util.HashMap) ApplicationConfig(org.apache.samza.config.ApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) StreamApplication(org.apache.samza.application.StreamApplication) ConfigException(org.apache.samza.config.ConfigException) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Example 7 with ConfigException

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

the class TestTaskFactoryUtil method testCreateStreamApplication.

@Test
public void testCreateStreamApplication() throws Exception {
    Config config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
        }
    });
    StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNotNull(streamApp);
    Object retFactory = TaskFactoryUtil.createTaskFactory(config, streamApp, mockRunner);
    assertTrue(retFactory instanceof StreamTaskFactory);
    assertTrue(((StreamTaskFactory) retFactory).createInstance() instanceof StreamOperatorTask);
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.InvalidStreamApplication");
        }
    });
    try {
        TaskFactoryUtil.createStreamApplication(config);
        fail("Should have failed w/ no.such.class");
    } catch (ConfigException ce) {
    // expected
    }
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "no.such.class");
        }
    });
    try {
        TaskFactoryUtil.createStreamApplication(config);
        fail("Should have failed w/ no.such.class");
    } catch (ConfigException ce) {
    // expected
    }
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "");
        }
    });
    streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNull(streamApp);
    config = new MapConfig(new HashMap<>());
    streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNull(streamApp);
}
Also used : HashMap(java.util.HashMap) ApplicationConfig(org.apache.samza.config.ApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) StreamApplication(org.apache.samza.application.StreamApplication) ConfigException(org.apache.samza.config.ConfigException) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Example 8 with ConfigException

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

the class ClassLoaderHelper method fromClassName.

public static <T> T fromClassName(String className, Class<T> classType) {
    try {
        Class<?> idGeneratorClass = Class.forName(className);
        if (!classType.isAssignableFrom(idGeneratorClass)) {
            throw new ConfigException(String.format("Class %s is not of type %s", className, classType));
        }
        Constructor<?> constructor = idGeneratorClass.getConstructor();
        return (T) constructor.newInstance();
    } catch (Exception e) {
        throw new ConfigException(String.format("Problem in loading %s class %s", classType, className), e);
    }
}
Also used : ConfigException(org.apache.samza.config.ConfigException) ConfigException(org.apache.samza.config.ConfigException)

Example 9 with ConfigException

use of org.apache.samza.config.ConfigException 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 10 with ConfigException

use of org.apache.samza.config.ConfigException 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)10 ApplicationConfig (org.apache.samza.config.ApplicationConfig)7 HashMap (java.util.HashMap)6 Config (org.apache.samza.config.Config)6 MapConfig (org.apache.samza.config.MapConfig)6 Test (org.junit.Test)6 StreamApplication (org.apache.samza.application.StreamApplication)5 TaskConfig (org.apache.samza.config.TaskConfig)2 TestAsyncStreamTask (org.apache.samza.testUtils.TestAsyncStreamTask)2 TestStreamTask (org.apache.samza.testUtils.TestStreamTask)2 Partition (org.apache.samza.Partition)1 SamzaException (org.apache.samza.SamzaException)1 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)1