Search in sources :

Example 1 with SamzaContainerExceptionHandler

use of org.apache.samza.container.SamzaContainerExceptionHandler 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);
}
Also used : Random(java.util.Random) JobConfig(org.apache.samza.config.JobConfig) ShellCommandConfig(org.apache.samza.config.ShellCommandConfig) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) SamzaContainerExceptionHandler(org.apache.samza.container.SamzaContainerExceptionHandler) JobModel(org.apache.samza.job.model.JobModel) SamzaException(org.apache.samza.SamzaException) JobConfig(org.apache.samza.config.JobConfig)

Aggregations

Random (java.util.Random)1 SamzaException (org.apache.samza.SamzaException)1 StreamApplication (org.apache.samza.application.StreamApplication)1 Config (org.apache.samza.config.Config)1 JobConfig (org.apache.samza.config.JobConfig)1 ShellCommandConfig (org.apache.samza.config.ShellCommandConfig)1 SamzaContainerExceptionHandler (org.apache.samza.container.SamzaContainerExceptionHandler)1 JobModel (org.apache.samza.job.model.JobModel)1