问题
I am very new to Spring Integration and trying to figure out how to return a stream of data, but cannot find any examples. I found documentation around the ByteStreamWritingMessageHandler, but it seems kind of vague to me and doesn't provide an example of usage (other than bean definition). I'm assuming what I need to do is define a channel that uses the ByteStreamWritingMessageHandler and reference it in my inbound-gateway (via reply-channel), but I have some questions:
First, am I right? Can I use the default channel type? Do I need a channel adapter? Can I just return a ByteArrayOutputStream from my service method? What would the channel definition look like?
Any help would be greatly appreciated.
---------------UPDATE-----------
Our current endpoints are structured like this:
<int:channel id="httpReplyChannel"/>
<int:channel id="exampleService.exampleMethod"/>
<int-http:inbound-gateway path="/example"
supported-methods="POST"
request-channel="exampleService.exampleMethod"
request-payload-type="java.lang.Integer"
reply-channel="httpReplyChannel"
message-converters="jsonMessageConverter"
mapped-request-headers="*"/>
<int:service-activator input-channel="exampleService.exampleMethod"
ref="exampleService"
method="exampleMethod"/>
So we use two channels one for inbound and one for outbound and use the reply-channel attribute of the http:inbound-gateway to configure the outbound channel. I would like to follow the same pattern, but create a new outbound channel. My problem is that I'm not sure what type of channel would work best for returning a stream. The endpoint will return an a stream containing an image directly to the browser (which will make the request via the HTML img tag). So, I need my exampleMethod to return a stream (ByteArrayOutputStream) and I need to have access to dynamically set headers based on what type of image is being returned.
回答1:
If you are saying you want to receive a request via an http inbound adapter and somehow open a stream to pump multiple data out then, no that is not currently supported.
Spring Integration is primarily a messaging framework, not a streaming framework.
The Streaming adapters you refer to are for simple one-way integration.
All gateways are strictly request/response. There are certain adapters (such as tcp) where you can perform this kind of streaming (with collaborating inbound/outbound adapters), but there is not currently anything in the http space (although we are currently looking at a number of technologies that will enable such in the future).
EDIT: In response to the first comment below
That question/answer is not streaming the output, it's returning a byte[] in the ResponseEntity. If that's what you want to do, simply return a message with a byte[] payload (and appropriate content-type header).
If you really want your service to get a reference to the response outputstream, no you can't do that with the standard http gateway. However, you can use a simple servlet/controller and inject a <gateway/> to send a request to SI; the gateway's interface might be
public interface MyInterface {
String foo(@Payload String request, @Header("stream") OutputStream stream);
}
with the service-activator having expression="@fooService.bar(payload, headers['stream'])".
来源:https://stackoverflow.com/questions/17096563/spring-integration-returning-stream