Remoting in Spring Framework by R4R Team

Remoting in Spring Framework:-
Spring framework makes the developement of remote-enabled services easy. It saves a lot of code by providing its own API. Spring provides various ways to develop remote services. Remote services are services hosted on remote servers and accessed by clients over the network.

For example, lets say you are developing a desktop application that needs to connect to a central server. The desktop application can be on various machines. you can use spring remoting to connect the clients on the desktop to the server. Web services developed using JAX-WS can also be developed and integrated using Spring.

Advantage of Spring Remoting:-
The programmer needs to concentrate on business logic only not plumbing activities such as starting and stopping the server.
Spring framework supports following remoting technologies:

1 Remote Method Invocation (RMI). Through the use of the RmiProxyFactoryBean and the RmiServiceExporter Spring supports both traditional RMI (with java.rmi.Remote interfaces and java.rmi.RemoteException) and transparent remoting via RMI invokers (with any Java interface).

2 Spring’s HTTP invoker. Spring provides a special remoting strategy which allows for Java serialization via HTTP, supporting any Java interface (just like the RMI invoker). The corresponding support classes are HttpInvokerProxyFactoryBean and HttpInvokerServiceExporter.

3 Hessian. By using Spring’s HessianProxyFactoryBean and the HessianServiceExporter you can transparently expose your services using the lightweight binary HTTP-based protocol provided by Caucho.

4 Burlap. Burlap is Caucho’s XML-based alternative to Hessian. Spring provides support classes such as BurlapProxyFactoryBean and BurlapServiceExporter.

5 JAX-WS. Spring provides remoting support for web services via JAX-WS (the successor of JAX-RPC, as introduced in Java EE 5 and Java 6).

6 JMS. Remoting using JMS as the underlying protocol is supported via the JmsInvokerServiceExporter and JmsInvokerProxyFactoryBean classes.

7 AMQP. Remoting using AMQP as the underlying protocol is supported by the Spring AMQP project.

Spring Remoting by HTTP Invoker Example:-
Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for http request than RMI and works well across the firewall.
1 Test.java
It is the simple interface containing one method square.

package com.r4r;

public interface Test {

int square(int number);

}


2 Demo.java
This class provides the implementation of Test interface.

package com.r4r;

public class Demo implements Test{

public int square(int number) {

return number*number;

} }

3 Web.xml
In this xml file, we are defining 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 httpInvoker-servlet.xml
It must be created inside the WEB-INF folder. Its name must be servletname-servlet.xml. It defines bean for Demo 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="TestBean" class="com.r4r.Demo"></bean>

<bean name="/Demo.http"

class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">

<property name="service" ref="TestBean"></property>

<property name="serviceInterface" value="com.r4r.Test"></property>

</bean>

</beans>

5 client-beans.xml
In this xml file, we are defining bean for HttpInvokerProxyFactoryBean. You 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="TestBean"

class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

<property name="serviceUrl"

value="http://localhost:8080/httpinvoker/Test.http"></property>

<property name="serviceInterface" value="com.r4r.Test"></property>

</bean>

</beans>

6 Client.java
This class gets the instance of Test and calls the method.

package com.r4r;

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");

Test t = (Test)context.getBean("TestBean");

System.out.println(t.square(6));

} }

Output: 36 

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!