Example usage for org.springframework.messaging.support GenericMessage GenericMessage

List of usage examples for org.springframework.messaging.support GenericMessage GenericMessage

Introduction

In this page you can find the example usage for org.springframework.messaging.support GenericMessage GenericMessage.

Prototype

public GenericMessage(T payload) 

Source Link

Document

Create a new message with the given payload.

Usage

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
public void testMputPartial() throws Exception {
    Session<FTPFile> session = spyOnSession();
    doAnswer(invocation -> {//from  ww w  .  j  a  va2 s . com
        throw new IOException("Failed to send localSource2");
    }).when(session).write(Mockito.any(InputStream.class), Mockito.contains("localSource2"));
    try {
        this.inboundMPut.send(new GenericMessage<File>(getSourceLocalDirectory()));
        fail("expected exception");
    } catch (PartialSuccessException e) {
        assertEquals(3, e.getDerivedInput().size());
        assertEquals(1, e.getPartialResults().size());
        assertEquals("ftpTarget/localSource1.txt", e.getPartialResults().iterator().next());
        assertThat(e.getCause().getMessage(), containsString("Failed to send localSource2"));
    }
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
public void testMputRecursivePartial() throws Exception {
    Session<FTPFile> session = spyOnSession();
    File sourceLocalSubDirectory = new File(getSourceLocalDirectory(), "subLocalSource");
    assertTrue(sourceLocalSubDirectory.isDirectory());
    File extra = new File(sourceLocalSubDirectory, "subLocalSource2.txt");
    FileOutputStream writer = new FileOutputStream(extra);
    writer.write("foo".getBytes());
    writer.close();//from   ww  w  . j ava2 s. co  m
    doAnswer(invocation -> {
        throw new IOException("Failed to send subLocalSource2");
    }).when(session).write(Mockito.any(InputStream.class), Mockito.contains("subLocalSource2"));
    try {
        this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory()));
        fail("expected exception");
    } catch (PartialSuccessException e) {
        assertEquals(3, e.getDerivedInput().size());
        assertEquals(2, e.getPartialResults().size());
        assertThat(e.getCause(), Matchers.instanceOf(PartialSuccessException.class));
        PartialSuccessException cause = (PartialSuccessException) e.getCause();
        assertEquals(2, cause.getDerivedInput().size());
        assertEquals(1, cause.getPartialResults().size());
        assertThat(cause.getCause().getMessage(), containsString("Failed to send subLocalSource2"));
    }
    extra.delete();
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
public void testMessageSessionCallback() {
    this.inboundCallback.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);//w ww . j  av  a  2 s. com
    assertEquals("FOO", receive.getPayload());
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();/* w w w.  j a v  a2s . com*/

    this.inboundLs.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(List.class));
    List<String> files = (List<String>) receive.getPayload();
    assertEquals(2, files.size());
    assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));

    FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
    for (FTPFile ftpFile : ftpFiles) {
        if (!ftpFile.isDirectory()) {
            assertTrue(files.contains(ftpFile.getName()));
        }
    }
}

From source file:org.springframework.integration.ftp.session.FtpRemoteFileTemplateTests.java

@Test
public void testINT3412AppendStatRmdir() throws IOException {
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setExpression("'foobar.txt'");
    template.setFileNameGenerator(fileNameGenerator);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setUseTemporaryFileName(false);
    template.execute(session -> {//from   w w w . j  a v  a2  s  .  co m
        session.mkdir("foo/");
        return session.mkdir("foo/bar/");
    });
    template.append(new GenericMessage<String>("foo"));
    template.append(new GenericMessage<String>("bar"));
    assertTrue(template.exists("foo/foobar.txt"));
    template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
        try {
            FTPFile[] files = client.listFiles("foo/foobar.txt");
            assertEquals(6, files[0].getSize());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
        assertTrue(session.remove("foo/foobar.txt"));
        assertTrue(session.rmdir("foo/bar/"));
        FTPFile[] files = session.list("foo/");
        assertEquals(0, files.length);
        assertTrue(session.rmdir("foo/"));
    });
    assertFalse(template.getSession().exists("foo"));
}

From source file:org.springframework.integration.ftp.session.FtpRemoteFileTemplateTests.java

@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();/*from ww  w  .ja  v a 2 s  .  c o  m*/
    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();
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testGoodResult() {
    this.whichTest = 0;
    this.handler.handleMessage(new GenericMessage<String>("foo"));
    assertNull(this.output.receive(0));
    this.latch.countDown();
    Message<?> received = this.output.receive(10000);
    assertNotNull(received);//w  ww  .ja  v a  2 s. co m
    assertEquals("reply", received.getPayload());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

private void doTestCloseOnSendError(TcpConnection conn1, TcpConnection conn2,
        CachingClientConnectionFactory cccf) throws Exception {
    TcpConnection cached1 = cccf.getConnection();
    try {/*from www .  j  av  a 2s .  c om*/
        cached1.send(new GenericMessage<String>("foo"));
        fail("Expected IOException");
    } catch (IOException e) {
        assertEquals("Foo", e.getMessage());
    }
    // Before INT-3163 this failed with a timeout - connection not returned to pool after failure on send()
    TcpConnection cached2 = cccf.getConnection();
    assertTrue(cached1.getConnectionId().contains(conn1.getConnectionId()));
    assertTrue(cached2.getConnectionId().contains(conn2.getConnectionId()));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void integrationTest() throws Exception {
    TestingUtilities.waitListening(serverCf, null);
    new DirectFieldAccessor(this.clientAdapterCf).setPropertyValue("port", this.serverCf.getPort());

    this.outbound.send(new GenericMessage<>("Hello, world!"));
    Message<?> m = inbound.receive(10000);
    assertNotNull(m);/*  w  w w  . j  a  v  a2s . com*/
    String connectionId = m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class);

    // assert we use the same connection from the pool
    outbound.send(new GenericMessage<String>("Hello, world!"));
    m = inbound.receive(10000);
    assertNotNull(m);
    assertEquals(connectionId, m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
//   @Repeat(1000) // INT-3722
public void gatewayIntegrationTest() throws Exception {
    final List<String> connectionIds = new ArrayList<String>();
    final AtomicBoolean okToRun = new AtomicBoolean(true);
    Executors.newSingleThreadExecutor().execute(() -> {
        while (okToRun.get()) {
            Message<?> m = inbound.receive(1000);
            if (m != null) {
                connectionIds.add((String) m.getHeaders().get(IpHeaders.CONNECTION_ID));
                replies.send(MessageBuilder.withPayload("foo:" + new String((byte[]) m.getPayload()))
                        .copyHeaders(m.getHeaders()).build());
            }//w ww. ja va 2 s.co  m
        }
    });
    TestingUtilities.waitListening(serverCf, null);
    new DirectFieldAccessor(this.clientGatewayCf).setPropertyValue("port", this.serverCf.getPort());

    this.toGateway.send(new GenericMessage<>("Hello, world!"));
    Message<?> m = fromGateway.receive(1000);
    assertNotNull(m);
    assertEquals("foo:" + "Hello, world!", new String((byte[]) m.getPayload()));

    BlockingQueue<?> connections = TestUtils.getPropertyValue(this.gatewayCF, "pool.available",
            BlockingQueue.class);
    // wait until the connection is returned to the pool
    int n = 0;
    while (n++ < 100 && connections.size() == 0) {
        Thread.sleep(100);
    }

    // assert we use the same connection from the pool
    toGateway.send(new GenericMessage<String>("Hello, world2!"));
    m = fromGateway.receive(1000);
    assertNotNull(m);
    assertEquals("foo:" + "Hello, world2!", new String((byte[]) m.getPayload()));

    assertEquals(2, connectionIds.size());
    assertEquals(connectionIds.get(0), connectionIds.get(1));

    okToRun.set(false);
}