INT-4381: MessageSources refactoring (#2517)
* INT-4381: MessageSources refactoring JIRA: https://jira.spring.io/browse/INT-4381 * Make all the out-of-the-box `MessageSource` implementations based on the `AbstractMessageSource` * Fix `JdbcPollingChannelAdapterIntegrationTests` for sporadic failure because of `fixed-rate` for the poller * Fix HeaderEnricherTests race condition The `errorChannel()` expect an error in the `testErrorChannel` after `1000` ms, but at the same time the `poller` in configured for the `3000` ms. * Increase all the timeouts for replies * Decrease a `fixed-delay` on the `poller` * Some other code style polishing for the `HeaderEnricherTests`
This commit is contained in:
committed by
Gary Russell
parent
d6c8baf50f
commit
0d0605be78
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,11 +20,8 @@ import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.integration.context.IntegrationObjectSupport;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.integration.endpoint.AbstractMessageSource;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* A pollable source for receiving bytes from an {@link InputStream}.
|
||||
@@ -32,11 +29,9 @@ import org.springframework.messaging.support.GenericMessage;
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class ByteStreamReadingMessageSource extends IntegrationObjectSupport implements MessageSource<byte[]> {
|
||||
public class ByteStreamReadingMessageSource extends AbstractMessageSource<byte[]> {
|
||||
|
||||
private BufferedInputStream stream;
|
||||
|
||||
private Object streamMonitor;
|
||||
private final BufferedInputStream stream;
|
||||
|
||||
private int bytesPerMessage = 1024;
|
||||
|
||||
@@ -48,7 +43,6 @@ public class ByteStreamReadingMessageSource extends IntegrationObjectSupport imp
|
||||
}
|
||||
|
||||
public ByteStreamReadingMessageSource(InputStream stream, int bufferSize) {
|
||||
this.streamMonitor = stream;
|
||||
if (stream instanceof BufferedInputStream) {
|
||||
this.stream = (BufferedInputStream) stream;
|
||||
}
|
||||
@@ -74,11 +68,12 @@ public class ByteStreamReadingMessageSource extends IntegrationObjectSupport imp
|
||||
return "stream:stdin-channel-adapter(byte)";
|
||||
}
|
||||
|
||||
public Message<byte[]> receive() {
|
||||
@Override
|
||||
protected byte[] doReceive() {
|
||||
try {
|
||||
byte[] bytes;
|
||||
int bytesRead = 0;
|
||||
synchronized (this.streamMonitor) {
|
||||
synchronized (this.stream) {
|
||||
if (this.stream.available() == 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -89,12 +84,12 @@ public class ByteStreamReadingMessageSource extends IntegrationObjectSupport imp
|
||||
return null;
|
||||
}
|
||||
if (!this.shouldTruncate) {
|
||||
return new GenericMessage<byte[]>(bytes);
|
||||
return bytes;
|
||||
}
|
||||
else {
|
||||
byte[] result = new byte[bytesRead];
|
||||
System.arraycopy(bytes, 0, result, 0, result.length);
|
||||
return new GenericMessage<byte[]>(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,11 +24,8 @@ import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.integration.context.IntegrationObjectSupport;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.integration.endpoint.AbstractMessageSource;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -36,14 +33,13 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class CharacterStreamReadingMessageSource extends IntegrationObjectSupport implements MessageSource<String>,
|
||||
ApplicationEventPublisherAware {
|
||||
public class CharacterStreamReadingMessageSource extends AbstractMessageSource<String>
|
||||
implements ApplicationEventPublisherAware {
|
||||
|
||||
private final BufferedReader reader;
|
||||
|
||||
private final Object monitor;
|
||||
|
||||
private final boolean blockToDetectEOF;
|
||||
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
@@ -91,7 +87,6 @@ public class CharacterStreamReadingMessageSource extends IntegrationObjectSuppor
|
||||
*/
|
||||
public CharacterStreamReadingMessageSource(Reader reader, int bufferSize, boolean blockToDetectEOF) {
|
||||
Assert.notNull(reader, "reader must not be null");
|
||||
this.monitor = reader;
|
||||
if (reader instanceof BufferedReader) {
|
||||
this.reader = (BufferedReader) reader;
|
||||
}
|
||||
@@ -115,9 +110,9 @@ public class CharacterStreamReadingMessageSource extends IntegrationObjectSuppor
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<String> receive() {
|
||||
public String doReceive() {
|
||||
try {
|
||||
synchronized (this.monitor) {
|
||||
synchronized (this.reader) {
|
||||
if (!this.blockToDetectEOF && !this.reader.ready()) {
|
||||
return null;
|
||||
}
|
||||
@@ -125,7 +120,7 @@ public class CharacterStreamReadingMessageSource extends IntegrationObjectSuppor
|
||||
if (line == null && this.applicationEventPublisher != null) {
|
||||
this.applicationEventPublisher.publishEvent(new StreamClosedEvent(this));
|
||||
}
|
||||
return (line != null) ? new GenericMessage<String>(line) : null;
|
||||
return line;
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
Reference in New Issue
Block a user