com.elusive_code.newsboy.AsyncEventService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of NewsBoy Show documentation
Show all versions of NewsBoy Show documentation
Asynchronous publish-subscribe communication library.
Features:
1. Listeners are stored using WeakReferences to prevent memory leaks when they are not unsubscribed.
2. Uses fork-join framework for concurrent event delivery.
3. Publish methods returns collection of Futures that represent event notifications.
4. Supports ordered publishing: guaranteed to notify of the events in order they were published.
5. Provides EventSource and EventService interfaces for better integration with IOC containers and alternative implementations.
/*
* Copyright 2014. Vladislav Dolgikh
*
* 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.
*/
package com.elusive_code.newsboy;
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
/**
*
* Implementation of {@link com.elusive_code.newsboy.EventService} that uses
*
* - weak references to store subscribers
*
- asynchronous, and uses Fork-Join framework to schedule notifications
*
*
*
*
* @see com.elusive_code.newsboy.EventService
* @see com.elusive_code.newsboy.Subscribe
* @author Vladislav Dolgikh
*/
public class AsyncEventService implements EventService {
private static final Logger LOG = Logger.getLogger(AsyncEventService.class.getName());
/**
* Subscribed listeners. Used for faster unsubscribing
* Key - listener object, Value - set of event handlers
*/
private WeakHashMap