All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atomikos.publish.EventPublisher Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * Copyright (C) 2000-2016 Atomikos 
 *
 * LICENSE CONDITIONS
 *
 * See http://www.atomikos.com/Main/WhichLicenseApplies for details.
 */

package com.atomikos.publish;

import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;

import com.atomikos.icatch.event.Event;
import com.atomikos.icatch.event.EventListener;
import com.atomikos.icatch.event.transaction.TransactionHeuristicEvent;
import com.atomikos.logging.Logger;
import com.atomikos.logging.LoggerFactory;

public class EventPublisher {
	
	private static Logger LOGGER = LoggerFactory.createLogger(EventPublisher.class);
	
	private static Set listeners = new HashSet();
	
	static {
		findAllEventListenersInClassPath();
	}
	
	private EventPublisher(){}
	
	private static void findAllEventListenersInClassPath() {
		ServiceLoader loader = ServiceLoader.load(EventListener.class,EventPublisher.class.getClassLoader());
		for (EventListener l : loader) {
			registerEventListener(l);
		}
	}

	public static void publish(Event event) {
		if (event != null) {
			notifyAllListeners(event);
		}
	}

	private static void notifyAllListeners(Event event) {
		if (event instanceof TransactionHeuristicEvent) {
			TransactionHeuristicEvent the = (TransactionHeuristicEvent)event;
			if (listeners.isEmpty()) {
				LOGGER.logError("Transaction " + the.transactionId + " corrupted - check https://www.atomikos.com/Documentation/HowToHandleHeuristics to learn more");
			}
		}
		
		
		for (EventListener listener : listeners) {				
			try {
				listener.eventOccurred(event);
			} catch (Exception e) {
				LOGGER.logError("Error notifying listener " + listener, e);
			}
		}
	}

	/**
	 * Useful for testing only. Not safe for other use.
	 * 
	 * @param listener
	 */
	public static void registerEventListener(EventListener listener) {
		listeners.add(listener);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy