Messaging Gateways
The primary purpose of a Gateway is to hide the messaging API provided by
Spring Integration. It allows your application's business logic to be completely
unaware of the Spring Integration API and using a generic Gateway, your code
interacts instead with a simple interface, only.
Enter the GatewayProxyFactoryBean
As mentioned above, it would be great to have no dependency on the Spring
Integration API at all - including the gateway class. For that reason, Spring
Integration provides the GatewayProxyFactoryBean that
generates a proxy for any interface and internally invokes the gateway
methods shown below. Using dependency injection you can then expose the interface
to your business methods.
Here is an example of an interface that can be used to interact with Spring
Integration:
Gateway XML Namespace Support
Namespace support is also
provided which allows you to configure such an interface as a service as demonstrated by the following example.
]]>
With this configuration defined, the "cafeService" can now be injected into other beans, and the code that invokes the methods on that
proxied instance of the Cafe interface has no awareness of the Spring Integration API. The general
approach is similar to that of Spring Remoting (RMI, HttpInvoker, etc.). See the "Samples" Appendix for
an example that uses this "gateway" element (in the Cafe demo).
Setting the Default Reply Channel
Typically you don't have to specify the default-reply-channel,
since a Gateway will auto-create a temporary, anonymous reply channel,
where it will listen for the reply. However, there are some cases which
may prompt you to define a default-reply-channel (or reply-channel
with adapter gateways such as HTTP, JMS, etc.).
For some background, we'll quickly discuss some of the inner-workings of the Gateway.
A Gateway will create a temporary point-to-point reply channel which is anonymous and is added
to the Message Headers with the name replyChannel.
When providing an explicit default-reply-channel (reply-channel with remote adapter gateways),
you have the option to point to a publish-subscribe channel, which is so named because you can add more than one subscriber to it.
Internally Spring Integration will create a Bridge between the temporary replyChannel and the explicitly defined
default-reply-channel.
So let's say you want your reply to go not only to the gateway, but also to some other consumer. In this case you
would want two things: a) a named channel you can subscribe to and b) that channel is a publish-subscribe-channel.
The default strategy used by the gateway will not satisfy those needs, because the reply channel added to the header is anonymous and
point-to-point. This means that no other subscriber can get a handle to it and even if it could, the channel
has point-to-point behavior such that only one subscriber would get the Message. So by defining a default-reply-channel
you can point to a channel of your choosing, which in this case would be a publish-subscribe-channel.
The Gateway would create a bridge from it to the temporary, anonymous reply channel that is stored in the header.
Another case where you might want to provide a reply channel explicitly is for monitoring or auditing via an interceptor
(e.g., wiretap). You need a named channel in order to configure a Channel Interceptor.
Gateway Configuration with Annotations and/or XML
The reason that the attributes on the 'gateway' element are named 'default-request-channel' and
'default-reply-channel' is that you may also provide per-method channel references by using the
@Gateway annotation.
You may alternatively provide such content in method sub-elements if you prefer XML configuration (see the next paragraph).
It is also possible to pass values to be interpreted as Message headers on the Message
that is created and sent to the request channel by using the @Header annotation:
If you prefer the XML approach of configuring Gateway methods, you can provide method sub-elements
to the gateway configuration.
]]>
You can also provide individual headers per method invocation via XML.
This could be very useful if the headers you want to set are static in nature and you don't want
to embed them in the gateway's method signature via @Header annotations.
For example, in the Loan Broker example we want to influence how aggregation of the Loan quotes
will be done based on what type of request was initiated (single quote or all quotes). Determining the
type of the request by evaluating what gateway method was invoked, although possible, would
violate the separation of concerns paradigm (the method is a java artifact), but expressing your
intention (meta information) via Message headers is natural in a Messaging architecture.
]]>
In the above case you can clearly see how a different value will be set for the 'RESPONSE_TYPE'
header based on the gateway's method.
Invoking No-Argument Methods
When invoking methods on a Gateway interface that do not have any arguments,
the default behavior is to receive a Message from a
PollableChannel.
At times however, you may want to trigger no-argument methods so that
you can in fact interact with other components downstream that do not require
user-provided parameters, e.g. triggering no-argument SQL calls or Stored
Procedures.
In order to achieve send-and-receive semantics, you must provide a payload.
In order to generate a payload, method parameters on the interface are
not necessary. You can either use the @Payload annotation
or the payload-expression attribute in XML on the method
sub-element. Below please find a few examples of what the payloads could be:
a literal string
#method (for the method name)
new java.util.Date()
@someBean.someMethod()'s return value
Here is an example using the @Payload annotation:
retrieveOpenOrders();
}]]>
If a method has no argument and no return value, but does contain a
payload expression, it will be treated as a send-only
operation.
Error Handling
Of course, the Gateway invocation might result in errors.
By default any error that has occurred downstream will be re-thrown as a
MessagingException (RuntimeException)
upon the Gateway's method invocation. However there are times when you may want
to simply log the error rather than propagating it, or you may want to treat an
Exception as a valid reply, by mapping it to a Message that will conform to some
"error message" contract that the caller understands. To accomplish this, our
Gateway provides support for a Message Channel dedicated to the errors via the
error-channel attribute. In the example below, you can see
that a 'transformer' is used to create a reply Message from the Exception.
]]>
The exceptionTransformer could be a simple POJO that
knows how to create the expected error response objects. That would then be
the payload that is sent back to the caller. Obviously, you could do many
more elaborate things in such an "error flow" if necessary. It might involve
routers (including Spring Integration's ErrorMessageExceptionTypeRouter),
filters, and so on. Most of the time, a simple 'transformer' should be sufficient,
however.
Alternatively, you might want to only log the Exception (or send it somewhere
asynchronously). If you provide a one-way flow, then nothing would be sent
back to the caller. In the case that you want to completely suppress Exceptions,
you can provide a reference to the global "nullChannel" (essentially a /dev/null
approach). Finally, as mentioned above, if no "error-channel" is defined at all,
then the Exceptions will propagate as usual.
Exposing the messaging system via simple POJI Gateways obviously provides benefits, but "hiding" the reality
of the underlying messaging system does come at a price so there are certain things you should consider.
We want our Java method to return as quickly as possible and not hang for an indefinite amount of time while
the caller is waiting on it to return (void, return value, or a thrown Exception). When regular methods are
used as a proxies in front of the Messaging system, we have to take into account the potentially asynchronous
nature of the underlying messaging. This means that there might
be a chance that a Message that was initiated by a Gateway could be dropped by a Filter, thus never reaching a
component that is responsible for producing a reply. Some Service Activator method might result in an Exception,
thus providing no reply (as we don't generate Null messages). So as you can see there are multiple scenarios
where a reply message might not be coming. That is perfectly natural in messaging systems. However think about the
implication on the gateway method. The Gateway's method input arguments were incorporated into a Message and
sent downstream. The reply Message would be converted to a return value of the Gateway's method. So you might want
to ensure that for each Gateway call there will always be a reply Message.
Otherwise, your Gateway method might never return and will hang indefinitely.
One of the ways of handling this situation is via an Asynchronous Gateway (explained later in this section). Another way of handling it is to explicitly set the reply-timeout attribute. That way, the gateway will not hang any longer than the time specified by the reply-timeout and will return 'null' if that timeout does elapse. Finally, you might want to consider setting downstream flags such as 'requires-reply' on
a service-activator or 'throw-exceptions-on-rejection' on a filter. These options will be discussed in more detail in the final section
of this chapter.
Asynchronous Gateway
As a pattern the Messaging Gateway is a very nice way to hide messaging-specific code while still exposing the full capabilities of the
messaging system. As you've seen, the GatewayProxyFactoryBean provides a convenient way to expose a Proxy over a service-interface
thus giving you POJO-based access to a messaging system (based on objects in your own domain, or primitives/Strings, etc). But when a
gateway is exposed via simple POJO methods which return values it does imply that for each Request message (generated when the method is invoked)
there must be a Reply message (generated when the method has returned). Since Messaging systems naturally are asynchronous you may not always be
able to guarantee the contract where "for each request there will always be be a reply".
With Spring Integration 2.0 we are introducing support for an Asynchronous Gateway which is a convenient way to initiate
flows where you may not know if a reply is expected or how long will it take for replies to arrive.
A natural way to handle these types of scenarios in Java would be relying upon java.util.concurrent.Future instances, and
that is exactly what Spring Integration uses to support an Asynchronous Gateway.
From the XML configuration, there is nothing different and you still define Asynchronous Gateway the same way as a regular Gateway.
]]>
However the Gateway Interface (service-interface) is a bit different.
public interface MathServiceGateway {
Future<Integer> multiplyByTwo(int i);
}
As you can see from the example above the return type for the gateway method is a Future. When
GatewayProxyFactoryBean sees that the
return type of the gateway method is a Future, it immediately switches to the async mode by utilizing
an AsyncTaskExecutor. That is all. The call to such a method always returns immediately with a Future instance.
Then, you can interact with the Future at your own pace to get the result, cancel, etc. And, as with
any other use of Future instances, calling get() may reveal a timeout, an execution exception, and so on.
MathServiceGateway mathService = ac.getBean("mathService", MathServiceGateway.class);
Future<Integer> result = mathService.multiplyByTwo(number);
// do something else here since the reply might take a moment
int finalResult = result.get(1000, TimeUnit.SECONDS);
For a more detailed example, please refer to the async-gateway sample distributed within the Spring Integration samples.
Asynchronous Gateway and AsyncTaskExecutor
By default GatewayProxyFactoryBean uses org.springframework.core.task.SimpleAsyncTaskExecutor
when submitting internal AsyncInvocationTask instances for any gateway method whose
return type is Future.class. However the async-executor attribute in the
<gateway/> element's configuration allows you to provide a reference to any implementation of
java.util.concurrent.Executor available within the Spring application context.
Gateway behavior when no response arrives
As it was explained earlier, the Gateway provides a convenient way of interacting with a Messaging system via POJO method
invocations, but realizing that a typical method invocation, which is generally expected to always return (even with an Exception),
might not always map one-to-one to message exchanges (e.g., a reply message might not arrive - which is equivalent to a
method not returning). It is important to go over several scenarios especially in the Sync Gateway case and understand
the default behavior of the Gateway and how to deal with these scenarios to make the Sync Gateway behavior more
predictable regardless of the outcome of the message flow that was initialed from such Gateway.
There are certain attributes that could be configured to make Sync Gateway behavior more predictable,
but some of them might not always work as you might have expected. One of them is reply-timeout.
So, lets look at the reply-timeout attribute and see how it can/can't influence the behavior
of the Sync Gateway in various scenarios. We will look at single-threaded scenario
(all components downstream are connected via Direct Channel) and multi-threaded scenarios
(e.g., somewhere downstream you may have Pollable or Executor Channel which breaks single-thread boundary)
Long running process downstream
Sync Gateway - single-threaded.
If a component downstream is still running (e.g., infinite loop or a very slow service), then setting a reply-timeout
has no effect and the Gateway method call will not return until such downstream service exits (via return or exception).
Sync Gateway - multi-threaded.
If a component downstream is still running (e.g., infinite loop or a very slow service), in a multi-threaded message
flow setting the reply-timeout will have an effect by allowing gateway method invocation to
return once the timeout has been reached, since the GatewayProxyFactoryBean will simply
poll on the reply channel waiting for a message until the timeout expires. However it could result in a 'null' return
from the Gateway method if the timeout has been reached before the actual reply was produced. It is also important to understand that
the reply message (if produced) will be sent to a reply channel after the Gateway method invocation might have returned, so you must be aware of that and design your flow with this in mind.
Downstream component returns 'null'
Sync Gateway - single-threaded.
If a component downstream returns 'null' and no reply-timeout has been configured, the Gateway
method call will hang indefinitely unless: a) a reply-timeout has been configured or b) the
requires-reply attribute has been set on the downstream component (e.g., service-activator)
that might return 'null'. In this case, an Exception would be thrown and propagated to the Gateway.
Sync Gateway - multi-threaded. Behavior is the same as above.
Downstream component return signature is 'void' while Gateway method signature is non-void
Sync Gateway - single-threaded.
If a component downstream returns 'void' and no reply-timeout has been configured,
the Gateway method call will hang indefinitely unless a reply-timeout has been configured
Sync Gateway - multi-threaded Behavior is the same as above.
Downstream component results in Runtime Exception (regardless of the method signature)
Sync Gateway - single-threaded.
If a component downstream throws a Runtime Exception, such exception will be propagated via an Error Message back to
the gateway and re-thrown.
Sync Gateway - multi-threaded Behavior is the same as above.
It is also important to understand that by default reply-timeout is unbounded* which means that
if not explicitly set there are several scenarios (described above) where your Gateway method invocation might
hang indefinitely. So, make sure you analyze your flow and if there is even a remote possibility of one of these
scenarios to occur, set the reply-timeout attribute to a 'safe' value or, even better,
set the requires-reply attribute of the downstream component to 'true' to ensure a timely response
as produced by the throwing of an Exception as soon as that downstream component does return null internally.
But also, realize that there are some scenarios (see the very first one)
where reply-timeout will not help. That means it is also important to analyze your message
flow and decide when to use a Sync Gateway vs an Async Gateway. As you've seen the latter case is simply a matter of
defining Gateway methods that return Future instances. Then, you are guaranteed to receive that return value, and
you will have more granular control over the results of the invocation.
Also, when dealing with a Router you should remember that setting the resolution-required attribute to 'true'
will result in an Exception thrown by the router if it can not resolve a particular channel. Likewise, when dealing with a Filter,
you can set the throw-exception-on-rejection attribute. In both of these cases, the resulting flow will
behave like that containing a service-activator with the 'requires-reply' attribute. In other words, it will help to ensure
a timely response from the Gateway method invocation.
* reply-timeout is unbounded for <gateway/>
elements (created by the GatewayProxyFactoryBean). Inbound gateways for external integration
(ws, http, etc.) share many characteristics and attributes with these gateways. However,
for those inbound gateways, the default reply-timeout is 1000
milliseconds (1 second). If a downstream async handoff is made to another thread, you may need to
increase this attribute to allow enough time for the flow to complete before the
gateway times out.