Search in sources :

Example 66 with MessagingException

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

the class ZkLockRegistryTests method testTwoThreadsSecondFailsToGetLock.

@Test
public void testTwoThreadsSecondFailsToGetLock() throws Exception {
    final ZookeeperLockRegistry registry = new ZookeeperLockRegistry(this.client);
    final Lock lock1 = registry.obtain("foo");
    lock1.lockInterruptibly();
    final AtomicBoolean locked = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
        Lock lock2 = registry.obtain("foo");
        locked.set(lock2.tryLock(200, TimeUnit.MILLISECONDS));
        latch.countDown();
        try {
            lock2.unlock();
        } catch (MessagingException e) {
            return e.getCause();
        }
        return null;
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertFalse(locked.get());
    lock1.unlock();
    Object ise = result.get(10, TimeUnit.SECONDS);
    assertThat(ise, instanceOf(IllegalMonitorStateException.class));
    assertThat(((Exception) ise).getMessage(), containsString("You do not own"));
    registry.destroy();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 67 with MessagingException

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

the class AbstractRemoteFileSynchronizerTests method testRollback.

@Test
public void testRollback() throws Exception {
    final AtomicBoolean failWhenCopyingBar = new AtomicBoolean(true);
    final AtomicInteger count = new AtomicInteger();
    SessionFactory<String> sf = new StringSessionFactory();
    AbstractInboundFileSynchronizer<String> sync = new AbstractInboundFileSynchronizer<String>(sf) {

        @Override
        protected boolean isFile(String file) {
            return true;
        }

        @Override
        protected String getFilename(String file) {
            return file;
        }

        @Override
        protected long getModified(String file) {
            return 0;
        }

        @Override
        protected boolean copyFileToLocalDirectory(String remoteDirectoryPath, String remoteFile, File localDirectory, Session<String> session) throws IOException {
            if ("bar".equals(remoteFile) && failWhenCopyingBar.getAndSet(false)) {
                throw new IOException("fail");
            }
            count.incrementAndGet();
            return true;
        }
    };
    sync.setFilter(new AcceptOnceFileListFilter<String>());
    sync.setRemoteDirectory("foo");
    try {
        sync.synchronizeToLocalDirectory(mock(File.class));
        assertEquals(1, count.get());
        fail("Expected exception");
    } catch (MessagingException e) {
        assertThat(e.getCause(), instanceOf(MessagingException.class));
        assertThat(e.getCause().getCause(), instanceOf(IOException.class));
        assertEquals("fail", e.getCause().getCause().getMessage());
    }
    sync.synchronizeToLocalDirectory(mock(File.class));
    assertEquals(3, count.get());
    sync.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) File(java.io.File) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 68 with MessagingException

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

the class FileOutboundChannelAdapterParserTests method adapterUsageWithFailMode.

@Test
public void adapterUsageWithFailMode() throws Exception {
    File testFile = new File("test/fileToFail.txt");
    if (testFile.exists()) {
        testFile.delete();
    }
    usageChannelWithFailMode.send(new GenericMessage<String>("Initial File Content:"));
    try {
        usageChannelWithFailMode.send(new GenericMessage<String>("String content:"));
    } catch (MessagingException e) {
        assertTrue(e.getMessage().contains("The destination file already exists at"));
        testFile.delete();
        return;
    }
    Assert.fail("Was expecting an Exception to be thrown.");
}
Also used : MessagingException(org.springframework.messaging.MessagingException) File(java.io.File) Test(org.junit.Test)

Example 69 with MessagingException

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

the class FtpRemoteFileTemplateTests method testFileCloseOnBadConnect.

@Test
public void testFileCloseOnBadConnect() throws Exception {
    @SuppressWarnings("unchecked") SessionFactory<FTPFile> sessionFactory = mock(SessionFactory.class);
    when(sessionFactory.getSession()).thenThrow(new RuntimeException("bar"));
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo"));
    template.afterPropertiesSet();
    File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write("foo".getBytes());
    fileOutputStream.close();
    try {
        template.send(new GenericMessage<File>(file));
        fail("exception expected");
    } catch (MessagingException e) {
        assertEquals("bar", e.getCause().getMessage());
    }
    File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    assertTrue(file.renameTo(newFile));
    file.delete();
    newFile.delete();
}
Also used : MessagingException(org.springframework.messaging.MessagingException) LiteralExpression(org.springframework.expression.common.LiteralExpression) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Example 70 with MessagingException

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

the class AsyncGatewayTests method testWithTimeout.

@Test
public void testWithTimeout() throws Exception {
    QueueChannel errors = new QueueChannel();
    this.gateway2.setOutputChannel(errors);
    this.gateway2.start();
    this.gateway2.handleMessage(MessageBuilder.withPayload("foo").setErrorChannel(errors).build());
    JmsTemplate template = new JmsTemplate(this.ccf);
    template.setReceiveTimeout(10000);
    final Message received = template.receive("asyncTest3");
    assertNotNull(received);
    org.springframework.messaging.Message<?> error = errors.receive(10000);
    assertNotNull(error);
    assertThat(error, instanceOf(ErrorMessage.class));
    assertThat(error.getPayload(), instanceOf(MessagingException.class));
    assertThat(((MessagingException) error.getPayload()).getCause(), instanceOf(JmsTimeoutException.class));
    assertEquals("foo", ((MessagingException) error.getPayload()).getFailedMessage().getPayload());
    this.gateway2.stop();
}
Also used : JmsTimeoutException(org.springframework.integration.jms.JmsTimeoutException) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessagingException(org.springframework.messaging.MessagingException) JmsTemplate(org.springframework.jms.core.JmsTemplate) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

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