标签:time tom ogg 端口 term 测试 remoting 远程调用 ext
package com.rmi;/*** 接口* @author edgewalk* @date 2017年6月11日*/public interface RmiServer {public boolean test();}
package com.rmi.impl;import com.rmi.RmiServer;/*** 实现类* @author edgewalk* @date 2017年6月11日*/public class RmiServerImpl implements RmiServer {@Overridepublic boolean test() {System.out.println("服务端test方法执行了.....");return true;}}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd"><beans><!-- 定义接口实现类--><bean id="rmiService" class="com.rmi.impl.RmiServerImpl"/><bean id="remoteRmiService" class="org.springframework.remoting.rmi.RmiServiceExporter"><!-- service接口 --><property name="serviceInterface" value="com.rmi.RmiServer"/><!-- 调用Service --><property name="service" ref="rmiService" /><!-- value值是提供给客户端调用 --><property name="serviceName" value="remoteService"/><!-- 注册端口 --><property name="registryPort" value="9400"/><!-- 服务端口 --><property name="servicePort" value="9401"/></bean></beans>
package com.rmi;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 服务端启动类* @author edgewalk* @date 2017年6月11日*/public class MainServer {public static void main(String[] args) {System.out.println("rmi服务端启动...");ApplicationContext ac = new ClassPathXmlApplicationContext("rmi-server.xml");System.out.println("rmi服务端启动完成...");}}
package com.rmi;/*** 在客户端使用服务端的接口文件* @author edgewalk* @date 2017年6月11日*/public interface RmiServer {public boolean test();}
package com.rmi;import java.io.IOException;import java.net.Socket;import java.rmi.server.RMIClientSocketFactory;/*** 自定义的socket连接工厂** @author edgewalk* @date 2017年6月11日*/public class RMICustomClientSocketFactory implements RMIClientSocketFactory {private int timeout = 1000; // 读超时时间public void setTimeout(int timeout) {this.timeout = timeout;}public Socket createSocket(String host, int port) throws IOException {Socket socket = new Socket(host, port);/*** 调用setSoTimeout(int* timeout)可以设置超时时间,如果到了超时时间仍没有数据,read会抛出一个SocketTimeoutException,* 程序需要捕获这个异常,但是当前的socket连接仍然是有效的。*/socket.setSoTimeout(timeout);return socket;}}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd"><beans><!-- 自定一个SCOKECT连接,可配置读超时时间 --><bean id="rmiClientSocketFactory" class="com.rmi.RMICustomClientSocketFactory"><property name="timeout" value="1000"></property></bean><!-- rmi远程调用 --><bean id="clientRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"><!-- rmiServer是调用服务端serviceName的value,rmiIp是服务端ip,rmiPort是服务端注册的端口 --><property name="serviceUrl" value="rmi://127.0.0.1:9400/remoteService" /><!-- service接口 --><property name="serviceInterface" value="com.rmi.RmiServer" /><!-- 客户端自动重连 --><!-- lookupStubOnStartup : false表示,不在容器启动的时候创建与Server端的连接; --><property name="lookupStubOnStartup" value="true" /><!-- refreshStubOnConnectFailure : 这个属性是表示是否连接出错时自动重连; --><property name="refreshStubOnConnectFailure" value="true" /><!-- registryClientSocketFactory : 这个是客户端与服务端创建SOCKECT的一个工厂。 --><property name="registryClientSocketFactory" ref="rmiClientSocketFactory" /></bean></beans>
package com.rmi.client;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.rmi.RmiServer;/*** 客户端测试调用服务端程序* @author edgewalk* @date 2017年6月11日*/public class TestRmi {public static void main(String[] arg) {System.out.println("rmi客户端开始调用...");ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi-client.xml");RmiServer rmi=(RmiServer)ctx.getBean("clientRmiService");//rmi.test();System.out.println("rmi客户端调用完成...");}}
rmi服务端启动...log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).log4j:WARN Please initialize the log4j system properly.rmi服务端启动完成...服务端test方法执行了.....
rmi客户端开始调用...log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).log4j:WARN Please initialize the log4j system properly.rmi客户端调用完成...
标签:time tom ogg 端口 term 测试 remoting 远程调用 ext
原文地址:http://www.cnblogs.com/edgedance/p/6984135.html