Search in sources :

Example 56 with ErrorMessage

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

the class TcpNioConnectionReadTests method testCloseCleanupPartialData.

/**
 * Tests socket closure when no data received.
 * @throws Exception
 */
@Test
public void testCloseCleanupPartialData() 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.getOutputStream().write("partial".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")));
    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 57 with ErrorMessage

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

the class AbstractMongoDbMessageStoreTests method testInt3076ErrorMessage.

@Test
@MongoDbAvailable
public void testInt3076ErrorMessage() throws Exception {
    MessageStore store = this.getMessageStore();
    Person p = new Person();
    p.setFname("John");
    p.setLname("Doe");
    Message<Person> failedMessage = MessageBuilder.withPayload(p).build();
    MessagingException messagingException;
    try {
        throw new RuntimeException("intentional");
    } catch (Exception e) {
        messagingException = new MessagingException(failedMessage, "intentional MessagingException", e);
    }
    Message<?> messageToStore = new ErrorMessage(messagingException);
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertTrue(retrievedMessage instanceof ErrorMessage);
    assertThat(retrievedMessage.getPayload(), Matchers.instanceOf(MessagingException.class));
    assertThat(((MessagingException) retrievedMessage.getPayload()).getMessage(), containsString("intentional MessagingException"));
    assertEquals(failedMessage, ((MessagingException) retrievedMessage.getPayload()).getFailedMessage());
    assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessagingException(org.springframework.messaging.MessagingException) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 58 with ErrorMessage

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

the class ChatMessageListeningEndpointTests method testWithErrorChannel.

@Test
public void testWithErrorChannel() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    XMPPConnection connection = mock(XMPPConnection.class);
    bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
    ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
    DirectChannel outChannel = new DirectChannel();
    outChannel.subscribe(message -> {
        throw new RuntimeException("ooops");
    });
    PollableChannel errorChannel = new QueueChannel();
    endpoint.setBeanFactory(bf);
    endpoint.setOutputChannel(outChannel);
    endpoint.setErrorChannel(errorChannel);
    endpoint.afterPropertiesSet();
    StanzaListener listener = (StanzaListener) TestUtils.getPropertyValue(endpoint, "stanzaListener");
    Message smackMessage = new Message(JidCreate.from("kermit@frog.com"));
    smackMessage.setBody("hello");
    smackMessage.setThread("1234");
    listener.processStanza(smackMessage);
    ErrorMessage msg = (ErrorMessage) errorChannel.receive();
    assertEquals("hello", ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.jivesoftware.smack.packet.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) StanzaListener(org.jivesoftware.smack.StanzaListener) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 59 with ErrorMessage

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

the class MessageMethodArgumentResolverTests method resolveMessageSubclassMatch.

@Test
public void resolveMessageSubclassMatch() throws Exception {
    ErrorMessage message = new ErrorMessage(new UnsupportedOperationException());
    MethodParameter parameter = new MethodParameter(this.method, 4);
    assertThat(this.resolver.supportsParameter(parameter)).isTrue();
    assertThat(this.resolver.resolveArgument(parameter, message)).isSameAs(message);
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.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