All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.scattersphere.job.example.Counter Maven / Gradle / Ivy

The newest version!
/*
 * Scattersphere
 * Copyright 2014-2015, Scattersphere Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.scattersphere.job.example;

import net.scattersphere.job.AbstractJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is a simple counter job.  It counts to a certain number, pausing 1 second between numbers generated.
 *
 * Created by kenji on 11/26/14.
 */
public class Counter extends AbstractJob {

	private final Logger LOG = LoggerFactory.getLogger(Counter.class);

	private static final int DEFAULT_COUNT = 60;
	private static final int ONE_SECOND = 1000;

	public Counter() {
		LOG.debug("Counter job instantiated.");
	}

    @Override
    public String getDescription() {
        return "Counts from 1 to (count), pausing 1 second between.";
    }

	public void run()  {
		String timezone = null;
		String[] arguments = getArguments();
		int count = DEFAULT_COUNT;

		if (arguments != null && arguments.length > 0) {
			count = Integer.parseInt(arguments[0]);
		}

		for(int i = 0; i < count; i++) {
			try {
				Thread.sleep(ONE_SECOND);
				writeToStream(("Counter: " + (i + 1)).getBytes());
			} catch(Exception ex) {
				LOG.debug("Interrupted thread, exiting.");
				return;
			}
		}

		writeToStream("Counter job complete.".getBytes());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy