Search in sources :

Example 1 with DempsyException

use of net.dempsy.DempsyException in project Dempsy by Dempsy.

the class LockingContainer method getInstanceForKey.

/**
 * This is required to return non null or throw a ContainerException
 */
InstanceWrapper getInstanceForKey(final Object key) throws ContainerException {
    // common case has "no" contention
    InstanceWrapper wrapper = instances.get(key);
    if (wrapper != null)
        return wrapper;
    // otherwise we will be working to get one.
    final Boolean tmplock = new Boolean(true);
    Boolean lock = keysBeingWorked.putIfAbsent(key, tmplock);
    if (lock == null)
        lock = tmplock;
    // otherwise we'll do an atomic check-and-update
    synchronized (lock) {
        // double checked lock?????
        wrapper = instances.get(key);
        if (wrapper != null)
            return wrapper;
        Object instance = null;
        try {
            instance = prototype.newInstance();
        } catch (final DempsyException e) {
            if (e.userCaused()) {
                LOGGER.warn("The message processor prototype " + SafeString.valueOf(prototype) + " threw an exception when trying to create a new message processor for they key " + SafeString.objectDescription(key));
                statCollector.messageFailed(true);
                instance = null;
            } else
                throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + " because the clone method threw an exception.", e);
        } catch (final RuntimeException e) {
            throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + " because the clone invocation resulted in an unknown exception.", e);
        }
        // activate
        boolean activateSuccessful = false;
        try {
            if (instance != null) {
                if (LOGGER.isTraceEnabled())
                    LOGGER.trace("the container for " + clusterId + " is activating instance " + String.valueOf(instance) + " via " + SafeString.valueOf(prototype));
                prototype.activate(instance, key);
                activateSuccessful = true;
            }
        } catch (final DempsyException e) {
            if (e.userCaused()) {
                LOGGER.warn("The message processor " + SafeString.objectDescription(instance) + " activate call threw an exception.");
                statCollector.messageFailed(true);
            } else
                throw new ContainerException("the container for " + clusterId + " failed to invoke the activate method of " + SafeString.valueOf(prototype) + ". Is the active method accessible - the class is public and the method is public?", e);
        } catch (final RuntimeException e) {
            throw new ContainerException("the container for " + clusterId + " failed to invoke the activate method of " + SafeString.valueOf(prototype) + " because of an unknown exception.", e);
        }
        if (activateSuccessful) {
            // we only want to create a wrapper and place the instance into the container
            // if the instance activated correctly. If we got here then the above try block
            // must have been successful.
            // null check above.
            wrapper = new InstanceWrapper(instance);
            // once it goes into the map, we can remove it from the 'being worked' set
            instances.put(key, wrapper);
            // remove it from the keysBeingWorked since any subsequent call will get
            keysBeingWorked.remove(key);
            // the newly added one.
            statCollector.messageProcessorCreated(key);
        }
        return wrapper;
    }
}
Also used : ContainerException(net.dempsy.container.ContainerException) DempsyException(net.dempsy.DempsyException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 2 with DempsyException

use of net.dempsy.DempsyException in project Dempsy by Dempsy.

the class ClusterGroupRouter method selectDestinationForMessage.

@Override
public ContainerAddress selectDestinationForMessage(final KeyedMessageWithType message) {
    final GroupDetails[] destinations = this.destinations.get();
    if (destinations == null)
        throw new DempsyException("It appears the " + ClusterGroupRouter.class.getSimpleName() + " strategy for the message key " + SafeString.objectDescription(message != null ? message.key : null) + " is being used prior to initialization or after a failure.");
    if (containerIndex < 0) {
        containerIndex = getIndex(destinations, clusterName);
        if (containerIndex < 0)
            return null;
    }
    final GroupDetails cur = destinations[utils.determineShard(message.key, mask)];
    return cur.containerAddresses[containerIndex];
}
Also used : GroupDetails(net.dempsy.router.group.intern.GroupDetails) DempsyException(net.dempsy.DempsyException)

Example 3 with DempsyException

use of net.dempsy.DempsyException in project Dempsy by Dempsy.

the class NioReceiver method getAddress.

@Override
public synchronized NioAddress getAddress(final Infrastructure infra) {
    if (internal == null) {
        final String ifNameToGetAddrFrom = infra.getConfigValue(NioReceiver.class, CONFIG_KEY_RECEIVER_NETWORK_IF_NAME, DEFAULT_RECEIVER_NETWORK_IF_NAME);
        if (useLocalHost) {
            if (ifNameToGetAddrFrom != null)
                LOGGER.warn("Both \"useLocalHost\" as well as the property " + CONFIG_KEY_RECEIVER_NETWORK_IF_NAME + " for " + NioReceiver.class.getPackage().getName() + ". The property will be ignored.");
            if (addrSupplier != null)
                LOGGER.warn("Both IP address supplier (" + addrSupplier.getClass().getName() + ") as well as \"useLocalHost\" was set. The address supplier will be ignored.");
        } else {
            if (addrSupplier != null && ifNameToGetAddrFrom != null)
                LOGGER.warn("Both IP Address supplier (" + addrSupplier.getClass().getName() + ") as well as the property " + CONFIG_KEY_RECEIVER_NETWORK_IF_NAME + " for " + NioReceiver.class.getPackage().getName() + ". The property will be ignored.");
        }
        try {
            final InetAddress addr = useLocalHost ? Inet4Address.getLocalHost() : (addrSupplier == null ? TcpUtils.getFirstNonLocalhostInetAddress(ifNameToGetAddrFrom) : addrSupplier.get());
            binding = new Binding(addr, internalPort);
            final InetSocketAddress inetSocketAddress = binding.bound;
            internalPort = inetSocketAddress.getPort();
            internal = new NioAddress(addr, internalPort, serId, binding.recvBufferSize);
            address = resolver.getExternalAddresses(internal);
        } catch (final IOException e) {
            throw new DempsyException(e, false);
        }
    }
    return address;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DempsyException(net.dempsy.DempsyException) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 4 with DempsyException

use of net.dempsy.DempsyException in project Dempsy by Dempsy.

the class NonLockingAltContainer method getInstanceForKey.

/**
 * This is required to return non null or throw a ContainerException
 */
InstanceWrapper getInstanceForKey(final Object key) throws ContainerException {
    // common case has "no" contention
    InstanceWrapper wrapper = instances.get(key);
    if (wrapper != null)
        return wrapper;
    // otherwise we will be working to get one.
    final Boolean tmplock = new Boolean(true);
    Boolean lock = keysBeingWorked.putIfAbsent(key, tmplock);
    if (lock == null)
        lock = tmplock;
    // otherwise we'll do an atomic check-and-update
    synchronized (lock) {
        // double checked lock?????
        wrapper = instances.get(key);
        if (wrapper != null)
            return wrapper;
        Object instance = null;
        try {
            instance = prototype.newInstance();
        } catch (final DempsyException e) {
            if (e.userCaused()) {
                LOGGER.warn("The message processor prototype " + SafeString.valueOf(prototype) + " threw an exception when trying to create a new message processor for they key " + SafeString.objectDescription(key));
                statCollector.messageFailed(true);
                instance = null;
            } else
                throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + " because the clone method threw an exception.", e);
        } catch (final RuntimeException e) {
            throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + " because the clone invocation resulted in an unknown exception.", e);
        }
        if (instance == null)
            throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + ". The value returned from the clone call appears to be null.");
        // activate
        boolean activateSuccessful = false;
        try {
            if (instance != null) {
                if (LOGGER.isTraceEnabled())
                    LOGGER.trace("the container for " + clusterId + " is activating instance " + String.valueOf(instance) + " via " + SafeString.valueOf(prototype));
                prototype.activate(instance, key);
                activateSuccessful = true;
            }
        } catch (final DempsyException e) {
            if (e.userCaused()) {
                LOGGER.warn("The message processor " + SafeString.objectDescription(instance) + " activate call threw an exception.");
                statCollector.messageFailed(true);
                instance = null;
            } else
                throw new ContainerException("the container for " + clusterId + " failed to invoke the activate method of " + SafeString.valueOf(prototype) + ". Is the active method accessible - the class is public and the method is public?", e);
        } catch (final RuntimeException e) {
            throw new ContainerException("the container for " + clusterId + " failed to invoke the activate method of " + SafeString.valueOf(prototype) + " because of an unknown exception.", e);
        }
        if (activateSuccessful) {
            // we only want to create a wrapper and place the instance into the container
            // if the instance activated correctly. If we got here then the above try block
            // must have been successful.
            // null check above.
            wrapper = new InstanceWrapper(instance);
            // once it goes into the map, we can remove it from the 'being worked' set
            instances.putIfAbsent(key, wrapper);
            // remove it from the keysBeingWorked since any subsequent call will get
            keysBeingWorked.remove(key);
            // the newly added one.
            statCollector.messageProcessorCreated(key);
        }
        return wrapper;
    }
}
Also used : ContainerException(net.dempsy.container.ContainerException) DempsyException(net.dempsy.DempsyException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 5 with DempsyException

use of net.dempsy.DempsyException in project Dempsy by Dempsy.

the class NonLockingContainer method createAndActivate.

// ----------------------------------------------------------------------------
// Test Hooks
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Internals
// ----------------------------------------------------------------------------
// this is called directly from tests but shouldn't be accessed otherwise.
private Object createAndActivate(final Object key) throws ContainerException {
    Object instance = null;
    try {
        instance = prototype.newInstance();
    } catch (final DempsyException e) {
        if (e.userCaused()) {
            LOGGER.warn("The message processor prototype " + SafeString.valueOf(prototype) + " threw an exception when trying to create a new message processor for they key " + SafeString.objectDescription(key));
            statCollector.messageFailed(true);
            instance = null;
        } else
            throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + " because the clone method threw an exception.", e);
    } catch (final RuntimeException e) {
        throw new ContainerException("the container for " + clusterId + " failed to create a new instance of " + SafeString.valueOf(prototype) + " for the key " + SafeString.objectDescription(key) + " because the clone invocation resulted in an unknown exception.", e);
    }
    // activate
    boolean activateSuccessful = false;
    try {
        if (instance != null) {
            if (LOGGER.isTraceEnabled())
                LOGGER.trace("the container for " + clusterId + " is activating instance " + String.valueOf(instance) + " via " + SafeString.valueOf(prototype));
            prototype.activate(instance, key);
            activateSuccessful = true;
        }
    } catch (final DempsyException e) {
        if (e.userCaused()) {
            LOGGER.warn("The message processor " + SafeString.objectDescription(instance) + " activate call threw an exception.");
            statCollector.messageFailed(true);
            instance = null;
        } else
            throw new ContainerException("the container for " + clusterId + " failed to invoke the activate method of " + SafeString.valueOf(prototype) + ". Is the active method accessible - the class is public and the method is public?", e);
    } catch (final RuntimeException e) {
        throw new ContainerException("the container for " + clusterId + " failed to invoke the activate method of " + SafeString.valueOf(prototype) + " because of an unknown exception.", e);
    }
    if (activateSuccessful) {
        // must have been successful.
        if (// once it goes into the map, we can remove it from the 'being worked' set
        instances.putIfAbsent(key, instance) != null)
            throw new IllegalStateException("WTF?");
        // the newly added one.
        statCollector.messageProcessorCreated(key);
    }
    return instance;
}
Also used : ContainerException(net.dempsy.container.ContainerException) DempsyException(net.dempsy.DempsyException)

Aggregations

DempsyException (net.dempsy.DempsyException)6 ContainerException (net.dempsy.container.ContainerException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)1 GroupDetails (net.dempsy.router.group.intern.GroupDetails)1 Test (org.junit.Test)1