原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp
In the earlier tutorials we saw an introduction to Spring remoting and its support for RMI and Hessian. In this tutorial we look at another remoting protocol supported by Spring - Burlap. Burlap is an XML based protocol for web services. It has been developed by Caucho. It is similar to Hessian, the only difference being Hessian is binary and Burlap is XML. Like Hessian, Burlap needs to be hosted over HTTP. Similar to Hessian, it has a BurlapServiceExporter and a BurlapProxyFactoryBean class. Note that since Burlap is not being actively developed, its support has been deprecated since Spring 4.0.
The sample program below is a Greeting Service and demonstrates Spring support for Burlap.
Create the GreetingService interface as shown below.
Create a method named getGreeting() that takes a name as a parameter and returns the greeting message (see line 5 below).
1
2
3
4
5
6
|
package
com.studytrails.tutorials.springremotingburlapserver;
public
interface
GreetingService {
String getGreeting(String name);
}
|
Create a class GreetingServiceImpl as shown below.
It implements the GreetingService interface (described earlier)
Implement the getGreeting() method by sending a greeting message (see lines 6-8 below).
1
2
3
4
5
6
7
8
9
10
|
package
com.studytrails.tutorials.springremotingburlapserver;
public
class
GreetingServiceImpl
implements
GreetingService{
@Override
public
String getGreeting(String name) {
return
"Hello "
+ name +
"!"
;
}
}
|
Create the burlap-servlet.xml file (see below).
Declare the 'greetingService' (see lines 14-15 below).
Export the 'greetingService' using Spring's BurlapServiceExporter class (see lines 17-21 below).
Note the following properties of BurlapServiceExporter class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<
bean
id
=
"greetingService"
class
=
"com.studytrails.tutorials.springremotingburlapserver.GreetingServiceImpl"
/>
<
bean
name
=
"/greetingService.http"
class
=
"org.springframework.remoting.caucho.BurlapServiceExporter"
>
<
property
name
=
"service"
ref
=
"greetingService"
/>
<
property
name
=
"serviceInterface"
value
=
"com.studytrails.tutorials.springremotingburlapserver.GreetingService"
/>
</
bean
>
</
beans
>
|
Create the web.xml file (see below).
Create the servlet mapping for the url pattern '*.http' (see line 16 below) for Spring's DispatcherServlet (see line 10 below)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<
web-app
>
<
display-name
>Spring Remoting: Burlap Server</
display-name
>
<
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
>
|
Create the GreetingService interface as shown below.
Copy the GreetingService interface created for Burlap Server (described above) and paste it in Burlap Client source code while retaining the java package structure.
Note: For reference, the source code is shown below.
1
2
3
4
5
6
|
package
com.studytrails.tutorials.springremotingburlapserver;
public
interface
GreetingService {
String getGreeting(String name);
}
|
Create a class TestSpringRemotingBurlap shown below to test Spring Burlap Remoting.
Load spring configuration file (see line 11 below)
Get a reference to GreetingService using the bean name 'greetingService' (see line 12 below)
Call the GreetingService.getGreting() method by passing the name 'Alpha' (see line 13 below)
Print the greeting message (see line 14 below).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.studytrails.tutorials.springremotingburlapclient;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
com.studytrails.tutorials.springremotingburlapserver.GreetingService;
public
class
TestSpringRemotingBurlap {
public
static
void
main(String[] args) {
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"spring-config-client.xml"
);
GreetingService greetingService = (GreetingService)context.getBean(
"greetingService"
);
String greetingMessage = greetingService.getGreeting(
"Alpha"
);
System.out.println(
"The greeting message is : "
+ greetingMessage);
}
}
|
Create the spring-config-client.xml file (see below).
Declare the 'greetingService' using Spring's BurlapProxyFactoryBean class (see lines 13-16 below).
Note the following properties of BurlapProxyFactoryBean class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<
bean
id
=
"greetingService"
class
=
"org.springframework.remoting.caucho.BurlapProxyFactoryBean"
>
<
property
name
=
"serviceUrl"
value
=
"http://localhost:8080/springremotingburlapserver/greetingService.http"
/>
<
property
name
=
"serviceInterface"
value
=
"com.studytrails.tutorials.springremotingburlapserver.GreetingService"
/>
</
bean
>
</
beans
>
|
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.
(Alternatively you can go the folder containing the springremotingburlapserver-installer.jar and execute the jar using java -jar springremotingburlapserver-installer.jar command)
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.
(Alternatively you can go the folder containing the springremotingburlapclient-installer.jar and execute the jar using java -jar springremotingburlapclient-installer.jar command)
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotingburlapserver . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.
The WAR file for this example is available as springremotingburlapserver.war in the download folder specified by you earlier (e.g. C:\Temp). The path for the WAR file is <DOWNLOAD_FOLDER_PATH>/springremotingburlapserver/dist/springremotingburlapserver.war.
This WAR file can be deployed in any webserver of your choice and example can be executed.
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotingburlapclient . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.