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
}
}
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);
}
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);
}
}
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;
}
}
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);
}
}
};
}
Aggregations