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 :: Actors</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">/*
* *********************************************************************************************************************
*
* TheseFoolishThings: Miscellaneous utilities
* http://tidalwave.it/projects/thesefoolishthings
*
* Copyright (C) 2009 - 2023 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 java.lang.ref.WeakReference;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
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 lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* A partial implementation of {@link MessageBus}.
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L50">@ThreadSafe @Slf4j</span>
public class SimpleMessageBus implements MessageBus
{
<span class="nc" id="L53"> private final Map<Class<?>, List<WeakReference<Listener<?>>>> listenersMapByTopic = new HashMap<>();</span>
private final MessageDelivery messageDelivery;
<span class="nc" id="L57"> @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="L68"> this(Executors.newFixedThreadPool(10));</span>
<span class="nc" id="L69"> }</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="L80"> this(executor, new SimpleAsyncMessageDelivery());</span>
<span class="nc" id="L81"> }</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="L92"> {</span>
<span class="nc" id="L93"> this.executor = executor;</span>
<span class="nc" id="L94"> this.messageDelivery = messageDelivery;</span>
<span class="nc" id="L95"> this.messageDelivery.initialize(this);</span>
<span class="nc" id="L96"> log.info("MessageBusSupport configured with {}", messageDelivery);</span>
<span class="nc" id="L97"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <T> void publish (@Nonnull final T message)
{
<span class="nc" id="L107"> publish((Class<T>)message.getClass(), message);</span>
<span class="nc" id="L108"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <T> void publish (@Nonnull final Class<T> topic, @Nonnull final T message)
{
<span class="nc" id="L118"> log.trace("publish({}, {})", topic, message);</span>
<span class="nc" id="L119"> messageDelivery.deliverMessage(topic, message);</span>
<span class="nc" id="L120"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <T> void subscribe (@Nonnull final Class<T> topic, @Nonnull final Listener<T> listener)
{
<span class="nc" id="L130"> log.debug("subscribe({}, {})", topic, listener);</span>
<span class="nc" id="L131"> findListenersByTopic(topic).add(new WeakReference<>(listener));</span>
<span class="nc" id="L132"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public void unsubscribe (@Nonnull final Listener<?> listener)
{
<span class="nc" id="L142"> log.debug("unsubscribe({})", listener);</span>
<span class="nc bnc" id="L144" title="All 2 branches missed."> for (final var list : listenersMapByTopic.values())</span>
{
<span class="nc bnc" id="L146" title="All 4 branches missed."> list.removeIf(ref -> (ref.get() == null) || (ref.get() == listener));</span>
<span class="nc" id="L147"> }</span>
<span class="nc" id="L148"> }</span>
/*******************************************************************************************************************
*
* Dispatches a message.
*
* @param <T> the static type of the topic
* @param topic the dynamic type of the topic
* @param message the message
*
******************************************************************************************************************/
protected <T> void dispatchMessage (@Nonnull final Class<T> topic, @Nonnull final T message)
{
<span class="nc" id="L161"> final var clone = new HashSet<>(listenersMapByTopic.entrySet()); // FIXME: marked as dubious by SpotBugs</span>
<span class="nc bnc" id="L163" title="All 2 branches missed."> for (final var 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<T>>> listeners = (List)e.getValue();</span>
<span class="nc bnc" id="L169" title="All 2 branches missed."> for (final var listenerReference : listeners)</span>
{
<span class="nc" id="L171"> final var 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 <T> List<WeakReference<Listener<T>>> findListenersByTopic (@Nonnull final Class<T> topic)
{
// FIXME: use putIfAbsent()
<span class="nc" id="L197"> List<WeakReference<Listener<T>>> listeners = (List)listenersMapByTopic.get(topic);</span>
<span class="nc bnc" id="L199" title="All 2 branches missed."> if (listeners == null)</span>
{
<span class="nc" id="L201"> listeners = new ArrayList<>();</span>
<span class="nc" id="L202"> listenersMapByTopic.put(topic, (List)listeners);</span>
}
<span class="nc" id="L205"> return listeners;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>