HttpInvoker is a Spring-specific remoting option that essentially enables Remote Procedure Calls (RPC) over HTTP.
In order to accomplish this, an outbound representation of a method invocation is serialized using standard Java
serialization and then passed within an HTTP POST request. After being invoked on the target system, the method's
return value is then serialized and written to the HTTP response. There are two main requirements. First, you
must be using Spring on both sides since the marshalling to and from HTTP requests and responses is handled by
the client-side invoker and server-side exporter. Second, the Objects that you are passing must implement
Serializable
and be available on both the client and server.
While traditional RPC provides physical decoupling, it does not offer nearly the same degree of logical decoupling as a messaging-based system. In other words, both participants in an RPC-based invocation must be aware of a specific interface and specific argument types. Interestingly, in Spring Integration, the "parameter" being sent is a Spring Integration Message, and the interface is an internal detail of Spring Integration's implementation. Therefore, the RPC mechanism is being used as a transport so that from the end user's perspective, it is not necessary to consider the interface and argument types. It's just another adapter to enable messaging between two systems.
To receive messages over http you can use an HttpInvokerInboundGateway
. Here is an
example bean definition:
<bean id="inboundGateway" class="org.springframework.integration.httpinvoker.HttpInvokerInboundGateway"> <property name="requestChannel" ref="requestChannel"/> <property name="replyChannel" ref="replyChannel"/> <property name="requestTimeout" value="30000"/> <property name="replyTimeout" value="10000"/> </bean>
Because the inbound gateway must be able to receive HTTP requests, it must be configured within a Servlet container. The easiest way to do this is to provide a servlet definition in web.xml:
<servlet> <servlet-name>inboundGateway</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet>
Notice that the servlet name matches the bean name.
Note | |
---|---|
If you are running within a Spring MVC application and using the BeanNameHandlerMapping, then the servlet definition is not necessary. In that case, the bean name for your gateway can be matched against the URL path just like a Spring MVC Controller bean. |
To configure the HttpInvokerOutboundGateway
write a bean definition like this:
<bean id="outboundGateway" class="org.springframework.integration.httpinvoker.HttpInvokerOutboundGateway"> <property name="replyChannel" ref="replyChannel"/> </bean>
The outbound gateway is a MessageHandler
and can therefore be registered with
either a PollingConsumer
or EventDrivenConsumer
.
The URL must match that defined by an inbound HttpInvoker Gateway as described in the previous section.
Spring Integration provides an "httpinvoker" namespace and schema definition. To include it in your configuration, simply provide the following URI within a namespace declaration: 'http://www.springframework.org/schema/integration/httpinvoker'. The schema location should then map to 'http://www.springframework.org/schema/integration/httpinvoker/spring-integration-httpinvoker-2.0.xsd'.
To configure the inbound gateway you can choose to use the namespace support for it. The following code snippet shows the different configuration options that are supported.
<httpinvoker:inbound-gateway id="inboundGateway" request-channel="requestChannel" request-timeout="10000" expect-reply="false" reply-timeout="30000"/>
Note | |
---|---|
A 'reply-channel' may also be provided, but it is recommended to rely on the temporary anonymous channel that will be created automatically for handling replies. |
To configure the outbound gateway you can use the namespace support as well. The following code snippet shows the different configuration for an outbound HttpInvoker gateway. Only the 'url' and 'request-channel' are required.
<httpinvoker:outbound-gateway id="outboundGateway" url="http://localhost:8080/example" request-channel="requestChannel" request-timeout="5000" reply-channel="replyChannel" reply-timeout="10000"/>