use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNetCrLfClientMode.
@Test
public void testNetCrLfClientMode() throws Exception {
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
serverSocket.set(server);
latch.countDown();
Socket socket = server.accept();
int i = 0;
while (true) {
byte[] b = new byte[6];
readFully(socket.getInputStream(), b);
b = ("Reply" + (++i) + "\r\n").getBytes();
socket.getOutputStream().write(b);
}
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
noopPublisher(ccf);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
ccf.setSerializer(serializer);
ccf.setDeserializer(serializer);
ccf.setSoTimeout(Integer.MAX_VALUE);
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
handler.setClientMode(true);
handler.setRetryInterval(10000);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(1);
taskScheduler.initialize();
handler.setTaskScheduler(taskScheduler);
handler.start();
adapter.start();
handler.handleMessage(MessageBuilder.withPayload("Test").build());
handler.handleMessage(MessageBuilder.withPayload("Test").build());
Message<?> mOut = channel.receive(10000);
assertThat(mOut).isNotNull();
assertThat(new String((byte[]) mOut.getPayload())).isEqualTo("Reply1");
mOut = channel.receive(10000);
assertThat(mOut).isNotNull();
assertThat(new String((byte[]) mOut.getPayload())).isEqualTo("Reply2");
done.set(true);
handler.stop();
handler.start();
handler.stop();
adapter.stop();
ccf.stop();
serverSocket.get().close();
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class ImapMailReceiverTests method testConnectionException.
@Test
public void testConnectionException() throws Exception {
ImapMailReceiver mailReceiver = new ImapMailReceiver("imap:foo");
ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(mailReceiver);
final AtomicReference<Object> theEvent = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
adapter.setApplicationEventPublisher(event -> {
theEvent.set(event);
latch.countDown();
});
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
adapter.setTaskScheduler(taskScheduler);
adapter.setReconnectDelay(1);
adapter.start();
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(theEvent.get().toString()).endsWith("cause=java.lang.IllegalStateException: Failure in 'idle' task. Will resubmit.]");
adapter.stop();
taskScheduler.destroy();
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class ImapMailReceiverTests method testIdleWithServerGuts.
public void testIdleWithServerGuts(ImapMailReceiver receiver, boolean mapped, boolean simple) throws Exception {
receiver.setMaxFetchSize(1);
receiver.setShouldDeleteMessages(false);
receiver.setShouldMarkMessagesAsRead(true);
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
setUpScheduler(receiver, taskScheduler);
receiver.setUserFlag("testSIUserFlag");
receiver.afterPropertiesSet();
ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
adapter.setTaskScheduler(taskScheduler);
adapter.setReconnectDelay(1);
adapter.start();
MimeMessage message = GreenMailUtil.createTextEmail("Foo <foo@bar>", "Bar <bar@baz>", "Test Email", "foo\r\n", imapIdleServer.getImap().getServerSetup());
message.setRecipients(Message.RecipientType.CC, "a@b, c@d");
message.setRecipients(Message.RecipientType.BCC, "e@f, g@h");
user.deliver(message);
user.deliver(GreenMailUtil.createTextEmail("to", "Bar <bar@baz>", "subject", "body", imapIdleServer.getImap().getServerSetup()));
if (!mapped) {
@SuppressWarnings("unchecked") org.springframework.messaging.Message<MimeMessage> received = (org.springframework.messaging.Message<MimeMessage>) channel.receive(10000);
assertThat(received).isNotNull();
assertThat(received.getPayload().getReceivedDate()).isNotNull();
assertThat(received.getPayload().getLineCount() > -1).isTrue();
if (simple) {
assertThat(received.getPayload().getContent()).isEqualTo("foo\r\n");
} else {
assertThat(received.getPayload().getContent()).isEqualTo("foo");
}
} else {
org.springframework.messaging.Message<?> received = channel.receive(10000);
assertThat(received).isNotNull();
MessageHeaders headers = received.getHeaders();
assertThat(headers.get(MailHeaders.RAW_HEADERS)).isNotNull();
assertThat(headers.get(MailHeaders.CONTENT_TYPE)).isEqualTo("TEXT/PLAIN; charset=us-ascii");
assertThat(headers.get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeTypeUtils.TEXT_PLAIN_VALUE);
assertThat(headers.get(MailHeaders.FROM)).isEqualTo("Bar <bar@baz>");
String[] toHeader = headers.get(MailHeaders.TO, String[].class);
assertThat(toHeader).isNotEmpty();
assertThat(toHeader[0]).isEqualTo("Foo <foo@bar>");
assertThat(Arrays.toString(headers.get(MailHeaders.CC, String[].class))).isEqualTo("[a@b, c@d]");
assertThat(Arrays.toString(headers.get(MailHeaders.BCC, String[].class))).isEqualTo("[e@f, g@h]");
assertThat(headers.get(MailHeaders.SUBJECT)).isEqualTo("Test Email");
if (simple) {
assertThat(received.getPayload()).isEqualTo("foo\r\n");
} else {
assertThat(received.getPayload()).isEqualTo("foo");
}
}
// new message after idle
assertThat(channel.receive(20000)).isNotNull();
// no new message after second and third idle
assertThat(channel.receive(100)).isNull();
adapter.stop();
taskScheduler.shutdown();
assertThat(imapSearches.stores.get(0)).contains("testSIUserFlag");
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class FileTailingMessageProducerTests method testGuts.
private void testGuts(FileTailingMessageProducerSupport adapter, String field) throws Exception {
this.adapter = adapter;
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
adapter.setTaskScheduler(taskScheduler);
final List<FileTailingEvent> events = new ArrayList<>();
adapter.setApplicationEventPublisher(event -> {
FileTailingEvent tailEvent = (FileTailingEvent) event;
logger.debug(event);
events.add(tailEvent);
});
adapter.setFile(new File(testDir, "foo"));
QueueChannel outputChannel = new QueueChannel();
adapter.setOutputChannel(outputChannel);
adapter.setTailAttemptsDelay(500);
adapter.setBeanFactory(mock(BeanFactory.class));
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(10000);
assertThat(message).as("expected a non-null message").isNotNull();
assertThat(message.getPayload()).isEqualTo("hello" + i);
}
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);
assertThat(message).as("expected a non-null message").isNotNull();
assertThat(message.getPayload()).isEqualTo("hello" + i);
assertThat(message.getHeaders().get(FileHeaders.ORIGINAL_FILE)).isEqualTo(file);
assertThat(message.getHeaders().get(FileHeaders.FILENAME)).isEqualTo(file.getName());
}
assertThat(events.size()).isGreaterThanOrEqualTo(1);
taskScheduler.destroy();
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class FileTailingMessageProducerTests method testIdleEvent.
@Test
public void testIdleEvent() throws Exception {
ApacheCommonsFileTailingMessageProducer adapter = new ApacheCommonsFileTailingMessageProducer();
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
adapter.setTaskScheduler(taskScheduler);
CountDownLatch idleCountDownLatch = new CountDownLatch(1);
CountDownLatch fileExistCountDownLatch = new CountDownLatch(1);
adapter.setApplicationEventPublisher(event -> {
if (event instanceof FileTailingIdleEvent) {
idleCountDownLatch.countDown();
}
if (event instanceof FileTailingEvent) {
FileTailingEvent fileTailingEvent = (FileTailingEvent) event;
if (fileTailingEvent.getMessage().contains("File not found")) {
fileExistCountDownLatch.countDown();
}
}
});
AtomicBoolean existsCalled = new AtomicBoolean();
File file = new File(this.testDir, "foo") {
@Override
public boolean exists() {
try {
return super.exists();
} finally {
existsCalled.set(true);
}
}
};
file.delete();
adapter.setFile(file);
adapter.setOutputChannel(new NullChannel());
adapter.setIdleEventInterval(10);
adapter.afterPropertiesSet();
adapter.start();
boolean noFile = fileExistCountDownLatch.await(10, TimeUnit.SECONDS);
assertThat(noFile).as("file does not exist event did not emit ").isTrue();
boolean noEvent = idleCountDownLatch.await(100, TimeUnit.MILLISECONDS);
assertThat(noEvent).as("event should not emit when no file exit").isFalse();
assertThat(existsCalled.get()).isTrue();
file.createNewFile();
boolean eventRaised = idleCountDownLatch.await(10, TimeUnit.SECONDS);
assertThat(eventRaised).as("idle event did not emit").isTrue();
adapter.stop();
file.delete();
}
Aggregations