Search in sources :

Example 1 with DempsyException

use of com.nokia.dempsy.DempsyException in project Dempsy by Dempsy.

the class ApplicationDefinition method initialize.

//------------------------------------------------------------------------
// Rudimentary functionality
//------------------------------------------------------------------------
/**
    * This is called from the Dempsy framework itself so there is no need for the
    * user to call it.
    */
public void initialize() throws DempsyException {
    if (!isInitialized) {
        for (ClusterDefinition clusterDef : clusterDefinitions) {
            if (clusterDef == null)
                throw new DempsyException("The application definition for \"" + applicationName + "\" has a null ClusterDefinition.");
            clusterDef.setParentApplicationDefinition(this);
        }
        isInitialized = true;
        validate();
    }
}
Also used : DempsyException(com.nokia.dempsy.DempsyException)

Example 2 with DempsyException

use of com.nokia.dempsy.DempsyException in project Dempsy by Dempsy.

the class ApplicationDefinition method validate.

public void validate() throws DempsyException {
    initialize();
    if (applicationName == null)
        throw new DempsyException("You must set the application name while configuring a Dempsy application.");
    if (clusterDefinitions == null || clusterDefinitions.size() == 0)
        throw new DempsyException("The application \"" + SafeString.valueOf(applicationName) + "\" doesn't have any clusters defined.");
    Set<ClusterId> clusterNames = new HashSet<ClusterId>();
    for (ClusterDefinition clusterDef : clusterDefinitions) {
        if (clusterDef == null)
            throw new DempsyException("The application definition for \"" + applicationName + "\" has a null ClusterDefinition.");
        if (clusterNames.contains(clusterDef.getClusterId()))
            throw new DempsyException("The application definition for \"" + applicationName + "\" has two cluster definitions with the name \"" + clusterDef.getClusterId().getMpClusterName() + "\"");
        clusterNames.add(clusterDef.getClusterId());
        clusterDef.validate();
    }
}
Also used : DempsyException(com.nokia.dempsy.DempsyException) HashSet(java.util.HashSet)

Example 3 with DempsyException

use of com.nokia.dempsy.DempsyException in project Dempsy by Dempsy.

the class ClusterDefinition method setParentApplicationDefinition.

protected ClusterDefinition setParentApplicationDefinition(ApplicationDefinition applicationDef) throws DempsyException {
    if (clusterName == null)
        throw new DempsyException("You must set the 'clusterName' when configuring a dempsy cluster for the application: " + String.valueOf(applicationDef));
    clusterId = new ClusterId(applicationDef.getApplicationName(), clusterName);
    parent = applicationDef;
    return this;
}
Also used : DempsyException(com.nokia.dempsy.DempsyException)

Example 4 with DempsyException

use of com.nokia.dempsy.DempsyException in project Dempsy by Dempsy.

the class Router method initialize.

/**
    * Prior to the {@link Router} being used it needs to be initialized.
    */
public void initialize() throws ClusterInfoException, DempsyException {
    // applicationDefinition cannot be null because the constructor checks
    // put all of the cluster definitions into a map for easy lookup
    Map<ClusterId, ClusterDefinition> defs = new HashMap<ClusterId, ClusterDefinition>();
    for (ClusterDefinition clusterDef : applicationDefinition.getClusterDefinitions()) defs.put(clusterDef.getClusterId(), clusterDef);
    // now see about the one that we are.
    ClusterDefinition currentClusterDef = null;
    if (currentCluster != null) {
        currentClusterDef = defs.get(currentCluster);
        if (currentClusterDef == null)
            throw new DempsyException("This Dempsy instance seems to be misconfigured. While this VM thinks it's an instance of " + currentCluster + " the application it's configured with doesn't contain this cluster definition. The application configuration consists of: " + applicationDefinition);
    }
    // get the set of explicit destinations if they exist
    Set<ClusterId> explicitClusterDestinations = (currentClusterDef != null && currentClusterDef.hasExplicitDestinations()) ? new HashSet<ClusterId>() : null;
    if (explicitClusterDestinations != null)
        explicitClusterDestinations.addAll(Arrays.asList(currentClusterDef.getDestinations()));
    //  then those are the only ones we want to consider
    for (ClusterDefinition clusterDef : applicationDefinition.getClusterDefinitions()) {
        if ((explicitClusterDestinations == null || explicitClusterDestinations.contains(clusterDef.getClusterId())) && !clusterDef.isRouteAdaptorType()) {
            RoutingStrategy strategy = (RoutingStrategy) clusterDef.getRoutingStrategy();
            ClusterId clusterId = clusterDef.getClusterId();
            if (strategy == null)
                throw new DempsyException("Could not retrieve the routing strategy for " + SafeString.valueOf(clusterId));
            // This create will result in a callback on the Router as the Outbound.Coordinator with a 
            // registration event. The Outbound may (will) call back on the Router to retrieve the 
            // MpClusterSession and register itself with the appropriate cluster.
            outbounds.add(strategy.createOutbound(this, mpClusterSession, clusterId));
        }
    }
//-------------------------------------------------------------------------------------
}
Also used : ClusterDefinition(com.nokia.dempsy.config.ClusterDefinition) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterId(com.nokia.dempsy.config.ClusterId) DempsyException(com.nokia.dempsy.DempsyException)

Example 5 with DempsyException

use of com.nokia.dempsy.DempsyException in project Dempsy by Dempsy.

the class ClusterDefinition method validate.

public void validate() throws DempsyException {
    if (parent == null)
        throw new DempsyException("The parent ApplicationDefinition isn't set for the Cluster " + SafeString.valueOf(clusterName) + ". You need to initialize the parent ApplicationDefinition prior to validating");
    if (clusterName == null)
        throw new DempsyException("You must set the 'clusterName' when configuring a dempsy cluster for the application.");
    if (messageProcessorPrototype == null && adaptor == null)
        throw new DempsyException("A dempsy cluster must contain either an 'adaptor' or a message processor prototype. " + clusterId + " doesn't appear to be configure with either.");
    if (messageProcessorPrototype != null && adaptor != null)
        throw new DempsyException("A dempsy cluster must contain either an 'adaptor' or a message processor prototype but not both. " + clusterId + " appears to be configured with both.");
    if (messageProcessorPrototype != null) {
        if (!messageProcessorPrototype.getClass().isAnnotationPresent(MessageProcessor.class))
            throw new DempsyException("Attempting to set an instance of \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" within the " + ClusterDefinition.class.getSimpleName() + " for \"" + SafeString.valueOf(clusterId) + "\" but it isn't identified as a MessageProcessor. Please annotate the class.");
        Method[] methods = messageProcessorPrototype.getClass().getMethods();
        boolean foundAtLeastOneMethod = false;
        for (Method method : methods) {
            if (method.isAnnotationPresent(MessageHandler.class)) {
                foundAtLeastOneMethod = true;
                break;
            }
        }
        if (!foundAtLeastOneMethod)
            throw new DempsyException("No method on the message processor of type \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" is identified as a MessageHandler. Please annotate the appropriate method using @MessageHandler.");
        int startMethods = 0;
        for (Method method : methods) {
            if (method.isAnnotationPresent(Start.class)) {
                startMethods++;
            }
        }
        if (startMethods > 1)
            throw new DempsyException("Multiple methods on the message processor of type\"" + SafeString.valueOf(messageProcessorPrototype) + "\" is identified as a Start method. Please annotate at most one method using @Start.");
        Method[] evictableMethods = messageProcessorPrototype.getClass().getMethods();
        boolean foundEvictableMethod = false;
        Method evictableMethod = null;
        for (Method method : evictableMethods) {
            if (method.isAnnotationPresent(Evictable.class)) {
                if (foundEvictableMethod) {
                    throw new DempsyException("More than one method on the message processor of type \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" is identified as a Evictable. Please annotate the appropriate method using @Evictable.");
                }
                foundEvictableMethod = true;
                evictableMethod = method;
            }
        }
        if (evictableMethod != null) {
            if (evictableMethod.getReturnType() == null || !evictableMethod.getReturnType().isAssignableFrom(boolean.class))
                throw new DempsyException("Evictable method \"" + SafeString.valueOf(evictableMethod) + "\" on the message processor of type \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" should return boolean value. Please annotate the appropriate method using @Evictable.");
        }
    }
    if (adaptor != null && keySource != null) {
        throw new DempsyException("A dempsy cluster can not pre-instantation an adaptor.");
    }
}
Also used : MessageProcessor(com.nokia.dempsy.annotations.MessageProcessor) DempsyException(com.nokia.dempsy.DempsyException) Method(java.lang.reflect.Method)

Aggregations

DempsyException (com.nokia.dempsy.DempsyException)5 MessageProcessor (com.nokia.dempsy.annotations.MessageProcessor)1 ClusterDefinition (com.nokia.dempsy.config.ClusterDefinition)1 ClusterId (com.nokia.dempsy.config.ClusterId)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1