co.com.bancolombia.commons.jms.utils.MQQueuesContainerImp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-jms-utils Show documentation
Show all versions of commons-jms-utils Show documentation
A performant abstraction on top of JMS
The newest version!
package co.com.bancolombia.commons.jms.utils;
import co.com.bancolombia.commons.jms.api.MQQueuesContainer;
import jakarta.jms.Queue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MQQueuesContainerImp implements MQQueuesContainer {
private final Map tempQueues = new ConcurrentHashMap<>();
private final Map> tempQueueGroups = new ConcurrentHashMap<>();
public void registerQueue(String alias, Queue queue) {
tempQueues.put(alias, queue);
}
@Override
public void registerToQueueGroup(String groupId, Queue queue) {
tempQueueGroups.computeIfAbsent(groupId, ignored -> Collections.synchronizedList(new ArrayList<>()))
.add(queue);
}
@Override
public void unregisterFromQueueGroup(String groupId, Queue queue) {
List group = tempQueueGroups.get(groupId);
if (group != null) {
group.remove(queue);
}
}
public Queue get(String alias) {
List queues = tempQueueGroups.get(alias);
if (queues != null) {
int selectIndex = (int) (System.currentTimeMillis() % queues.size());
return queues.get(selectIndex);
} else {
return tempQueues.get(alias);
}
}
@Override
public String toString() {
return "MQQueuesContainerImp{" +
"tempQueues=" + tempQueues +
", tempQueueGroups=" + tempQueueGroups +
'}';
}
}