This commit is contained in:
sxyx2008
2014-07-01 14:24:29 +08:00
parent 4e0bfbb643
commit 1fd14f906e
16 changed files with 489 additions and 3 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
/.project
/.settings
/src
/src
+1
View File
@@ -11,5 +11,6 @@
<module>spring3-mvc-simple</module>
<module>spring3-jmx-example</module>
<module>spring3-rmi-example</module>
<module>spring3-rmi-httpinvoker-example</module>
</modules>
</project>
-2
View File
@@ -58,8 +58,6 @@
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,5 @@
/target/
/dependency-reduced-pom.xml
/.settings
/.classpath
/.project
+19
View File
@@ -0,0 +1,19 @@
spring3-rmi-httpinvoker-example
=======================
基于Spring Http invoker的远程方法调用
先使用```mvn jetty:run```运行服务端,再运行```HttpUserServiceClient```client端。注意看控制台日志输出
注意的配置文件
server
* [spring-servlet.xml](src/main/webapp/WEB-INF/spring-servlet.xml)
* [web.xml](src/main/webapp/WEB-INF/web.xml)
client
* [httpClientApplicationContext.xml](src/main/resources/httpClientApplicationContext.xml)
* [HttpUserServiceClient.java](src/main/java/net/aimeizi/rmi/httpinvoke/example/http/client/HttpUserServiceClient.java)
+103
View File
@@ -0,0 +1,103 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.aimeizi</groupId>
<artifactId>maven-framework-project</artifactId>
<version>1.0.0</version>
</parent>
<groupId>net.aimeizi.rmi.httpinvoke.example</groupId>
<artifactId>spring3-rmi-httpinvoker-example</artifactId>
<packaging>war</packaging>
<properties>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-remoting</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Log4j library -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<build>
<finalName>spring3-rmi-httpinvoker-example</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>net.aimeizi.rmi.httpinvoke.example.http.client.HttpUserServiceClient</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,29 @@
package net.aimeizi.rmi.httpinvoke.example.cache.service;
import java.util.concurrent.ConcurrentHashMap;
import net.aimeizi.rmi.httpinvoke.example.model.User;
public class CacheService implements ICacheService {
ConcurrentHashMap<Long, User> userMap;
/**
* Get User Map
*
* @return ConcurrentHashMap User Map
*/
public ConcurrentHashMap<Long, User> getUserMap() {
return userMap;
}
/**
* Set User Map
*
* @param ConcurrentHashMap User Map
*/
public void setUserMap(ConcurrentHashMap<Long, User> userMap) {
this.userMap = userMap;
}
}
@@ -0,0 +1,16 @@
package net.aimeizi.rmi.httpinvoke.example.cache.service;
import java.util.concurrent.ConcurrentHashMap;
import net.aimeizi.rmi.httpinvoke.example.model.User;
public interface ICacheService {
/**
* Get User Map
*
* @return ConcurrentHashMap User Map
*/
public ConcurrentHashMap<Long, User> getUserMap();
}
@@ -0,0 +1,48 @@
package net.aimeizi.rmi.httpinvoke.example.http.client;
import net.aimeizi.rmi.httpinvoke.example.http.server.IHttpUserService;
import net.aimeizi.rmi.httpinvoke.example.model.User;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HttpUserServiceClient {
private static Logger logger = Logger.getLogger(HttpUserServiceClient.class);
/**
* Main method of the Http User Service Client
*
*/
public static void main(String[] args) {
logger.debug("Http User Service Client is starting...");
//Http Client Application Context is started...
ApplicationContext context = new ClassPathXmlApplicationContext("httpClientApplicationContext.xml");
//Remote User Service is called via Http Client Application Context...
IHttpUserService httpClient = (IHttpUserService) context.getBean("HttpUserService");
//New User is created...
User user = new User();
user.setId(1);
user.setName("Bruce");
user.setSurname("Willis");
//The user is added to the remote cache...
httpClient.addUser(user);
//The users are gotten via remote cache...
httpClient.getUserList();
//The user is deleted from remote cache...
httpClient.deleteUser(user);
//The users are gotten via remote cache...
httpClient.getUserList();
logger.debug("Http User Service Client is stopped...");
}
}
@@ -0,0 +1,71 @@
package net.aimeizi.rmi.httpinvoke.example.http.server;
import java.util.ArrayList;
import java.util.List;
import net.aimeizi.rmi.httpinvoke.example.cache.service.ICacheService;
import net.aimeizi.rmi.httpinvoke.example.model.User;
import org.apache.log4j.Logger;
public class HttpUserService implements IHttpUserService {
private static Logger logger = Logger.getLogger(HttpUserService.class);
ICacheService cacheService;
/**
* Add User
*
* @param User user
* @return boolean response of the method
*/
public boolean addUser(User user) {
getCacheService().getUserMap().put(user.getId(), user);
logger.debug("User has been added to cache. User : "+getCacheService().getUserMap().get(user.getId()));
return true;
}
/**
* Delete User
*
* @param User user
* @return boolean response of the method
*/
public boolean deleteUser(User user) {
getCacheService().getUserMap().remove(user.getId());
logger.debug("User has been deleted from cache. User : "+user);
return true;
}
/**
* Get User List
*
* @return List user list
*/
public List<User> getUserList() {
List<User> list = new ArrayList<User>();
list.addAll(getCacheService().getUserMap().values());
logger.debug("User List : "+list);
return list;
}
/**
* Get Remote Cache Service
*
* @return ICacheService Remote Cache Service
*/
public ICacheService getCacheService() {
return cacheService;
}
/**
* Set Remote Cache Service
*
* @param ICacheService Remote Cache Service
*/
public void setCacheService(ICacheService cacheService) {
this.cacheService = cacheService;
}
}
@@ -0,0 +1,32 @@
package net.aimeizi.rmi.httpinvoke.example.http.server;
import java.util.List;
import net.aimeizi.rmi.httpinvoke.example.model.User;
public interface IHttpUserService {
/**
* Add User
*
* @param User user
* @return boolean response of the method
*/
public boolean addUser(User user);
/**
* Delete User
*
* @param User user
* @return boolean response of the method
*/
public boolean deleteUser(User user);
/**
* Get User List
*
* @return List user list
*/
public List<User> getUserList();
}
@@ -0,0 +1,74 @@
package net.aimeizi.rmi.httpinvoke.example.model;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable {
private long id;
private String name;
private String surname;
/**
* Get User Id
*
* @return long id
*/
public long getId() {
return id;
}
/**
* Set User Id
*
* @param long id
*/
public void setId(long id) {
this.id = id;
}
/**
* Get User Name
*
* @return long id
*/
public String getName() {
return name;
}
/**
* Set User Name
*
* @param String name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get User Surname
*
* @return long id
*/
public String getSurname() {
return surname;
}
/**
* Set User Surname
*
* @param String surname
*/
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("Id : ").append(getId());
strBuilder.append(", Name : ").append(getName());
strBuilder.append(", Surname : ").append(getSurname());
return strBuilder.toString();
}
}
@@ -0,0 +1,14 @@
<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-3.0.xsd">
<!-- Http Invoker Client Declaration -->
<bean id="HttpUserService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- serviceUrl represents Http Service Url called-->
<property name="serviceUrl" value="http://127.0.0.1:8080/HttpUserService"/>
<!-- serviceInterface represents Http Service Interface called -->
<property name="serviceInterface" value="net.aimeizi.rmi.httpinvoke.example.http.server.IHttpUserService"/>
</bean>
</beans>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} %5p (%F:%L) - %m%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="CA" />
</root>
</log4j:configuration>
@@ -0,0 +1,39 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="UserMap" class="java.util.concurrent.ConcurrentHashMap" />
<bean id="CacheService" class="net.aimeizi.rmi.httpinvoke.example.cache.service.CacheService">
<property name="userMap" ref="UserMap"/>
</bean>
<bean id="HttpUserService" class="net.aimeizi.rmi.httpinvoke.example.http.server.HttpUserService" >
<property name="cacheService" ref="CacheService"/>
</bean>
<!-- Http Invoker Service Declaration -->
<bean id="HttpUserServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<!-- service represents Service Impl -->
<property name="service" ref="HttpUserService"/>
<!-- serviceInterface represents Http Service Interface exposed -->
<property name="serviceInterface" value="net.aimeizi.rmi.httpinvoke.example.http.server.IHttpUserService"/>
</bean>
<!-- Mapping configurations from URLs to request handler beans -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/HttpUserService">HttpUserServiceExporter</prop>
</props>
</property>
</bean>
</beans>
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring3-rmi-httpinvoker-example</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/HttpUserService</url-pattern>
</servlet-mapping>
</web-app>