Content of file MultiQueue.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>MultiQueue.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">TheseFoolishThings :: MessageBus :: Spring</a> > <a href="../index.html" class="el_bundle">it-tidalwave-messagebus</a> > <a href="index.source.html" class="el_package">it.tidalwave.messagebus.spi</a> > <span class="el_source">MultiQueue.java</span></div><h1>MultiQueue.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* TheseFoolishThings: Miscellaneous utilities
* http://tidalwave.it/projects/thesefoolishthings
*
* Copyright (C) 2009 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* 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.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
* git clone https://github.com/tidalwave-it/thesefoolishthings-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.messagebus.spi;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Queue;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.LinkedBlockingQueue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L49">@Slf4j</span>
<span class="nc" id="L50">public class MultiQueue </span>
{
<span class="nc bnc" id="L52" title="All 4 branches missed."> @RequiredArgsConstructor @Getter @ToString</span>
static class TopicAndMessage<TOPIC>
{
@Nonnull
<span class="nc" id="L56"> private final Class<TOPIC> topic;</span>
@Nonnull
<span class="nc" id="L59"> private final TOPIC message;</span>
}
<span class="nc" id="L62"> private final ConcurrentNavigableMap<Class<?>, Queue<?>> queueMapByTopic =</span>
<span class="nc" id="L63"> new ConcurrentSkipListMap<>(Comparator.comparing(Class::getName));</span>
<span class="nc" id="L65"> private Class<?> latestSentTopic = null;</span>
/*******************************************************************************************************************
*
* Adds a message of the given topic to this queue and issues a notification.
*
* @param <TOPIC> the static type of the message
* @param topic the dynamic type of the message
* @param message the message
*
******************************************************************************************************************/
public synchronized <TOPIC> void add (@Nonnull final Class<TOPIC> topic, @Nonnull final TOPIC message)
{
<span class="nc" id="L78"> getQueue(topic).add(message);</span>
<span class="nc" id="L79"> notifyAll();</span>
<span class="nc" id="L80"> }</span>
/*******************************************************************************************************************
*
* Removes and returns the next pair (topic, message) from the queue. Blocks until one is available.
*
* @param <TOPIC> the static type of the topic
* @return the topic and message
* @throws InterruptedException if interrupted while waiting
*
******************************************************************************************************************/
@Nonnull
public synchronized <TOPIC> TopicAndMessage<TOPIC> remove()
throws InterruptedException
{
for (;;)
{
<span class="nc bnc" id="L97" title="All 2 branches missed."> for (final Class<?> topic : reorderedTopics())</span>
{
<span class="nc" id="L99"> final Queue<?> queue = queueMapByTopic.get(topic);</span>
<span class="nc" id="L100"> final Object message = queue.poll();</span>
<span class="nc bnc" id="L102" title="All 2 branches missed."> if (message != null)</span>
{
<span class="nc" id="L104"> latestSentTopic = topic;</span>
<span class="nc bnc" id="L106" title="All 2 branches missed."> if (log.isTraceEnabled())</span>
{
<span class="nc" id="L108"> log.trace("stats {}", stats());</span>
}
<span class="nc" id="L111"> return new TopicAndMessage<>((Class<TOPIC>)topic, (TOPIC)message); </span>
}
<span class="nc" id="L113"> }</span>
<span class="nc bnc" id="L115" title="All 2 branches missed."> if (log.isTraceEnabled())</span>
{
<span class="nc" id="L117"> log.trace("all queues empty; stats {}", stats());</span>
}
<span class="nc" id="L120"> wait(); </span>
}
}
/*******************************************************************************************************************
*
* Returns the list of topics reordered, so it starts just after latestSentTopic and wraps around.
*
******************************************************************************************************************/
@Nonnull
private List<Class<?>> reorderedTopics()
{
<span class="nc" id="L132"> final NavigableSet<Class<?>> keySet = queueMapByTopic.navigableKeySet();</span>
<span class="nc" id="L133"> final List<Class<?>> scanSet = new ArrayList<>();</span>
<span class="nc bnc" id="L135" title="All 2 branches missed."> if (latestSentTopic == null)</span>
{
<span class="nc" id="L137"> scanSet.addAll(keySet);</span>
}
else
{
<span class="nc" id="L141"> scanSet.addAll(keySet.subSet(latestSentTopic, false, keySet.last(), true));</span>
<span class="nc" id="L142"> scanSet.addAll(keySet.subSet(keySet.first(), true, latestSentTopic, true));</span>
}
<span class="nc" id="L145"> return scanSet;</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private synchronized String stats()
{
<span class="nc" id="L154"> final StringBuilder b = new StringBuilder();</span>
<span class="nc" id="L155"> String separator = "";</span>
<span class="nc bnc" id="L157" title="All 2 branches missed."> for (final Map.Entry<Class<?>, Queue<?>> e : queueMapByTopic.entrySet())</span>
{
<span class="nc" id="L159"> b.append(separator).append(String.format("%s[%s]: %d", </span>
<span class="nc bnc" id="L160" title="All 2 branches missed."> e.getKey().getSimpleName(), e.getKey().equals(latestSentTopic) ? "X" : " ", e.getValue().size()));</span>
<span class="nc" id="L161"> separator = ", ";</span>
<span class="nc" id="L162"> }</span>
<span class="nc" id="L164"> return b.toString();</span>
}
/*******************************************************************************************************************
*
* Returns the queue associated to a given topic. The queue is created if the topic is new.
*
* @param topic the topic
* @return the queue
*
******************************************************************************************************************/
@Nonnull
private synchronized <TOPIC> Queue<TOPIC> getQueue (@Nonnull final Class<TOPIC> topic)
{
// TODO Java 8 would make this easier
| Java 8 would make this easier | |
<span class="nc" id="L179"> Queue<TOPIC> queue = (Queue<TOPIC>)queueMapByTopic.get(topic);</span>
<span class="nc bnc" id="L181" title="All 2 branches missed."> if (queue == null)</span>
{
<span class="nc" id="L183"> queue = new LinkedBlockingQueue<>();</span>
<span class="nc" id="L184"> queueMapByTopic.put(topic, queue);</span>
}
<span class="nc" id="L187"> return queue;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.6.202009150832</span></div></body></html>