Search in sources :

Example 6 with DempsyException

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

the class TestContainer method testMpThrowsDempsyException.

@Test
public void testMpThrowsDempsyException() throws Exception {
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
    justThrowMe = new DempsyException("JustThrowMe!");
    final KeyedMessageWithType kmwt = ke.extract(new MyMessage("YO")).get(0);
    container.dispatch(kmwt, Operation.handle, true);
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
}
Also used : KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) DempsyException(net.dempsy.DempsyException) Test(org.junit.Test)

Example 7 with DempsyException

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

the class NioDefaultExternalAddressResolver method getExternalAddresses.

@Override
public NioAddress getExternalAddresses(final NioAddress addr) throws DempsyException {
    if (addr == null)
        throw new NullPointerException("Null address passed to resolver");
    final String dockerHost = System.getenv(DOCKER_HOST_ENV_VARIABLE);
    // If we set the environment variable DOCKER_HOST
    if (dockerHost != null) {
        final int colonIndex = dockerHost.indexOf(':');
        final String[] splitStr = colonIndex < 0 ? new String[] { dockerHost, null } : dockerHost.split(":", -1);
        final String ipAddr = (splitStr[0] == null || splitStr[0].trim().length() == 0) ? null : splitStr[0];
        final int port = splitStr[1] == null ? addr.port : Integer.parseInt(splitStr[1]);
        try {
            return new NioAddress(ipAddr == null ? TcpUtils.getFirstNonLocalhostInetAddress() : InetAddress.getByName(ipAddr), port, addr.serializerId, addr.recvBufferSize, addr.messageSizeLimit);
        } catch (final UnknownHostException | SocketException uhe) {
            throw new DempsyException(uhe, true);
        }
    }
    // Otherwise, if we bound the internal address to all interfaces
    if (addr.inetAddress == null || addr.inetAddress.isAnyLocalAddress()) {
        try {
            return new NioAddress(TcpUtils.getFirstNonLocalhostInetAddress(), addr.port, addr.serializerId, addr.recvBufferSize, addr.messageSizeLimit);
        } catch (final SocketException e) {
            throw new DempsyException(e, false);
        }
    }
    // otherwise, the external address is the internal address.
    return addr;
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) DempsyException(net.dempsy.DempsyException)

Example 8 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, null);
        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 (ipToBindToProvider != null)
                LOGGER.warn("Both a supplier for the IP address to bind to (" + ipToBindToProvider.getClass().getName() + ") as well as \"useLocalHost\" was set. The address supplier will be ignored.");
        } else {
            if (ipToBindToProvider != null && ifNameToGetAddrFrom != null)
                LOGGER.warn("Both a supplier for the IP address to bind to (" + ipToBindToProvider.getClass().getName() + ") as well as the property " + CONFIG_KEY_RECEIVER_NETWORK_IF_NAME + " for " + NioReceiver.class.getPackage().getName() + ". The property will be ignored.");
        }
        InetAddress bindAddr = null;
        try {
            bindAddr = useLocalHost ? Inet4Address.getLocalHost() : (ipToBindToProvider == null ? // if someone set the variable for explicitly using a particular interface, then use it.
            (ifNameToGetAddrFrom == null ? null : TcpUtils.getFirstNonLocalhostInetAddress(ifNameToGetAddrFrom)) : ipToBindToProvider.ipToBindTo());
            binding = new Binding(bindAddr, internalPort);
            final InetSocketAddress inetSocketAddress = binding.bound;
            internalPort = inetSocketAddress.getPort();
            if (bindAddr == null)
                // this will be the wildcard address.
                bindAddr = binding.bound.getAddress();
            internal = new NioAddress(bindAddr, internalPort, serId, binding.recvBufferSize, this.maxMessageSize);
            address = resolver.getExternalAddresses(internal);
        } catch (final BindException be) {
            throw new DempsyException("Failed trying to bind to " + (bindAddr == null ? "*" : bindAddr.toString()) + ":" + internalPort, be, false);
        } catch (final IOException e) {
            throw new DempsyException(e, false);
        }
    }
    return address;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) DempsyException(net.dempsy.DempsyException) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 9 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)9 ContainerException (net.dempsy.container.ContainerException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)2 SafeString (net.dempsy.util.SafeString)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BindException (java.net.BindException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1