Package org.wso2.carbon.event.broker

Source Code of org.wso2.carbon.event.broker.CarbonNotificationManager$Worker

/*
*  Copyright (c) 2005-2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you 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.wso2.carbon.event.broker;

import org.apache.axis2.context.ConfigurationContext;
import org.wso2.event.*;
import org.wso2.event.exceptions.EventException;
import org.wso2.carbon.event.broker.exceptions.NotificationException;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.context.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;
import java.util.concurrent.*;

public class CarbonNotificationManager implements NotificationManager {

    private static final Log log = LogFactory.getLog(CarbonNotificationManager.class);
   
    private EventBrokerService broker = null;
    private ExecutorService executor = null;
    private Map<String, String> parameters = null;

    // TODO: Added custom dispatchers for the moment, until we introduce formatting. Remove this
    //       once we have message formatting in place.
    private Map<String, EventDispatcher> customDispatchers = null;
   
   
    public CarbonNotificationManager() {
    super();
  }

  public void setEventBroker(EventBrokerService broker) {
        this.broker = broker;
    }

    public String getPropertyValue(String key) {
        if (this.parameters != null) {
            return this.parameters.get(key);
        }
        return null;
    }

    public void init(Map<String, String> parameters) {
        if (parameters != null) {
            this.parameters = parameters;
        }
        this.customDispatchers = new HashMap<String, EventDispatcher>();
        String temp = null;
        int minSpareThreads = 25;
        if (parameters != null) {
            temp = parameters.get("minSpareThreads");
            if (temp != null) {
                minSpareThreads = Integer.parseInt(temp);
            }
        }
        int maxThreads = 150;
        if (parameters != null) {
            temp = parameters.get("maxThreads");
            if (temp != null) {
                maxThreads = Integer.parseInt(temp);
            }
        }
        int maxQueuedRequests = 100;
        if (parameters != null) {
            temp = parameters.get("maxQueuedRequests");
            if (temp != null) {
                maxQueuedRequests = Integer.parseInt(temp);
            }
        }
        long keepAliveTime = 1000;
        if (parameters != null) {
            temp = parameters.get("keepAliveTime");
            if (temp != null) {
                keepAliveTime = Long.parseLong(temp);
            }
        }
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(maxQueuedRequests);
        executor = new ThreadPoolExecutor(minSpareThreads, maxThreads,
                keepAliveTime, TimeUnit.NANOSECONDS, queue);
    }

    public void publishEvent(Event event) throws NotificationException {
        publishEvent(null, event);
    }

    @SuppressWarnings("unchecked")
    public void publishEvent(ConfigurationContext configurationContext, Event event) throws NotificationException {
        try {
      if (getExecutor() == null) {
          String msg = "Notification Dispatcher not initialized";
          log.error(msg);
          throw new NotificationException(msg);
      }
            if (event == null || event.getMessage() == null) {
                String msg = "Unable to Dispatch Notification. The incoming event was null, or did " +
                        "not contain a message.";
          log.error(msg);
          throw new NotificationException(msg);
            }
           
            if(event.getMessage() instanceof MessageContext){
                SOAPEnvelope envelope = ((MessageContext)event.getMessage()).getEnvelope();
                envelope.build();
                envelope.getBody().build();   
            } else if(event.getMessage() instanceof OMElement){
                ((OMElement)event.getMessage()).build();
            }
           
      SubscriptionManager subManager = broker.getSubscriptionManager();
      List<Subscription> subscriptions = subManager.getMatchingSubscriptions(event);
      for (Subscription subscription : subscriptions) {
         try {
           EventDispatcher dispatcher = subscription.getEventDispatcher();

           if (dispatcher == null){
                         for (String key : customDispatchers.keySet()) {
                             if (subscription.getFilterDesc().getFilterValue() != null &&
                                     subscription.getFilterDesc().getFilterValue().startsWith(key)) {
                                 dispatcher = customDispatchers.get(key);
                                 break;
                             }
                         }
                         if (dispatcher == null) {
                             dispatcher = new CarbonEventDispatcher();
                             if (configurationContext != null) {
                                 ((CarbonEventDispatcher) dispatcher).init(configurationContext);
                             }
                         }
           }
           getExecutor().submit(new Worker(dispatcher,  event, subscription));
           } catch (Exception e) {
             //errors should not happen here, but just in case
               log.warn("Unable to dispatch event of type " + event.getClass().getName() +
                   e.getMessage(), e);
           }
      }
    } catch (EventException e) {
            if (e instanceof NotificationException) {
                throw (NotificationException)e;
            }
      throw new NotificationException("Failed publishing the event "+ e.getMessage(),e);
    }
    }

 

    public ExecutorService getExecutor() {
        return executor;
    }

    public Map<String, EventDispatcher> getCustomDispatchers() {
        return customDispatchers;
    }

                                                            
    private class Worker implements Runnable {

        private EventDispatcher dispatcher;
        private Event event;
        Subscription subscription;

        public Worker(EventDispatcher dispatcher, Event event, Subscription subscription) {
            this.dispatcher = dispatcher;
            this.event = event;
            this.subscription = subscription;
        }

        @SuppressWarnings("unchecked")
        public void run() {
            try {
                dispatcher.onMatchingEvent(event, subscription);
            } catch (Exception e) {
                log.warn("Unable to dispatch event of type " + event.getClass().getName() +
                        " using the dispatcher of type " + dispatcher.getClass().getName(), e);
            }
        }
    }
}
TOP

Related Classes of org.wso2.carbon.event.broker.CarbonNotificationManager$Worker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.