com.ottogroup.bi.asap.component.strategy.SleepingWaitStrategy.java Source code

Java tutorial

Introduction

Here is the source code for com.ottogroup.bi.asap.component.strategy.SleepingWaitStrategy.java

Source

/**
 * Copyright 2014 Otto (GmbH & Co KG)
 *
 * 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 com.ottogroup.bi.asap.component.strategy;

import java.util.Properties;
import java.util.concurrent.locks.LockSupport;

import org.apache.commons.lang3.StringUtils;

import com.ottogroup.bi.asap.component.ComponentType;
import com.ottogroup.bi.asap.component.annotation.AsapComponent;
import com.ottogroup.bi.asap.exception.RequiredInputMissingException;
import com.ottogroup.bi.asap.mailbox.Mailbox;

/**
 * Follows an approach implemented as beta in {@linkplain https://github.com/LMAX-Exchange/disruptor}. First it
 * waits by decrementing a counter. If that reaches a boundary it pars ({@link LockSupport#parkNanos(long)) the 
 * thread for a fixed time. The strategy is applied only if the referenced {@link Mailbox} shows no elements  
 * @author mnxfst
 * @since Dec 16, 2014
 */
@AsapComponent(type = ComponentType.MESSAGE_WAIT_STRATEGY, name = "sleepingMessageWaitStrategy", description = "Waits for messages - first by active waiting, then by sleeping", version = "0.0.1")
public class SleepingWaitStrategy implements MessageWaitStrategy {

    public static final String CFG_COMPONENT_ID = "id";
    /** number of retries while checking the associated mailbox for elements */
    public static final String CFG_RETRIES = "retries";
    private static final int DEFAULT_RETRIES = 200;

    private String id;
    private int retries = DEFAULT_RETRIES;
    private Mailbox mailbox;

    /**
     * @see com.ottogroup.bi.asap.component.Component#init(java.util.Properties)
     */
    public void init(Properties settings) throws RequiredInputMissingException {

        this.id = settings.getProperty(CFG_COMPONENT_ID);
        if (StringUtils.isBlank(this.id))
            throw new RequiredInputMissingException("Missing required input for '" + CFG_COMPONENT_ID + "'");

        try {
            retries = Integer.parseInt(settings.getProperty(CFG_RETRIES));
        } catch (Exception e) {
            // assign default setting
            this.retries = DEFAULT_RETRIES;
        }
    }

    /**
     * @see com.ottogroup.bi.asap.component.strategy.MessageWaitStrategy#waitFor()
     */
    public void waitFor() throws InterruptedException {
        int counter = retries;
        while (counter > 0 && mailbox.size() < 1) {
            counter = applyWaitMethod(counter);
        }
    }

    /**
     * Waiting .... 
     * @param counter
     * @return
     */
    private int applyWaitMethod(int counter) {
        if (counter > 100) {
            --counter;
        } else {
            LockSupport.parkNanos(1L);
        }
        return counter;
    }

    /**
     * @see com.ottogroup.bi.asap.component.Component#shutdown()
     */
    public boolean shutdown() {
        return true;
    }

    /**
     * @see com.ottogroup.bi.asap.component.strategy.MessageWaitStrategy#setMailbox(com.ottogroup.bi.asap.mailbox.Mailbox)
     */
    public void setMailbox(Mailbox mailbox) {
        this.mailbox = mailbox;
    }

    /**
     * @see com.ottogroup.bi.asap.component.Component#getId()
     */
    public String getId() {
        return this.id;
    }

    /**
     * @see com.ottogroup.bi.asap.component.Component#setId(java.lang.String)
     */
    public void setId(String id) {
        this.id = id;
    }

}