Search in sources :

Example 21 with ErrorMessage

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

the class TcpNioConnectionReadTests method testClosureMidMessageGuts.

private void testClosureMidMessageGuts(AbstractByteArraySerializer serializer, String shortMessage) throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final List<TcpConnection> added = new ArrayList<TcpConnection>();
    final List<TcpConnection> removed = new ArrayList<TcpConnection>();
    final CountDownLatch errorMessageLetch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorMessageRef = new AtomicReference<Throwable>();
    AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
        if (message instanceof ErrorMessage) {
            errorMessageRef.set(((ErrorMessage) message).getPayload());
            errorMessageLetch.countDown();
        }
        return false;
    }, new TcpSender() {

        @Override
        public void addNewConnection(TcpConnection connection) {
            added.add(connection);
            semaphore.release();
        }

        @Override
        public void removeDeadConnection(TcpConnection connection) {
            removed.add(connection);
            semaphore.release();
        }
    });
    Socket socket = SocketFactory.getDefault().createSocket("localhost", scf.getPort());
    socket.getOutputStream().write(shortMessage.getBytes());
    socket.close();
    whileOpen(semaphore, added);
    assertEquals(1, added.size());
    assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
    assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Connection is closed"), containsString("Socket closed during message assembly"), containsString("Stream closed after 2 of 3")));
    assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
    assertTrue(removed.size() > 0);
    scf.stop();
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Socket(java.net.Socket)

Example 22 with ErrorMessage

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

the class TcpNioConnectionReadTests method testCloseCleanupNoData.

/**
 * Tests socket closure when no data received.
 * @throws Exception
 */
@Test
public void testCloseCleanupNoData() throws Exception {
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    serializer.setMaxMessageSize(1024);
    final Semaphore semaphore = new Semaphore(0);
    final List<TcpConnection> added = new ArrayList<TcpConnection>();
    final List<TcpConnection> removed = new ArrayList<TcpConnection>();
    final CountDownLatch errorMessageLetch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorMessageRef = new AtomicReference<Throwable>();
    AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
        if (message instanceof ErrorMessage) {
            errorMessageRef.set(((ErrorMessage) message).getPayload());
            errorMessageLetch.countDown();
        }
        return false;
    }, new TcpSender() {

        @Override
        public void addNewConnection(TcpConnection connection) {
            added.add(connection);
            semaphore.release();
        }

        @Override
        public void removeDeadConnection(TcpConnection connection) {
            removed.add(connection);
            semaphore.release();
        }
    });
    Socket socket = SocketFactory.getDefault().createSocket("localhost", scf.getPort());
    socket.close();
    whileOpen(semaphore, added);
    assertEquals(1, added.size());
    assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
    assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Connection is closed"), containsString("Stream closed after 2 of 3")));
    assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
    assertTrue(removed.size() > 0);
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Socket(java.net.Socket) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 23 with ErrorMessage

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

the class RedisMessageGroupStoreTests method testJsonSerialization.

@Test
@RedisAvailable
public void testJsonSerialization() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    Message<?> genericMessage = new GenericMessage<>(new Date());
    Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
    Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
    ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
    store.addMessagesToGroup(this.groupId, genericMessage, mutableMessage, adviceMessage, errorMessage);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(4, messageGroup.size());
    List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
    assertEquals(genericMessage, messages.get(0));
    assertEquals(mutableMessage, messages.get(1));
    assertEquals(adviceMessage, messages.get(2));
    Message<?> errorMessageResult = messages.get(3);
    assertEquals(errorMessage.getHeaders(), errorMessageResult.getHeaders());
    assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
    assertEquals(errorMessage.getOriginalMessage(), ((ErrorMessage) errorMessageResult).getOriginalMessage());
    assertEquals(errorMessage.getPayload().getMessage(), ((ErrorMessage) errorMessageResult).getPayload().getMessage());
    Message<Foo> fooMessage = new GenericMessage<>(new Foo("foo"));
    try {
        store.addMessageToGroup(this.groupId, fooMessage).getMessages().iterator().next();
        fail("SerializationException expected");
    } catch (Exception e) {
        assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
        assertThat(e.getMessage(), containsString("The class with " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo and name of " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo " + "is not in the trusted packages:"));
    }
    mapper = JacksonJsonUtils.messagingAwareMapper(getClass().getPackage().getName());
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
    mapper = JacksonJsonUtils.messagingAwareMapper("*");
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
}
Also used : GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) AdviceMessage(org.springframework.integration.message.AdviceMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) AdviceMessage(org.springframework.integration.message.AdviceMessage) Date(java.util.Date) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 24 with ErrorMessage

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

the class GroovyServiceActivatorTests method invalidInlineScript.

// INT-2399
@Test(expected = MessageHandlingException.class)
public void invalidInlineScript() throws Exception {
    Message<?> message = new ErrorMessage(new ReplyRequiredException(new GenericMessage<String>("test"), "reply required!"));
    try {
        this.invalidInlineScript.send(message);
        fail("MessageHandlingException expected!");
    } catch (Exception e) {
        Throwable cause = e.getCause();
        assertEquals(MissingPropertyException.class, cause.getClass());
        assertThat(cause.getMessage(), Matchers.containsString("No such property: ReplyRequiredException for class: script"));
        throw e;
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ReplyRequiredException(org.springframework.integration.handler.ReplyRequiredException) MissingPropertyException(groovy.lang.MissingPropertyException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) ReplyRequiredException(org.springframework.integration.handler.ReplyRequiredException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MissingPropertyException(groovy.lang.MissingPropertyException) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Test(org.junit.Test)

Example 25 with ErrorMessage

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

the class ChannelPublishingJmsMessageListenerTests method testBadConversion.

@Test
public void testBadConversion() throws Exception {
    final QueueChannel requestChannel = new QueueChannel();
    ChannelPublishingJmsMessageListener listener = new ChannelPublishingJmsMessageListener();
    Log logger = spy(TestUtils.getPropertyValue(listener, "logger", Log.class));
    doNothing().when(logger).error(anyString(), any(Throwable.class));
    new DirectFieldAccessor(listener).setPropertyValue("logger", logger);
    listener.setRequestChannel(requestChannel);
    QueueChannel errorChannel = new QueueChannel();
    listener.setErrorChannel(errorChannel);
    listener.setBeanFactory(mock(BeanFactory.class));
    listener.setMessageConverter(new TestMessageConverter() {

        @Override
        public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {
            return null;
        }
    });
    listener.afterPropertiesSet();
    javax.jms.Message jmsMessage = session.createTextMessage("test");
    listener.onMessage(jmsMessage, mock(Session.class));
    ErrorMessage received = (ErrorMessage) errorChannel.receive(0);
    assertNotNull(received);
    assertThat(received.getPayload().getMessage(), startsWith("Inbound conversion failed"));
    listener.stop();
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) Log(org.apache.commons.logging.Log) JMSException(javax.jms.JMSException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

ErrorMessage (org.springframework.messaging.support.ErrorMessage)59 Test (org.junit.Test)47 CountDownLatch (java.util.concurrent.CountDownLatch)17 GenericMessage (org.springframework.messaging.support.GenericMessage)17 MessagingException (org.springframework.messaging.MessagingException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Message (org.springframework.messaging.Message)12 QueueChannel (org.springframework.integration.channel.QueueChannel)11 MessageHandlingException (org.springframework.messaging.MessageHandlingException)11 Socket (java.net.Socket)9 ArrayList (java.util.ArrayList)7 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)7 Semaphore (java.util.concurrent.Semaphore)6 Log (org.apache.commons.logging.Log)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 ServerSocket (java.net.ServerSocket)3