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

net.scattersphere.job.example.Sleep 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;

import java.util.Date;

/**
 * This test job is used to test the queueing mechanism in the system.  By default, the job waits 30 seconds, but this can be
 * overridden in the parameters sent to the job at start time.
 *
 * Created by kenji on 12/15/14.
 */
public class Sleep extends AbstractJob {

    private static final int DEFAULT_SLEEPTIME = 30;

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

    @Override
    public String getDescription() {
        return "Sleeps for a determinant amount of time, default 30 seconds.";
    }

    public void run() {
        int sleepTime = DEFAULT_SLEEPTIME;
        String[] arguments = getArguments();

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

        if (sleepTime < 0) {
            throw new IllegalArgumentException("Sleep time must be a positive value.");
        }

        writeToStream(("Currently Sleeping for " + sleepTime + " second(s): started at " + (new Date()).toString()).getBytes());

        try {
            Thread.sleep(sleepTime * 1000);
        } catch(Exception ex) {
            throw new RuntimeException(ex);
        }

        writeToStream("Sleep completed.".getBytes());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy