net.logstash.logback.appender.WaitStrategyFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logstash-logback-encoder Show documentation
Show all versions of logstash-logback-encoder Show documentation
Provides logback encoders, layouts, and appenders to log in JSON and other formats supported by Jackson
/*
* Copyright 2013-2022 the original author or authors.
*
* 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.logstash.logback.appender;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import net.logstash.logback.encoder.com.lmax.disruptor.BlockingWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.BusySpinWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.LiteBlockingWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.LiteTimeoutBlockingWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.PhasedBackoffWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.SleepingWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.TimeoutBlockingWaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.WaitStrategy;
import net.logstash.logback.encoder.com.lmax.disruptor.YieldingWaitStrategy;
/**
* Creates {@link WaitStrategy} objects from strings.
*/
public class WaitStrategyFactory {
private static final char PARAM_END_CHAR = '}';
private static final char PARAM_START_CHAR = '{';
private static final char PARAM_SEPARATOR_CHAR = ',';
/**
* Creates a {@link WaitStrategy} from a string.
*
* The following strategies are supported:
*
* blocking
- {@link BlockingWaitStrategy}
* busySpin
- {@link BusySpinWaitStrategy}
* liteBlocking
- {@link LiteBlockingWaitStrategy}
* sleeping{retries,sleepTimeNs}
- {@link SleepingWaitStrategy}
* - retries
an integer number of times to spin before sleeping. (default = 200)
* sleepTimeNs
nanosecond time to sleep each iteration after spinning (default = 100)
*
* yielding
- {@link YieldingWaitStrategy}
* phasedBackoff{spinTimeout,yieldTimeout,timeUnit,fallackStrategy}
- {@link PhasedBackoffWaitStrategy}
* - spinTimeout
and yieldTimeout
are long values.
* timeUnit
is a string name of one of the {@link TimeUnit} values.
* fallbackStrategy
is a wait strategy string (e.g. blocking
).
*
* timeoutBlocking{timeout,timeUnit}
- {@link TimeoutBlockingWaitStrategy}
* - timeout
is a long value.
* timeUnit
is a string name of one of the {@link TimeUnit} values.
*
* liteTimeoutBlocking{timeout,timeUnit}
- {@link LiteTimeoutBlockingWaitStrategy}
* - timeout
is a long value.
* timeUnit
is a string name of one of the {@link TimeUnit} values.
*
*
*
* @param waitStrategyType the name of the desired wait strategy
* @return a {@link WaitStrategy} instance or {@code null} if the supplied name is {@code null} or empty
* @throws IllegalArgumentException if an unknown wait strategy type is given, or the parameters are unable to be parsed.
*/
public static WaitStrategy createWaitStrategyFromString(String waitStrategyType) {
if (waitStrategyType == null) {
return null;
}
waitStrategyType = waitStrategyType.trim().toLowerCase();
if (waitStrategyType.isEmpty()) {
return null;
}
if (waitStrategyType.equals("blocking")) {
return new BlockingWaitStrategy();
}
if (waitStrategyType.equals("busyspin")) {
return new BusySpinWaitStrategy();
}
if (waitStrategyType.equals("liteblocking")) {
return new LiteBlockingWaitStrategy();
}
if (waitStrategyType.startsWith("sleeping")) {
if (waitStrategyType.equals("sleeping")) {
return new SleepingWaitStrategy();
} else {
List