001    /**
002     * 
003     */
004    package de.jw.cloud42.core.eventing.notification;
005    
006    import java.util.Iterator;
007    import java.util.logging.Level;
008    import java.util.logging.Logger;
009    
010    import de.jw.cloud42.core.eventing.Message;
011    import de.jw.cloud42.core.eventing.storage.DefaultSubscriberStore;
012    import de.jw.cloud42.core.eventing.storage.SubscriberStore;
013    import de.jw.cloud42.core.eventing.subscription.Subscription;
014    
015    /**
016     * This class provides a static method to start the notification process.
017     * 
018     * @author fbitzer
019     *
020     */
021    public class NotificationManager {
022    
023            /**
024             * Notifies all stored subscribers.
025             * 
026             * @param message the notification message. Each subscriber transforms this message to its own format (e.g. a
027             * SOAP message) before sending.
028             */
029            public static void notifySubscribers(Message message){
030                    
031                    //iterate over subscribers and call sendEventData on each subscriber
032                    SubscriberStore store = new DefaultSubscriberStore();
033                    try {
034                            
035                            
036                            for (Iterator iter = store.retrieveAllSubscribers(message.topic); iter.hasNext();) {
037                                    Subscription s = (Subscription) iter.next();
038                            
039                                    s.sendEventData(message);
040                                    
041                                    //this causes the current thread to sleep 750 ms so that messages do not overlap
042                                    Thread.sleep(750);
043                            }
044                    
045                    } catch (Exception ex){
046                            Logger.getAnonymousLogger().log(Level.SEVERE, "Error notifiying all subscribers: " + ex.getMessage());
047                            ex.printStackTrace();
048                    }
049            }
050    }