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> > <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">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 "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.
*
* *********************************************************************************************************************
*
*
* *********************************************************************************************************************
* #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<Class<?>, List<WeakReference<Listener<?>>>> listenersMapByTopic = new HashMap<>();</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("MessageBusSupport configured with {}", messageDelivery);</span>
<span class="nc" id="L93"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <Topic> void publish (@Nonnull final Topic message)
{
<span class="nc" id="L103"> publish((Class<Topic>)message.getClass(), message);</span>
<span class="nc" id="L104"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <Topic> void publish (@Nonnull final Class<Topic> topic, @Nonnull final Topic message)
{
<span class="nc" id="L114"> log.trace("publish({}, {})", topic, message);</span>
<span class="nc" id="L115"> messageDelivery.deliverMessage(topic, message);</span>
<span class="nc" id="L116"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <Topic> void subscribe (@Nonnull final Class<Topic> topic, @Nonnull final Listener<Topic> listener)
{
<span class="nc" id="L126"> log.debug("subscribe({}, {})", topic, listener);</span>
<span class="nc" id="L127"> findListenersByTopic(topic).add(new WeakReference<>(listener));</span>
<span class="nc" id="L128"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public void unsubscribe (@Nonnull final Listener<?> listener)
{
<span class="nc" id="L138"> log.debug("unsubscribe({})", listener);</span>
<span class="nc bnc" id="L140" title="All 2 branches missed."> for (final List<WeakReference<Listener<?>>> list : listenersMapByTopic.values())</span>
{
<span class="nc bnc" id="L142" title="All 2 branches missed."> for (final Iterator<WeakReference<Listener<?>>> i = list.iterator(); i.hasNext(); )</span>
{
<span class="nc" id="L144"> final WeakReference<?> 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 <TOPIC> void dispatchMessage (@Nonnull final Class<TOPIC> topic, @Nonnull final TOPIC message)
{
<span class="nc" id="L160"> final HashSet<Map.Entry<Class<?>, List<WeakReference<MessageBus.Listener<?>>>>> clone =</span>
<span class="nc" id="L161"> new HashSet<>(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<Class<?>, List<WeakReference<MessageBus.Listener<?>>>> 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<WeakReference<MessageBus.Listener<TOPIC>>> listeners = (List)e.getValue();</span>
<span class="nc bnc" id="L169" title="All 2 branches missed."> for (final WeakReference<MessageBus.Listener<TOPIC>> listenerReference : listeners)</span>
{
<span class="nc" id="L171"> final MessageBus.Listener<TOPIC> 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("deliverMessage()", 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 <Topic> List<WeakReference<Listener<Topic>>> findListenersByTopic (@Nonnull final Class<Topic> topic)
{
<span class="nc" id="L196"> List<WeakReference<Listener<Topic>>> 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<>();</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>