HTTP is most often used as a Web-Service transport protocol. However, it is not able to meet the asynchronous communication requirements.
Web-Service on XMPP transport is capable of asynchronous communication in which a client doesn't need to wait for a response from a service; instead, the service sends the response to the client when the process is completed. Spring-WS 2.0 includes XMPP (Jabber) support in which a Web-Service can communicate over the XMPP protocol. In this recipe, setting up a Spring-WS on XMPP transport is presented. Since there is no external HTTP server, a test class is used to load the application context.
In this recipe, the project's name is LiveRestaurant_R-1.7
, which has the following Maven dependencies:
spring-ws-core-2.0.1.RELEASE.jar
spring-ws-support-2.0.1.RELEASE.jar
spring-test-3.0.5.RELEASE.jar
junit-4.7.jar
xmlunit-1.1.jar
smack-3.1.0.jar
Create an endpoint (
SamplePlayLoadEndPoint
).Configure connection to the XMPP server in the application context (
applicationContext.xml
).Configure the message receiver in the application context.
Run the following command:
mvn clean package
The following is the response received:
<placeOrderRequest xmlns="..."><id>9999</id></placeOrderRequest> ... for request ...<placeOrderRequest xmlns="...."><id>9999</id></placeOrderReque t>...
In the application context, the messageFactory
bean is responsible for creating the incoming and outgoing SOAP messages. The messageReceiver
bean acts as a server, using a connection (to XMPP server:google talk
), and listens to the host on a specific service with a username and password.
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/> <bean id="connection" class="org.springframework.ws.transport.xmpp.support.XmppConnectionFactoryBean"> <property name="host" value="talk.google.com"/> <property name="username" value="[email protected]"/> <property name="password" value="yourPassword"/> <property name="serviceName" value="gmail.com"/> </bean> <bean id="messagingReceiver" class="org.springframework.ws.transport.xmpp.XmppMessageReceiver"> <property name="messageFactory" ref="messageFactory"/> <property name="connection" ref="connection"/> <property name="messageReceiver" ref="messageDispatcher"/> </bean>
Once the message is sent by the client, it will be forwarded to the endpoint (SamplePlayLoadEndPoint
that is configured within messageDispatcher
) by the message dispatcher and the response will be returned to the client:
<bean id="messageDispatcher" class="org.springframework.ws.soap.server.SoapMessageDispatcher"> <property name="endpointMappings"> <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping"> <property name="defaultEndpoint"> <bean class="com.packtpub.liverestaurant.service.endpoint.SamplePlayLoadEndPoint"/> </property> </bean> </property> </bean>
Webservicetemplate
is used here as a client; it will be discussed in the next chapter.
SamplePlayLoadEndPoint
just receives a request and returns a response:
public class SamplePlayLoadEndPoint implements PayloadEndpoint { static Logger logger = Logger.getLogger(SamplePlayLoadEndPoint.class); public Source invoke(Source request) throws Exception { return request; }
A test class is included in the project to load the application context, set up the XMPP Web-Service server, and test the Web-Service.
The Creating Web-Service client on XMPP transport recipe discussed in Chapter 2, Building Clients for SOAP Web-Services.