Coucho can provide both Hessian and Burlap. Burlap is an xml-based Hessian substitute. We may use the BurlapServiceExporter and BurlapProxyFactoryBean classes to implement burlap’s remoting service.
Implementation: You need to create the following files for creating a simple burlap application:
- Calculation.java
- CalculationImpl.java
- web.xml
- burlap-servlet.xml
- client-beans.xml
- Client.java
The project structure will look as follows:
A. File: Calculation.java
// Java Program to illustrate Simple Interface // Containing a Method package com.geeksforgeeks;
// Interface public interface Calculation {
int cube( int number);
} |
B. File: CalculationImpl.java
// Java Program to Implement Calculation Interface package com.geeksforgeeks;
// Class // Implementing interface public class CalculationImpl implements Calculation {
// Method
public int cube( int number)
{
// Returning cube of a number
return number * number * number;
}
} |
C. File: web.xml
The front controller is defined in this XML file called DispatcherServlet. Any request that ends in.http will be routed to DispatcherServlet.
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app >
< servlet >
< servlet-name >burlap</ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
< load-on-startup >1</ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >burlap</ servlet-name >
< url-pattern >*.http</ url-pattern >
</ servlet-mapping >
</ web-app >
|
D. File: burlap-servlet.xml
It has to be placed in the WEB-INF folder. It must be called servletname-servlet.xml. It specifies CalculationImpl and BurlapServiceExporter as beans.
<? xml version = "1.0" encoding = "UTF-8" ?>
xsi:schemaLocation="http://www.springframework.org/schema/beans
< bean id = "calculationBean"
class = "com.geeksforgeeks.CalculationImpl" >
</ bean >
< bean name = "/Calculation.http" class = "org.springframework.remoting.caucho.BurlapServiceExporter" >
< property name = "service" ref = "calculationBean" ></ property >
< property name = "serviceInterface" value = "com.geeksforgeeks.Calculation" ></ property >
</ bean >
</ beans >
|
E. File: client-beans.xml
The bean for BurlapProxyFactoryBean is defined in this XML file. This class requires two attributes to be defined.
- serviceUrl
- serviceInterface
<? xml version = "1.0" encoding = "UTF-8" ?>
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = "calculationBean" class = "org.springframework.remoting.caucho.BurlapProxyFactoryBean" >
< property name = "serviceInterface" value = "com.geeksforgeeks.Calculation" ></ property >
</ bean >
</ beans >
|
F. File: Client.java
This class gets the instance of Calculation and calls the cube method.
// Java Program to Illustrate Client Class package com.geeksforgeeks;
// Importing required classes import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Class public class Client {
// Main driver method
public static void main(String[] args)
{
// Creating an object of ApplicationContext class
ApplicationContext context
= new ClassPathXmlApplicationContext(
"client-beans.xml" );
Calculation calculation
= (Calculation)context.getBean(
"calculationBean" );
// Printing cube of a random number
System.out.println(calculation.cube( 3 ));
}
} |
Output:
27
Note: In order to run the above code fragment to get the output:
- Start and deploy the project, assuming that the server is listening on port 8888. Change the serviceURL in client-beans.xml if the port number is changed.
- The Client.java file should then be compiled and run.