怎么制作手机网站,遂宁建设局网站首页,广东网站建站公司,wordpress容易优化吗spring rmiSpring远程支持简化了启用远程服务的开发。 当前#xff0c;Spring支持以下远程技术#xff1a;远程方法调用#xff08;RMI#xff09;#xff0c;HTTP调用程序#xff0c;Hessian#xff0c;Burlap#xff0c;JAX-RPC#xff0c;JAX-WS和JMS。 远程方法调… spring rmi Spring远程支持简化了启用远程服务的开发。 当前Spring支持以下远程技术远程方法调用RMIHTTP调用程序HessianBurlapJAX-RPCJAX-WS和JMS。 远程方法调用RMI Spring通过RmiProxyFactoryBean和RmiServiceExporter支持RMI。 RmiServiceExporter将任何Spring管理的bean导出为RMI服务并进行注册。 RmiProxyFactoryBean是一种工厂bean可为RMI服务创建代理。 该代理对象代表客户端与远程RMI服务进行通信。 Spring的HTTP调用程序 Spring HTTP调用程序使用标准的Java序列化机制通过HTTP公开服务。 Spring通过HttpInvokerProxyFactoryBean和HttpInvokerServiceExporter支持HTTP调用程序基础结构。 HttpInvokerServiceExporter它将指定的服务bean导出为HTTP调用程序服务终结点可通过HTTP调用程序代理访问。 HttpInvokerProxyFactoryBean是用于HTTP调用程序代理的工厂bean。 Hessian Hessian提供了一个基于HTTP的二进制远程协议。 Spring通过HessianProxyFactoryBean和HessianServiceExporter支持Hessian。 粗麻布粗麻布是Caucho的基于XML的粗麻布替代品。 Spring提供了支持类例如BurlapProxyFactoryBean和BurlapServiceExporter。 JAX-RPC Spring通过JAX-RPCJ2EE 1.4的Web服务API为Web服务提供远程支持。 JAX-WS Spring通过JAX-WSJava EE 5和Java 6中引入的JAX-RPC的继承者为Web服务提供远程支持。 JMS JmsInvokerServiceExporter和JmsInvokerProxyFactoryBean类提供了Spring中的JMS远程支持。 让我们看一下Spring RMI支持以开发Spring RMI ServiceClient。 二手技术 JDK 1.6.0_31 春天3.1.1 Maven的3.0.2 步骤1建立已完成的专案 创建一个Maven项目如下所示。 可以使用Maven或IDE插件创建。 步骤2图书馆 Spring依赖项已添加到Maven的pom.xml中。 !-- Spring 3.1.x dependencies --
propertiesspring.version3.1.1.RELEASE/spring.version
/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependency
dependencies 步骤3建立使用者类别 创建一个新的用户类。 package com.otv.user;import java.io.Serializable;/*** User Bean** author onlinetechvision.com* since 24 Feb 2012* version 1.0.0**/
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;}Overridepublic String toString() {StringBuilder strBuilder new StringBuilder();strBuilder.append(Id : ).append(getId());strBuilder.append(, Name : ).append(getName());strBuilder.append(, Surname : ).append(getSurname());return strBuilder.toString();}
} 步骤4建立ICacheService介面 创建了代表远程缓存服务接口的ICacheService接口。 package com.otv.cache.service;import java.util.concurrent.ConcurrentHashMap;import com.otv.user.User;/*** Cache Service Interface** author onlinetechvision.com* since 27 Feb 2012* version 1.0.0**/
public interface ICacheService {/*** Get User Map** return ConcurrentHashMap User Map*/public ConcurrentHashMaplong, user getUserMap();} 步骤5创建CacheService类 CacheService类是通过实现ISchedulerService接口创建的。 它提供对远程缓存的访问… package com.otv.cache.service;import java.util.concurrent.ConcurrentHashMap;import com.otv.user.User;/*** Cache Service Implementation** author onlinetechvision.com* since 6:04:49 PM* version 1.0.0**/
public class CacheService implements ICacheService {//User Map is injected...ConcurrentHashMaplong, user userMap;/*** Get User Map** return ConcurrentHashMap User Map*/public ConcurrentHashMaplong, user getUserMap() {return userMap;}/*** Set User Map** param ConcurrentHashMap User Map*/public void setUserMap(ConcurrentHashMaplong, user userMap) {this.userMap userMap;}} 步骤6建立IRMIUserService接口 创建了代表RMI服务接口的IRMIUserService接口。 此外它为RMI客户端提供了远程方法。 package com.otv.rmi.server;import java.util.List;import com.otv.user.User;/*** RMI User Service Interface** author onlinetechvision.com* since 24 Feb 2012* version 1.0.0**/
public interface IRMIUserService {/*** 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 ListUser getUserList();} 步骤7创建RMIUserService类 RMIUserService类又名RMI对象是通过实现IRMIUserService接口创建的。 package com.otv.rmi.server;import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;import com.otv.cache.service.ICacheService;
import com.otv.user.User;/*** RMI User Service Implementation** author onlinetechvision.com* since 24 Feb 2012* version 1.0.0**/
public class RMIUserService implements IRMIUserService {private static Logger logger Logger.getLogger(RMIUserService.class);//Remote Cache Service is injected...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().put(user.getId(), user);logger.debug(User has been deleted from cache. User : user);return true;}/*** Get User List** return List user list*/public ListUser getUserList() {ListUser list new ArrayListUser();list.addAll(getCacheService().getUserMap().values());logger.debug(User List : list);return list;}/*** Get RMI User Service** return IRMIUserService RMI User Service*/public ICacheService getCacheService() {return cacheService;}/*** Set RMI User Service** param IRMIUserService RMI User Service*/public void setCacheService(ICacheService cacheService) {this.cacheService cacheService;}
} 步骤8创建RMIServerStarter类别 RMI服务器启动程序类已创建。 它启动RMI服务器。 package com.otv.rmi.server.starter;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** RMI Server Starter** author onlinetechvision.com* since 27 Feb 2012* version 1.0.0**/
public class RMIServerStarter {public static void main(String[] args) {//RMI Server Application Context is started...new ClassPathXmlApplicationContext(rmiServerAppContext.xml);}
} 步骤9创建rmiServerAppContext.xml RMI服务器应用程序上下文的内容如下所示。 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd!-- Beans Declaration --bean idUserMap classjava.util.concurrent.ConcurrentHashMap /bean idCacheService classcom.otv.cache.service.CacheServiceproperty nameuserMap refUserMap//bean bean idRMIUserService classcom.otv.rmi.server.RMIUserService property namecacheService refCacheService//bean!-- RMI Server Declaration --bean classorg.springframework.remoting.rmi.RmiServiceExporter!-- serviceName represents RMI Service Name --property nameserviceName valueRMIUserService/!-- service represents RMI Object(RMI Service Impl) --property nameservice refRMIUserService/!-- serviceInterface represents RMI Service Interface exposed --property nameserviceInterface valuecom.otv.rmi.server.IRMIUserService/!-- defaults to 1099 --property nameregistryPort value1099//bean/beans 步骤10创建RMIServiceClient类 RMIServiceClient类已创建。 它调用RMI用户服务并执行用户操作。 package com.otv.rmi.client;import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.otv.rmi.server.IRMIUserService;
import com.otv.rmi.server.RMIUserService;
import com.otv.user.User;/*** RMI Service Client** author onlinetechvision.com* since 24 Feb 2012* version 1.0.0**/
public class RMIServiceClient {private static Logger logger Logger.getLogger(RMIUserService.class);/*** Main method of the RMI Service Client**/public static void main(String[] args) {logger.debug(RMI Service Client is starting...);//RMI Client Application Context is started...ApplicationContext context new ClassPathXmlApplicationContext(rmiClientAppContext.xml);//Remote User Service is called via RMI Client Application Context...IRMIUserService rmiClient (IRMIUserService) context.getBean(RMIUserService);//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...rmiClient.addUser(user);//The users are gotten via remote cache...rmiClient.getUserList();//The user is deleted from remote cache...rmiClient.deleteUser(user);logger.debug(RMI Service Client is stopped...);}
} 步骤11创建rmiClientAppContext.xml RMI客户端应用程序上下文的内容如下所示。 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd!-- RMI Client Declaration --bean idRMIUserService classorg.springframework.remoting.rmi.RmiProxyFactoryBean!-- serviceUrl represents RMI Service Url called--property nameserviceUrl valuermi://x.x.x.x:1099/RMIUserService/!-- serviceInterface represents RMI Service Interface called --property nameserviceInterface valuecom.otv.rmi.server.IRMIUserService/!-- refreshStubOnConnectFailure enforces automatic re-lookup of the stub if acall fails with a connect exception --property namerefreshStubOnConnectFailure valuetrue//bean/beans 步骤12运行项目 如果运行RMI Server时启动了RMI Service Client则将在RMI Server输出日志下方显示。 同样可以通过IDE打开两个单独的控制台来运行RMI Server和Client。 ....
04.03.2012 14:23:15 DEBUG (RmiBasedExporter.java:59) - RMI service [com.otv.rmi.server.RMIUserService16dadf9] is an RMI invoker
04.03.2012 14:23:15 DEBUG (JdkDynamicAopProxy.java:113) - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.otv.rmi.server.RMIUserService16dadf9]
04.03.2012 14:23:15 INFO (RmiServiceExporter.java:276) - Binding service RMIUserService to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.1.7:1099](local),objID:[0:0:0, 0]]]]
04.03.2012 14:23:15 DEBUG (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean org.springframework.remoting.rmi.RmiServiceExporter#0
04.03.2012 14:23:15 DEBUG (AbstractApplicationContext.java:845) - Unable to locate LifecycleProcessor with name lifecycleProcessor: using default [org.springframework.context.support.DefaultLifecycleProcessor3c0007]
04.03.2012 14:23:15 DEBUG (AbstractBeanFactory.java:245) - Returning cached instance of singleton bean lifecycleProcessor04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.addUser
04.03.2012 14:25:43 DEBUG (RMIUserService.java:33) - User has been added to cache. User : Id : 1, Name : Bruce, Surname : Willis
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.addUser04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList
04.03.2012 14:25:43 DEBUG (RMIUserService.java:57) - User List : [Id : 1, Name : Bruce, Surname : Willis]
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.deleteUser
04.03.2012 14:25:43 DEBUG (RMIUserService.java:45) - User has been deleted from cache. User : Id : 1, Name : Bruce, Surname : Willis
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.deleteUser04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList
04.03.2012 14:25:43 DEBUG (RMIUserService.java:57) - User List : []
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList 步骤13下载 OTV_SpringRMI 参考 Online Technology Vision博客中的JCG合作伙伴 Eren Avsarogullari的Spring Remoting支持和RMI服务开发 。 翻译自: https://www.javacodegeeks.com/2012/04/spring-remoting-support-and-developing.htmlspring rmi