com.taotao.boot.sms.common.loadbalancer.RoundRobinLoadBalancer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-boot-starter-sms-common Show documentation
Show all versions of taotao-boot-starter-sms-common Show documentation
taotao-boot-starter-sms-common
The newest version!
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* 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
*
* https://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.taotao.boot.sms.common.loadbalancer;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* round robin Load Balancer
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:53:03
*/
public class RoundRobinLoadBalancer extends AbstractLoadBalancer {
private final Lock lock = new ReentrantLock();
private int position = 0;
/** instantiation Load Balancer with CopyOnWriteArrayList */
public RoundRobinLoadBalancer() {
super();
}
/**
* instantiation Load Balancer with appoint list
*
* @param targetList target object list
*/
public RoundRobinLoadBalancer(List> targetList) {
super(targetList);
}
@Override
protected T choose0(List> activeTargetList, C chooseReferenceObject) {
int size = activeTargetList.size();
lock.lock();
try {
if (position >= size) {
position = 0;
}
TargetWrapper wrapper = activeTargetList.get(position);
position++;
return wrapper == null ? null : wrapper.getTarget();
} finally {
lock.unlock();
}
}
}