Exposing services using HTTP invokers:-
Spring HTTP invokers use the standard Java serialization mechanism to
expose services through HTTP. Spring uses either the standard facilities
provided by the JDK or Apache HttpComponents to perform HTTP
calls.
The main classes are :
org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter - This is a servlet API based Http request handler. It is used to export the remote services. It takes in a service property that is the service to be exported and a ServiceInterface that specifies the interface that the service is tied to.
org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean -
This is a proxy factory for creating http invoker proxies. It has a
serviceUrl property that must be an http url exposing an http invoker
service. This class serializes the objects that are sent to remote services and
deserializes the objects back.
Example of Spring Remoting by HTTP Invoker:-
There are some following step to make an Application using HTTP Invoker with Spring as :
The following example show how to write a simple
Web Project Springhttpinvoker
application using HTTP
Invoker. To
start with it, let us have working Eclipse
IDE in
place and follow the following steps to developed a Java
Application:
1 Create a Dynamic
Web Project with
a name Springhttpinvoker and
create a package r4r.in under
the src
folder in the created project.
2 Add Spring and other libraries into the folder lib to make it.
3 Create a interface Square.java and Java class SquareImpl.java, Client.java ,client-beans.xml under the r4r.in package.
4 Create configuration files web.xml, httpinvoker.xml under WebContent/WEB-INF folder .
5 The final step is to create the content of all the source and configuration files and export the application as explained below.
Following are the list of JAR files required for this application:
1 Spring Core jar files
2 Spring Remoting jar files
3 Spring AOP jar files
4 Spring Web jar files
1 Following is the content of Interface Square.java containing one sq(square) metod.
package r4r.in;
public interface Square {
int sq(int number);
}
2 Following is the content of java class SquareIml.java .This class provides the implementation of Square interface.
package r4r.in;
public class SquareImpl implements Square{
@Override
public int sq(int number) {
return number*number;
} }
3 Following is the content of xml file web.xml .In this xml file we define the DispatcherServlet as the front controller. If any request is followed by .http extension, it will be forwarded to DispatcherServlet.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>httpInvoker</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>httpInvoker</servlet-name>
<url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>
4
Following is the content of xml file client-beans.xml.
In this xml file, we define bean for HttpInvokerProxyFactoryBean. now we
need to define two properties of this class.
1 serviceUrl
2 serviceInterface
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="squareBean" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/Springhttpinvoker/Square.http"></property>
<property name="serviceInterface" value="r4r.in.Square"></property>
</bean>
</beans>
5 Following is the content of xml file httpinvoker.xml. It defines bean for SquareImpl and HttpInvokerServiceExporter.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="squareBean" class="r4r.in.SquareImpl"></bean>
<bean name="/Square.http" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="squareBean"></property>
<property name="serviceInterface" value="r4r.in.Square"></property>
</bean>
</beans>
6 Following is the content of java class client.java. It is simply getting the instance of Square and call the method.
package r4r.in;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
Square square = (Square)context.getBean("squareBean");
System.out.println(square.sq(6));
} }
7 Run this Application : client.java