Example usage for org.springframework.integration.channel QueueChannel receive

List of usage examples for org.springframework.integration.channel QueueChannel receive

Introduction

In this page you can find the example usage for org.springframework.integration.channel QueueChannel receive.

Prototype

@Override 
@Nullable
public Message<?> receive(long timeout) 

Source Link

Document

Receive the first available message from this channel.

Usage

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithByteArrayPayloadAndFilePathHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);//from   w w w  . j  a v a2 s  . c  o m
    handler.setOutputChannel(output);
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT.getBytes(DEFAULT_ENCODING))
            .setHeader(FileHeaders.ORIGINAL_FILE, sourceFile.getAbsolutePath()).build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithInputstreamPayloadAndFileInstanceHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);/*from   w  w w .ja  v a2  s  .  c o  m*/
    handler.setOutputChannel(output);

    InputStream is = new FileInputStream(sourceFile);

    Message<?> message = MessageBuilder.withPayload(is).setHeader(FileHeaders.ORIGINAL_FILE, sourceFile)
            .build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithInputstreamPayloadAndFilePathHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);//  w w w . j  ava2 s  .c  om
    handler.setOutputChannel(output);

    InputStream is = new FileInputStream(sourceFile);

    Message<?> message = MessageBuilder.withPayload(is)
            .setHeader(FileHeaders.ORIGINAL_FILE, sourceFile.getAbsolutePath()).build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void customFileNameGenerator() throws Exception {
    final String anyFilename = "fooBar.test";
    QueueChannel output = new QueueChannel();
    handler.setOutputChannel(output);/*from w w  w .j a  v a2  s  .  c  om*/
    handler.setFileNameGenerator(message -> anyFilename);
    Message<?> message = MessageBuilder.withPayload("test").build();
    handler.handleMessage(message);
    File result = (File) output.receive(0).getPayload();
    assertThat(result.getName(), is(anyFilename));
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void existingFileIgnored() throws Exception {
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build();
    QueueChannel output = new QueueChannel();
    File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile));
    handler.setCharset(DEFAULT_ENCODING);
    handler.setOutputChannel(output);/*from  www.  j a va 2 s . c o  m*/
    handler.setFileExistsMode(FileExistsMode.IGNORE);
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIs(result, "foo");
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void existingWritingFileIgnored() throws Exception {
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build();
    QueueChannel output = new QueueChannel();
    File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg.writing");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile));
    handler.setCharset(DEFAULT_ENCODING);
    handler.setOutputChannel(output);// ww w . j a  va  2 s .c  om
    handler.setFileExistsMode(FileExistsMode.IGNORE);
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    File destFile = (File) result.getPayload();
    assertNotSame(destFile, sourceFile);
    assertThat(destFile.exists(), is(false));
    assertThat(outFile.exists(), is(true));
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void existingWritingFileNotIgnoredIfEmptySuffix() throws Exception {
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build();
    QueueChannel output = new QueueChannel();
    File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg.writing");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile));
    handler.setCharset(DEFAULT_ENCODING);
    handler.setOutputChannel(output);//from   ww  w .  j  av  a 2s  . c  o  m
    handler.setFileExistsMode(FileExistsMode.IGNORE);
    handler.setTemporaryFileSuffix("");
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    File destFile = (File) result.getPayload();
    assertNotSame(destFile, sourceFile);
    assertFileContentIsMatching(result);
    assertThat(outFile.exists(), is(true));
    assertFileContentIs(outFile, "foo");
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void replaceIfDifferent() throws IOException {
    QueueChannel output = new QueueChannel();
    this.handler.setOutputChannel(output);
    this.handler.setPreserveTimestamp(true);
    this.handler.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
    this.handler.handleMessage(
            MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
                    .setHeader(FileHeaders.SET_MODIFIED, 42_000_000).build());
    Message<?> result = output.receive(0);
    assertFileContentIs(result, "foo");
    assertLastModifiedIs(result, 42_000_000);
    this.handler.handleMessage(
            MessageBuilder.withPayload("bar").setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
                    .setHeader(FileHeaders.SET_MODIFIED, 42_000_000).build());
    result = output.receive(0);/*from w w w  . j  a v a 2  s.  com*/
    assertFileContentIs(result, "foo"); // no overwrite - timestamp same
    assertLastModifiedIs(result, 42_000_000);
    this.handler.handleMessage(
            MessageBuilder.withPayload("bar").setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
                    .setHeader(FileHeaders.SET_MODIFIED, 43_000_000).build());
    result = output.receive(0);
    assertFileContentIs(result, "bar");
    assertLastModifiedIs(result, 43_000_000);
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void replaceIfDifferentFile() throws IOException {
    File file = new File(this.temp.newFolder(), "foo.txt");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(file));
    file.setLastModified(42_000_000);//w  w w. j a v a  2s  .co m
    QueueChannel output = new QueueChannel();
    this.handler.setOutputChannel(output);
    this.handler.setPreserveTimestamp(true);
    this.handler.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
    this.handler.handleMessage(MessageBuilder.withPayload(file).build());
    Message<?> result = output.receive(0);
    assertFileContentIs(result, "foo");
    assertLastModifiedIs(result, 42_000_000);
    FileCopyUtils.copy("bar".getBytes(), new FileOutputStream(file));
    file.setLastModified(42_000_000);
    this.handler.handleMessage(MessageBuilder.withPayload(file).build());
    result = output.receive(0);
    assertFileContentIs(result, "foo"); // no overwrite - timestamp same
    assertLastModifiedIs(result, 42_000_000);
    file.setLastModified(43_000_000);
    this.handler.handleMessage(MessageBuilder.withPayload(file).build());
    result = output.receive(0);
    assertFileContentIs(result, "bar");
    assertLastModifiedIs(result, 43_000_000);
}

From source file:org.springframework.integration.file.tail.FileTailingMessageProducerTests.java

private void testGuts(FileTailingMessageProducerSupport adapter, String field) throws Exception {
    this.adapter = adapter;
    final List<FileTailingEvent> events = new ArrayList<FileTailingEvent>();
    adapter.setApplicationEventPublisher(new ApplicationEventPublisher() {
        @Override/*from w w w .jav a 2 s  .c  o  m*/
        public void publishEvent(ApplicationEvent event) {
            FileTailingEvent tailEvent = (FileTailingEvent) event;
            logger.warn(event);
            events.add(tailEvent);
        }
    });
    adapter.setFile(new File(testDir, "foo"));
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(outputChannel);
    adapter.setTailAttemptsDelay(500);
    adapter.afterPropertiesSet();
    File file = new File(testDir, "foo");
    File renamed = new File(testDir, "bar");
    file.delete();
    renamed.delete();
    adapter.start();
    waitForField(adapter, field);
    FileOutputStream foo = new FileOutputStream(file);
    for (int i = 0; i < 50; i++) {
        foo.write(("hello" + i + "\n").getBytes());
    }
    foo.flush();
    foo.close();
    for (int i = 0; i < 50; i++) {
        Message<?> message = outputChannel.receive(5000);
        assertNotNull("expected a non-null message", message);
        assertEquals("hello" + i, message.getPayload());
    }
    file.renameTo(renamed);
    file = new File(testDir, "foo");
    foo = new FileOutputStream(file);
    if (adapter instanceof ApacheCommonsFileTailingMessageProducer) {
        Thread.sleep(1000);
    }
    for (int i = 50; i < 100; i++) {
        foo.write(("hello" + i + "\n").getBytes());
    }
    foo.flush();
    foo.close();
    for (int i = 50; i < 100; i++) {
        Message<?> message = outputChannel.receive(10000);
        assertNotNull("expected a non-null message", message);
        assertEquals("hello" + i, message.getPayload());
    }
}