Skip to content

Content of file SimpleMessageBus.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>SimpleMessageBus.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">SimpleMessageBus.java</span></div><h1>SimpleMessageBus.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 * 
 * These Foolish Things - Miscellaneous utilities
 * http://thesefoolishthings.tidalwave.it - git clone git@bitbucket.org:tidalwave/thesefoolishthings-src.git
 * %%
 * Copyright (C) 2009 - 2021 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.
 * 
 * *********************************************************************************************************************
 * 
 * 
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.messagebus.spi;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import it.tidalwave.messagebus.MessageBus;
import it.tidalwave.messagebus.MessageBus.Listener;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * A partial implementation of {@link MessageBus}.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L52">@ThreadSafe @Slf4j</span>
public class SimpleMessageBus implements MessageBus
  {
<span class="nc" id="L55">    private final Map&lt;Class&lt;?&gt;, List&lt;WeakReference&lt;Listener&lt;?&gt;&gt;&gt;&gt; listenersMapByTopic = new HashMap&lt;&gt;();</span>

    private final MessageDelivery messageDelivery;
    
<span class="nc" id="L59">    @Getter</span>
    private final Executor executor;
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    public SimpleMessageBus() 
      {
<span class="nc" id="L69">        this(Executors.newFixedThreadPool(10));</span>
<span class="nc" id="L70">      }</span>

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    public SimpleMessageBus (@Nonnull final Executor executor)
      {
<span class="nc" id="L79">        this(executor, new SimpleAsyncMessageDelivery());</span>
<span class="nc" id="L80">      }</span>
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    public SimpleMessageBus (@Nonnull final Executor executor, @Nonnull final MessageDelivery messageDelivery)
<span class="nc" id="L88">      {</span>
<span class="nc" id="L89">        this.executor = executor;</span>
<span class="nc" id="L90">        this.messageDelivery = messageDelivery;</span>
<span class="nc" id="L91">        this.messageDelivery.initialize(this);</span>
<span class="nc" id="L92">        log.info(&quot;MessageBusSupport configured with {}&quot;, messageDelivery);</span>
<span class="nc" id="L93">      }</span>
     
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public &lt;Topic&gt; void publish (@Nonnull final Topic message)
      {
<span class="nc" id="L103">        publish((Class&lt;Topic&gt;)message.getClass(), message);</span>
<span class="nc" id="L104">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public &lt;Topic&gt; void publish (@Nonnull final Class&lt;Topic&gt; topic, @Nonnull final Topic message)
      {
<span class="nc" id="L114">        log.trace(&quot;publish({}, {})&quot;, topic, message);</span>
<span class="nc" id="L115">        messageDelivery.deliverMessage(topic, message);</span>
<span class="nc" id="L116">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public &lt;Topic&gt; void subscribe (@Nonnull final Class&lt;Topic&gt; topic, @Nonnull final Listener&lt;Topic&gt; listener)
      {
<span class="nc" id="L126">        log.debug(&quot;subscribe({}, {})&quot;, topic, listener);</span>
<span class="nc" id="L127">        findListenersByTopic(topic).add(new WeakReference&lt;&gt;(listener));</span>
<span class="nc" id="L128">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void unsubscribe (@Nonnull final Listener&lt;?&gt; listener)
      {
<span class="nc" id="L138">        log.debug(&quot;unsubscribe({})&quot;, listener);</span>

<span class="nc bnc" id="L140" title="All 2 branches missed.">        for (final List&lt;WeakReference&lt;Listener&lt;?&gt;&gt;&gt; list : listenersMapByTopic.values())</span>
          {
<span class="nc bnc" id="L142" title="All 2 branches missed.">            for (final Iterator&lt;WeakReference&lt;Listener&lt;?&gt;&gt;&gt; i = list.iterator(); i.hasNext(); )</span>
              {
<span class="nc" id="L144">                final WeakReference&lt;?&gt; ref = i.next();</span>

<span class="nc bnc" id="L146" title="All 4 branches missed.">                if ((ref.get() == null) || (ref.get() == listener))</span>
                  {
<span class="nc" id="L148">                    i.remove();</span>
                  }
<span class="nc" id="L150">              }</span>
<span class="nc" id="L151">          }</span>
<span class="nc" id="L152">      }</span>

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected &lt;TOPIC&gt; void dispatchMessage (@Nonnull final Class&lt;TOPIC&gt; topic, @Nonnull final TOPIC message)
      {
<span class="nc" id="L160">        final HashSet&lt;Map.Entry&lt;Class&lt;?&gt;, List&lt;WeakReference&lt;MessageBus.Listener&lt;?&gt;&gt;&gt;&gt;&gt; clone =</span>
<span class="nc" id="L161">                new HashSet&lt;&gt;(listenersMapByTopic.entrySet()); // FIXME: marked as dubious by SpotBugs</span>
marked as dubious by SpotBugs
<span class="nc bnc" id="L163" title="All 2 branches missed."> for (final Map.Entry&lt;Class&lt;?&gt;, List&lt;WeakReference&lt;MessageBus.Listener&lt;?&gt;&gt;&gt;&gt; e : clone)</span> { <span class="nc bnc" id="L165" title="All 2 branches missed."> if (e.getKey().isAssignableFrom(topic))</span> { <span class="nc" id="L167"> final List&lt;WeakReference&lt;MessageBus.Listener&lt;TOPIC&gt;&gt;&gt; listeners = (List)e.getValue();</span> <span class="nc bnc" id="L169" title="All 2 branches missed."> for (final WeakReference&lt;MessageBus.Listener&lt;TOPIC&gt;&gt; listenerReference : listeners)</span> { <span class="nc" id="L171"> final MessageBus.Listener&lt;TOPIC&gt; listener = listenerReference.get();</span> <span class="nc bnc" id="L173" title="All 2 branches missed."> if (listener != null)</span> { try { <span class="nc" id="L177"> listener.notify(message);</span> } <span class="nc" id="L179"> catch (Throwable t)</span> { <span class="nc" id="L181"> log.warn(&quot;deliverMessage()&quot;, t);</span> <span class="nc" id="L182"> }</span> } <span class="nc" id="L184"> }</span> } <span class="nc" id="L186"> }</span> <span class="nc" id="L187"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private &lt;Topic&gt; List&lt;WeakReference&lt;Listener&lt;Topic&gt;&gt;&gt; findListenersByTopic (@Nonnull final Class&lt;Topic&gt; topic) { <span class="nc" id="L196"> List&lt;WeakReference&lt;Listener&lt;Topic&gt;&gt;&gt; listeners = (List)listenersMapByTopic.get(topic);</span> <span class="nc bnc" id="L198" title="All 2 branches missed."> if (listeners == null)</span> { <span class="nc" id="L200"> listeners = new ArrayList&lt;&gt;();</span> <span class="nc" id="L201"> listenersMapByTopic.put(topic, (List)listeners);</span> } <span class="nc" id="L204"> return listeners;</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>