Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010. Axon Framework
   *
   * 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 org.axonframework.eventhandling;
 
 
 import java.util.Set;
Implementation of the EventBus that directly forwards all published events (in the callers' thread) to all subscribed listeners.

Listeners are expected to implement asynchronous handling themselves.

Author(s):
Allard Buijze
Since:
0.5
See also:
AsynchronousEventHandlerWrapper
 
 public class SimpleEventBus implements EventBus {
 
     private static final Logger logger = LoggerFactory.getLogger(SimpleEventBus.class);
     private final Set<EventListenerlisteners = new CopyOnWriteArraySet<EventListener>();
     private volatile SimpleEventBusStatistics statistics = new SimpleEventBusStatistics();

    
Initializes the SimpleEventBus and registers the mbeans for management information.
 
     public SimpleEventBus() {
         this(true);
     }

    
Initiates the SimpleEventBus and makes the registration of mbeans for management information optional.

Parameters:
registerMBeans true to register the mbeans, false for not registering them.
 
     public SimpleEventBus(boolean registerMBeans) {
         if (registerMBeans) {
             JmxConfiguration.getInstance().registerMBean(getClass());
         }
     }

    
 
     @Override
     public void unsubscribe(EventListener eventListener) {
         if (.remove(eventListener)) {
             Object listener = getActualListenerFrom(eventListener);
             .recordUnregisteredListener(listener.getClass().getSimpleName());
             .debug("EventListener {} unsubscribed successfully"eventListener.getClass().getSimpleName());
         } else {
             .info("EventListener {} not removed. It was already unsubscribed",
                         eventListener.getClass().getSimpleName());
         }
     }

    
 
     @Override
     public void subscribe(EventListener eventListener) {
         if (.add(eventListener)) {
             Object listener = getActualListenerFrom(eventListener);
             .listenerRegistered(listener.getClass().getSimpleName());
             .debug("EventListener [{}] subscribed successfully"eventListener.getClass().getSimpleName());
         } else {
             .info("EventListener [{}] not added. It was already subscribed",
                         eventListener.getClass().getSimpleName());
         }
     }
 
     private Object getActualListenerFrom(EventListener eventListener) {
         Object listener = eventListener;
         while (listener instanceof EventListenerProxy) {
             listener = ((EventListenerProxylistener).getTarget();
         }
         return listener;
     }

    
    @Override
    public void publish(Event event) {
        if ( != null && !.isEmpty()) {
            for (EventListener listener : ) {
                .debug("Dispatching Event [{}] to EventListener [{}]",
                             event.getClass().getSimpleName(),
                             listener.getClass().getSimpleName());
                listener.handle(event);
            }
        }
    }
New to GrepCode? Check out our FAQ X