Skip to content

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> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-messagebus</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.messagebus.spi</a> &gt; <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 &quot;License&quot;); 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 &quot;AS IS&quot; 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&lt;TOPIC&gt;
      {
        @Nonnull
<span class="nc" id="L56">        private final Class&lt;TOPIC&gt; topic;</span>

        @Nonnull
<span class="nc" id="L59">        private final TOPIC message;</span>
      }
     
<span class="nc" id="L62">    private final ConcurrentNavigableMap&lt;Class&lt;?&gt;, Queue&lt;?&gt;&gt; queueMapByTopic =</span>
<span class="nc" id="L63">            new ConcurrentSkipListMap&lt;&gt;(Comparator.comparing(Class::getName));</span>
    
<span class="nc" id="L65">    private Class&lt;?&gt; latestSentTopic = null;</span>
    
    /*******************************************************************************************************************
     *
     * Adds a message of the given topic to this queue and issues a notification.
     *
     * @param   &lt;TOPIC&gt;   the static type of the message
     * @param   topic     the dynamic type of the message
     * @param   message   the message
     *
     ******************************************************************************************************************/
    public synchronized &lt;TOPIC&gt; void add (@Nonnull final Class&lt;TOPIC&gt; 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   &lt;TOPIC&gt;                 the static type of the topic
     * @return                          the topic and message
     * @throws  InterruptedException    if interrupted while waiting
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized &lt;TOPIC&gt; TopicAndMessage&lt;TOPIC&gt; remove() 
      throws InterruptedException
      {
        for (;;)
          {
<span class="nc bnc" id="L97" title="All 2 branches missed.">            for (final Class&lt;?&gt; topic : reorderedTopics())</span>
              {
<span class="nc" id="L99">                final Queue&lt;?&gt; 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(&quot;stats {}&quot;, stats());</span>
                      }
                    
<span class="nc" id="L111">                    return new TopicAndMessage&lt;&gt;((Class&lt;TOPIC&gt;)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(&quot;all queues empty; stats {}&quot;, 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&lt;Class&lt;?&gt;&gt; reorderedTopics() 
      {
<span class="nc" id="L132">        final NavigableSet&lt;Class&lt;?&gt;&gt; keySet = queueMapByTopic.navigableKeySet();</span>
<span class="nc" id="L133">        final List&lt;Class&lt;?&gt;&gt; scanSet = new ArrayList&lt;&gt;();</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 = &quot;&quot;;</span>
        
<span class="nc bnc" id="L157" title="All 2 branches missed.">        for (final Map.Entry&lt;Class&lt;?&gt;, Queue&lt;?&gt;&gt; e : queueMapByTopic.entrySet())</span>
          {
<span class="nc" id="L159">            b.append(separator).append(String.format(&quot;%s[%s]: %d&quot;, </span>
<span class="nc bnc" id="L160" title="All 2 branches missed.">                    e.getKey().getSimpleName(), e.getKey().equals(latestSentTopic) ? &quot;X&quot; : &quot; &quot;, e.getValue().size()));</span>
<span class="nc" id="L161">            separator = &quot;, &quot;;</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 &lt;TOPIC&gt; Queue&lt;TOPIC&gt; getQueue (@Nonnull final Class&lt;TOPIC&gt; topic)
      {
        // TODO Java 8 would make this easier
Java 8 would make this easier
<span class="nc" id="L179"> Queue&lt;TOPIC&gt; queue = (Queue&lt;TOPIC&gt;)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&lt;&gt;();</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>