Search in sources :

Example 46 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class ConnectionEventTests method testOutboundGatewayNoConnectionEvents.

@Test
public void testOutboundGatewayNoConnectionEvents() {
    TcpOutboundGateway gw = new TcpOutboundGateway();
    AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
    };
    final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
    ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            theEvent.set(event);
        }
    });
    gw.setConnectionFactory(ccf);
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
    gw.start();
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertEquals("bar", event.getConnectionId());
    MessagingException messagingException = (MessagingException) event.getCause();
    assertSame(message, messagingException.getFailedMessage());
    assertEquals("Cannot correlate response - no pending reply for bar", messagingException.getMessage());
    message = new GenericMessage<String>("foo");
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertNull(event.getConnectionId());
    messagingException = (MessagingException) event.getCause();
    assertSame(message, messagingException.getFailedMessage());
    assertEquals("Cannot correlate response - no connection id", messagingException.getMessage());
    gw.stop();
    ccf.stop();
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) ApplicationEvent(org.springframework.context.ApplicationEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Test(org.junit.Test)

Example 47 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class ConnectionFactoryShutDownTests method testShutdownDoesntDeadlock.

@Test
public void testShutdownDoesntDeadlock() throws Exception {
    final AbstractConnectionFactory factory = new AbstractConnectionFactory(0) {

        @Override
        public TcpConnection getConnection() {
            return null;
        }
    };
    factory.setActive(true);
    Executor executor = factory.getTaskExecutor();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    executor.execute(() -> {
        latch1.countDown();
        try {
            while (true) {
                factory.getTaskExecutor();
                Thread.sleep(10);
            }
        } catch (MessagingException e1) {
        } catch (InterruptedException e2) {
            Thread.currentThread().interrupt();
        }
        latch2.countDown();
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    StopWatch watch = new StopWatch();
    watch.start();
    factory.stop();
    watch.stop();
    assertTrue("Expected < 10000, was: " + watch.getLastTaskTimeMillis(), watch.getLastTaskTimeMillis() < 10000);
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
}
Also used : Executor(java.util.concurrent.Executor) MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 48 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class HelloWorldInterceptor method onMessage.

@Override
public boolean onMessage(Message<?> message) {
    if (!this.negotiated) {
        synchronized (this) {
            if (!this.negotiated) {
                Object payload = message.getPayload();
                logger.debug(this.toString() + " received " + payload);
                if (this.isServer()) {
                    if (payload.equals(hello)) {
                        try {
                            logger.debug(this.toString() + " sending " + this.world);
                            super.send(MessageBuilder.withPayload(world).build());
                            this.negotiated = true;
                            return true;
                        } catch (Exception e) {
                            throw new MessagingException("Negotiation error", e);
                        }
                    } else {
                        throw new MessagingException("Negotiation error, expected '" + hello + "' received '" + payload + "'");
                    }
                } else {
                    if (payload.equals(world)) {
                        this.negotiated = true;
                        this.negotiationSemaphore.release();
                    } else {
                        throw new MessagingException("Negotiation error - expected '" + world + "' received " + payload);
                    }
                    return true;
                }
            }
        }
    }
    try {
        return super.onMessage(message);
    } finally {
        // on the server side, we don't want to close if we are expecting a response
        if (!(this.isServer() && this.hasRealSender()) && !this.pendingSend) {
            this.checkDeferredClose();
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MessagingException(org.springframework.messaging.MessagingException)

Example 49 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class TcpNetConnection method send.

@Override
@SuppressWarnings("unchecked")
public synchronized void send(Message<?> message) throws Exception {
    if (this.socketOutputStream == null) {
        int writeBufferSize = this.socket.getSendBufferSize();
        this.socketOutputStream = new BufferedOutputStream(this.socket.getOutputStream(), writeBufferSize > 0 ? writeBufferSize : 8192);
    }
    Object object = this.getMapper().fromMessage(message);
    this.lastSend = System.currentTimeMillis();
    try {
        ((Serializer<Object>) this.getSerializer()).serialize(object, this.socketOutputStream);
        this.socketOutputStream.flush();
    } catch (Exception e) {
        this.publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
        this.closeConnection(true);
        throw e;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(getConnectionId() + " Message sent " + message);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) BufferedOutputStream(java.io.BufferedOutputStream) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) SoftEndOfStreamException(org.springframework.integration.ip.tcp.serializer.SoftEndOfStreamException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) Serializer(org.springframework.core.serializer.Serializer)

Example 50 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class TcpNioConnection method send.

@Override
@SuppressWarnings("unchecked")
public void send(Message<?> message) throws Exception {
    synchronized (this.socketChannel) {
        if (this.bufferedOutputStream == null) {
            int writeBufferSize = this.socketChannel.socket().getSendBufferSize();
            this.bufferedOutputStream = new BufferedOutputStream(this.getChannelOutputStream(), writeBufferSize > 0 ? writeBufferSize : 8192);
        }
        Object object = this.getMapper().fromMessage(message);
        this.lastSend = System.currentTimeMillis();
        try {
            ((Serializer<Object>) this.getSerializer()).serialize(object, this.bufferedOutputStream);
            this.bufferedOutputStream.flush();
        } catch (Exception e) {
            this.publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
            this.closeConnection(true);
            throw e;
        }
        if (logger.isDebugEnabled()) {
            logger.debug(getConnectionId() + " Message sent " + message);
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) BufferedOutputStream(java.io.BufferedOutputStream) MessagingException(org.springframework.messaging.MessagingException) SoftEndOfStreamException(org.springframework.integration.ip.tcp.serializer.SoftEndOfStreamException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SocketTimeoutException(java.net.SocketTimeoutException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Serializer(org.springframework.core.serializer.Serializer)

Aggregations

MessagingException (org.springframework.messaging.MessagingException)143 Test (org.junit.Test)60 IOException (java.io.IOException)25 Message (org.springframework.messaging.Message)23 QueueChannel (org.springframework.integration.channel.QueueChannel)22 ErrorMessage (org.springframework.messaging.support.ErrorMessage)21 CountDownLatch (java.util.concurrent.CountDownLatch)17 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 MessageHandlingException (org.springframework.messaging.MessageHandlingException)15 GenericMessage (org.springframework.messaging.support.GenericMessage)15 MessageHandler (org.springframework.messaging.MessageHandler)14 File (java.io.File)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 DirectChannel (org.springframework.integration.channel.DirectChannel)12 ArrayList (java.util.ArrayList)11 PollableChannel (org.springframework.messaging.PollableChannel)11 Lock (java.util.concurrent.locks.Lock)8 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)8