use of org.apache.samza.application.StreamApplication 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.application.StreamApplication in project samza by apache.
the class LocalContainerRunner method main.
public static void main(String[] args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new SamzaContainerExceptionHandler(() -> {
log.info("Exiting process now.");
System.exit(1);
}));
String containerId = System.getenv(ShellCommandConfig.ENV_CONTAINER_ID());
log.info(String.format("Got container ID: %s", containerId));
String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL());
log.info(String.format("Got coordinator URL: %s", coordinatorUrl));
int delay = new Random().nextInt(SamzaContainer.DEFAULT_READ_JOBMODEL_DELAY_MS()) + 1;
JobModel jobModel = SamzaContainer.readJobModel(coordinatorUrl, delay);
Config config = jobModel.getConfig();
JobConfig jobConfig = new JobConfig(config);
if (jobConfig.getName().isEmpty()) {
throw new SamzaException("can not find the job name");
}
String jobName = jobConfig.getName().get();
String jobId = jobConfig.getJobId().getOrElse(ScalaToJavaUtils.defaultValue("1"));
MDC.put("containerName", "samza-container-" + containerId);
MDC.put("jobName", jobName);
MDC.put("jobId", jobId);
StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
LocalContainerRunner localContainerRunner = new LocalContainerRunner(jobModel, containerId);
localContainerRunner.run(streamApp);
}
use of org.apache.samza.application.StreamApplication 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.application.StreamApplication in project samza by apache.
the class ApplicationRunnerMain method main.
public static void main(String[] args) throws Exception {
ApplicationRunnerCommandLine cmdLine = new ApplicationRunnerCommandLine();
OptionSet options = cmdLine.parser().parse(args);
Config orgConfig = cmdLine.loadConfig(options);
Config config = Util.rewriteConfig(orgConfig);
ApplicationRunnerOperation op = cmdLine.getOperation(options);
if (config.containsKey(STREAM_APPLICATION_CLASS_CONFIG)) {
ApplicationRunner runner = ApplicationRunner.fromConfig(config);
StreamApplication app = (StreamApplication) Class.forName(config.get(STREAM_APPLICATION_CLASS_CONFIG)).newInstance();
switch(op) {
case RUN:
runner.run(app);
break;
case KILL:
runner.kill(app);
break;
case STATUS:
System.out.println(runner.status(app));
break;
default:
throw new IllegalArgumentException("Unrecognized operation: " + op);
}
} else {
JobRunner$.MODULE$.main(args);
}
}
Aggregations