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">/*
 * *********************************************************************************************************************
 *
 * 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 javax.annotation.concurrent.ThreadSafe;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
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="L51">@ThreadSafe @Slf4j</span>
public class SimpleMessageBus implements MessageBus
  {
<span class="nc" id="L54">    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="L58">    @Getter</span>
    private final Executor executor;
    
    /*******************************************************************************************************************
     *
     * Creates a new instance with a {@link SimpleAsyncMessageDelivery} strategy for delivery. It will use its own
     * thread pool.
     *
     ******************************************************************************************************************/
    public SimpleMessageBus() 
      {
<span class="nc" id="L69">        this(Executors.newFixedThreadPool(10));</span>
<span class="nc" id="L70">      }</span>

    /*******************************************************************************************************************
     *
     * Creates a new instance given an executor and a {@link SimpleAsyncMessageDelivery} strategy for delivery.
     *
     * @param   executor          the {@link Executor}
     *
     ******************************************************************************************************************/
    public SimpleMessageBus (@Nonnull final Executor executor)
      {
<span class="nc" id="L81">        this(executor, new SimpleAsyncMessageDelivery());</span>
<span class="nc" id="L82">      }</span>
    
    /*******************************************************************************************************************
     *
     * Creates a new instance given an executor and a strategy for delivery.
     *
     * @param   executor          the {@link Executor}
     * @param   messageDelivery   the strategy for delivery
     *
     ******************************************************************************************************************/
    public SimpleMessageBus (@Nonnull final Executor executor, @Nonnull final MessageDelivery messageDelivery)
<span class="nc" id="L93">      {</span>
<span class="nc" id="L94">        this.executor = executor;</span>
<span class="nc" id="L95">        this.messageDelivery = messageDelivery;</span>
<span class="nc" id="L96">        this.messageDelivery.initialize(this);</span>
<span class="nc" id="L97">        log.info(&quot;MessageBusSupport configured with {}&quot;, messageDelivery);</span>
<span class="nc" id="L98">      }</span>
     
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public &lt;Topic&gt; void publish (@Nonnull final Topic message)
      {
<span class="nc" id="L108">        publish((Class&lt;Topic&gt;)message.getClass(), message);</span>
<span class="nc" id="L109">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public &lt;Topic&gt; void publish (@Nonnull final Class&lt;Topic&gt; topic, @Nonnull final Topic message)
      {
<span class="nc" id="L119">        log.trace(&quot;publish({}, {})&quot;, topic, message);</span>
<span class="nc" id="L120">        messageDelivery.deliverMessage(topic, message);</span>
<span class="nc" id="L121">      }</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="L131">        log.debug(&quot;subscribe({}, {})&quot;, topic, listener);</span>
<span class="nc" id="L132">        findListenersByTopic(topic).add(new WeakReference&lt;&gt;(listener));</span>
<span class="nc" id="L133">      }</span>

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

<span class="nc bnc" id="L145" 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="L147" title="All 4 branches missed.">            list.removeIf(ref -&gt; (ref.get() == null) || (ref.get() == listener));</span>
<span class="nc" id="L148">          }</span>
<span class="nc" id="L149">      }</span>

    /*******************************************************************************************************************
     *
     * Dispatches a message.
     *
     * @param   &lt;TOPIC&gt;   the static type of the topic
     * @param   topic     the dynamic type of the topic
     * @param   message   the message
     *
     ******************************************************************************************************************/
    protected &lt;TOPIC&gt; void dispatchMessage (@Nonnull final Class&lt;TOPIC&gt; topic, @Nonnull final TOPIC message)
      {
<span class="nc" id="L162">        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="L163">                new HashSet&lt;&gt;(listenersMapByTopic.entrySet()); // FIXME: marked as dubious by SpotBugs</span>
marked as dubious by SpotBugs
<span class="nc bnc" id="L165" 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="L167" title="All 2 branches missed."> if (e.getKey().isAssignableFrom(topic))</span> { <span class="nc" id="L169"> final List&lt;WeakReference&lt;MessageBus.Listener&lt;TOPIC&gt;&gt;&gt; listeners = (List)e.getValue();</span> <span class="nc bnc" id="L171" title="All 2 branches missed."> for (final WeakReference&lt;MessageBus.Listener&lt;TOPIC&gt;&gt; listenerReference : listeners)</span> { <span class="nc" id="L173"> final MessageBus.Listener&lt;TOPIC&gt; listener = listenerReference.get();</span> <span class="nc bnc" id="L175" title="All 2 branches missed."> if (listener != null)</span> { try { <span class="nc" id="L179"> listener.notify(message);</span> } <span class="nc" id="L181"> catch (Throwable t)</span> { <span class="nc" id="L183"> log.warn(&quot;deliverMessage()&quot;, t);</span> <span class="nc" id="L184"> }</span> } <span class="nc" id="L186"> }</span> } <span class="nc" id="L188"> }</span> <span class="nc" id="L189"> }</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="L198"> List&lt;WeakReference&lt;Listener&lt;Topic&gt;&gt;&gt; listeners = (List)listenersMapByTopic.get(topic);</span> <span class="nc bnc" id="L200" title="All 2 branches missed."> if (listeners == null)</span> { <span class="nc" id="L202"> listeners = new ArrayList&lt;&gt;();</span> <span class="nc" id="L203"> listenersMapByTopic.put(topic, (List)listeners);</span> } <span class="nc" id="L206"> 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>