`

RMI 超时设置适用于spring

阅读更多

连接超时和响应超时:

 

因为spring没有直接设置超时的参数,只能设置rmi底层参数。

 

在jvm启动参数中增加:

 

 -Dsun.rmi.transport.proxy.connectTimeout=3000 -Dsun.rmi.transport.tcp.responseTimeout=3000

 

但在spring 的 RMI中却不能使用,经过查看源代码发现在调用Socket时,没有设置相关超时时间。

 

修改

 RMIDirectSocketFactory.java文件
/*
 * @(#)RMIDirectSocketFactory.java	1.10 03/12/19
 *
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package sun.rmi.transport.proxy;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.rmi.server.LogStream;
import java.rmi.server.RMISocketFactory;

import sun.rmi.runtime.Log;
import sun.security.action.GetLongAction;

/**
 * RMIDirectSocketFactory creates a direct socket connection to the specified
 * port on the specified host.
 */
public class RMIDirectSocketFactory extends RMISocketFactory {
	static final Log proxyLog = Log.getLog("sun.rmi.transport.tcp.proxy",
			"transport", RMIDirectSocketFactory.logLevel);

	static int logLevel = LogStream.parseLevel(getLogLevel());

	private static String getLogLevel() {
		return (String) java.security.AccessController
				.doPrivileged(new sun.security.action.GetPropertyAction(
						"sun.rmi.transport.proxy.logLevel"));
	}

	private static int connectTimeout = getTimeout(
			"sun.rmi.transport.proxy.connectTimeout", 15000);

	private static int responseTimeout = getTimeout(
			"sun.rmi.transport.tcp.responseTimeout", 30 * 60 * 1000);

	private static int getTimeout(String prop, long defaultTimeout) {
		return ((Long) java.security.AccessController
				.doPrivileged(new GetLongAction(prop, defaultTimeout)))
				.intValue(); // default: 15 seconds
	}

	public Socket createSocket(String host, int port) throws IOException {
		Socket socket = new Socket();
		SocketAddress endpoint = host != null ? new InetSocketAddress(host,
				port)
				: new InetSocketAddress(InetAddress.getByName(null), port);
		if (proxyLog.isLoggable(Log.BRIEF))
			proxyLog.log(Log.BRIEF, "createSocket: " + host.toString()
					+ ",connectTimeout=" + connectTimeout + ",responseTimeout="
					+ responseTimeout);

		socket.setSoTimeout(responseTimeout);
		socket.connect(endpoint, connectTimeout);

		return socket;
	}

	public ServerSocket createServerSocket(int port) throws IOException {
		return new ServerSocket(port);
	}
}

 

在JDK1.5,Spring1.2.8环境测试成功。

 

参考:http://java.sun.com/javase/6/docs/technotes/guides/rmi/sunrmiproperties.html

 

sun.rmi Properties

 


WARNING: The properties described here are not supported, can change at any time, and only exist in certain implementations of JavaTM Remote Method Invocation (Java RMI) from Sun Microsystems. These properties are not part of the Java RMI public API. To see a list of the properties that are part of the public API, please refer to the page, java.rmi Properties .

 


Please Note : Unless otherwise stated, any output from these properties is sent to System.err .

Each of the properties that have names ending in ".logLevel " have possible values of "SILENT ", "BRIEF ", and "VERBOSE ". These values are case insensitive and may be abbreviated as a string containing the first n letters, where n is greater than or equal to 1. If not specified, these properties have a default value of "SILENT ". A value of "BRIEF" causes the corresponding Logger to be set to the level Level.FINE , and a value of "VERBOSE" causes the corresponding Logger to be set to the level Level.FINER . Use of the logging configuration file is now preferred over the use of these sun.rmi.* system properties for logging.

Logging messages are sent to System.err . For instance, if a property is said to send output to the "transport" log, this means that logging messages are sent to a LogStream returned by java.rmi.server.LogStream.log("transport") , which sends messages to System.err , unless you've set it to something else.

 


Properties that can be set on rmid

sun.rmi.activation.execPolicy (1.3 and later)
The value of this property controls the policy that rmid uses to determine whether or not the information in an ActivationGroupDesc may be used to launch a virtual machine for an activation group. The possible values for the property are default , none , or the fully qualified name of a class that implements a method named checkExecCommand that is called to verify ActivationGroupDesc information. For details, see the tools documentation for rmid [Solaris and Linux ] [Windows ].

 

sun.rmi.activation.execTimeout (1.2 and later)
The value of this property represents the time (in milliseconds) that the activation system will wait for a spawned activation group to start up. Setting this property gives you the ability to shorten or lengthen the maximum time that rmid will wait for an activation group to start up. The default value is 30000 milliseconds (30 seconds).

 

sun.rmi.activation.groupThrottle (5.0 and later)
The value of this property represents the maximum number of activation group VMs that rmid will allow to be in the "spawning but not yet active" state simultaneously. If more VMs need to be started, they will queue up until one of the current spawn attempts either succeeds or times out. Note that this property does not limit the maximum number of active VMs; it is intended to smooth out sudden spikes of activity to avoid reaching operating system limits. While setting the value of this property to a lower number may result in a longer start-up time for rmid , and setting the value to a higher number could shorten the start-up time, setting this value too high can crash rmid , because your system may run out of resources. The default value is 3. In previous releases, this property was named sun.rmi.rmid.maxstartgroup which is no longer supported.

 

sun.rmi.activation.groupTimeout (5.0 and later)
The value of this property represents the time (in milliseconds) that rmid will wait after destroying an activation group's process before creating a new incarnation of the activation group. The default value is 60000 milliseconds (60 seconds).

 

sun.rmi.activation.snapshotInterval (1.2 and later)
This property controls the number of updates for which the activation system will wait before it serializes a snapshot of its state to the rmid log file on disk. An "update" refers to a persistent change in the state of the activation system (for example, the registration of an Activatable object) since the last snapshot was taken. Changing the value of this property can be used to make rmid re-start more quickly (by taking snapshots of the log more often) or to make rmid more efficient (by taking snapshots of the log less often). The value of this property is a positive integer value. The default value is 200.

 

sun.rmi.log.debug (1.2 and later)
If this value is true , details of rmid 's logging activity are sent to System.err .

 

sun.rmi.server.activation.debugExec (1.2 and later)
If this value is true , the activation system will print out debugging information to the command line that is used for spawning activation groups. By default, the value is false , so debugging information is not printed.

 

Properties that are useful to set on VMs that export remote objects

sun.rmi.dgc.ackTimeout (1.4 and later)
The value of this property represents the length of time (in milliseconds) that the server-side Java RMI runtime will strongly refer to a remote object (or a reference to a remote object) that has been returned from the current virtual machine as part of the result of a remote method call, until it receives positive acknowledgment from the client that the remote reference has been fully received and processed. This timeout only applies to failure situations (in which the client fails to send the acknowledgment). Setting the value too low can increase the risk of a remote object being prematurely garbage collected when the only known reference to the remote object is the one in transit as part of the remote method call result. The maximum value is Long.MAX_VALUE . The default value is 300000 (five minutes).

 

sun.rmi.dgc.checkInterval (1.1 and later)
The value of this property represents (in milliseconds) how often the Java RMI runtime checks for expired DGC leases. The default value is half the value of the java.rmi.dgc.leaseValue property.

 

sun.rmi.dgc.logLevel (1.1 and later)
This property controls the logging of incoming and outgoing calls related to DGC lease granting, renewing, and expiration. It sends output to the "dgc" log.

 

sun.rmi.dgc.server.gcInterval (1.2 and later)
When it is necessary to ensure that unreachable remote objects are unexported and garbage collected in a timely fashion, the value of this property represents the maximum interval (in milliseconds) that the Java RMI runtime will allow between garbage collections of the local heap. The default value is 3600000 milliseconds (one hour).

 

sun.rmi.loader.logLevel (1.2 and later)
This property controls the logging of each class name and codebase, whenever the Java RMI runtime attempts to load a class as a result of unmarshalling either an argument or return value. This property sends output to the "loader" log.

sun.rmi.server.exceptionTrace (1.2 and later)
This property controls the output of server-side stack traces from exceptions and errors that are thrown by dispatched, incoming remote calls. If this value is true , exception stack traces will be printed. By default (false ), exception and error stack traces are not printed.

sun.rmi.server.suppressStackTraces (1.4 and later)
If this value is true , the server-side Java RMI runtime implementation will clear the stack traces of all exceptions thrown from the current virtual machine as the result of remote calls. This property is useful for certain server applications that may wish to prevent any server-side stack trace data from accompanying an exception to be marshalled out as the result of an incoming remote call (as part of the exception's default serialized form in J2SE 1.4), perhaps for reasons of performance or confidentiality.

 

sun.rmi.transport.logLevel (1.1 and later)
This property controls detailed logging throughout the transport layer. It sends output to the "transport" log.

 

sun.rmi.transport.tcp.localHostNameTimeOut (1.1.7 and later)
The value of this property represents the time (in milliseconds) that the Java RMI runtime will wait to obtain a fully qualified domain name for the local host. The default value is 10000 milliseconds (10 seconds).

 

sun.rmi.transport.tcp.logLevel (1.1 and later)
This property provides detailed logging for the TCP-specific transport sub-layer. It sends output to the "tcp" log.

sun.rmi.transport.tcp.readTimeout (1.2.2 and later)
The value of this property represents the time (in milliseconds) used as an idle timeout for incoming TCP connections the Java RMI runtime uses. The value is passed to java.net.Socket.setSoTimeout . This property is used only for cases where a client has not dropped an unused connection as it should (see sun.rmi.transport.connectionTimeout ). The default value is 2*3600*1000 milliseconds (two hours).

 

sun.rmi.transport.tcp.maxConnectionThreads (6 and later)
The value of this property controls the maximum size of the pool of threads used to handle incoming connections, and thus it places an upper bound on the number of incoming remote method invocations that can execute concurrently. Setting this property to a lower value may improve the throughput of a Java RMI server application under heavy load, but setting it too low may (depending on the nature of the application's remote invocation patterns) lead to deadlock or starvation. The default value is the maximum, Integer.MAX_VALUE , which effectively means no limit.

 

sun.rmi.transport.tcp.threadKeepAliveTime (6 and later)
The value of this property controls the amount of time that a thread in the pool of threads used to handle incoming connections will remain idle before terminating. The default value is 60000 milliseconds (one minute).

Properties that are useful to set on VMs that make remote method calls

sun.rmi.client.logCalls (1.4 and later)
If the value of this property is true , the sun.rmi.client.call logger will be set to the level Level.FINER . Remote calls are logged at the level Level.FINER , and exceptions from remote calls are logged at the level Level.FINE .

 

sun.rmi.dgc.cleanInterval (1.1 and later)
The value of this property represents the maximum length of time (in milliseconds) that the Java RMI runtime will wait before retrying a failed DGC "clean" call. The default value is 180000 milliseconds (three minutes).

 

sun.rmi.dgc.client.gcInterval (1.2 and later)
When it is necessary to ensure that DGC clean calls for unreachable remote references are delivered in a timely fashion, the value of this property represents the maximum interval (in milliseconds) that the Java RMI runtime will allow between garbage collections of the local heap. The default value is 3600000 milliseconds (one hour).

 

sun.rmi.loader.logLevel (1.2 and later)
This property controls the logging of each class name and codebase, whenever the Java RMI runtime attempts to load a class as a result of unmarshalling either an argument or return value. This property sends output to the "loader" log.

 

sun.rmi.server.logLevel (1.1 and later)
This property controls the logging of information related to outgoing calls, including some connection-reuse information. It sends output to the "transport" log.

sun.rmi.transport.connectionTimeout (1.1.6 and later)
The value of this property represents the period (in milliseconds) for which socket connections may reside in an "unused" state, before the Java RMI runtime will allow those connections to be freed (closed). The default value is 15000 milliseconds (15 seconds). See also sun.rmi.transport.tcp.readTimeout .

 

sun.rmi.transport.logLevel (1.1 and later)
This property controls detailed logging throughout the transport layer. It sends output to the "transport" log.

 

sun.rmi.transport.proxy.connectTimeout (1.1 and later)
The value of this property represents the maximum length of time (in milliseconds) that the Java RMI runtime will wait for a connection attempt (createSocket ) to complete, before attempting to contact the server using HTTP. This property is only used when the http.proxyHost property is set and the value of java.rmi.server.disableHttp is false . The default value is 15000 milliseconds (15 seconds).

sun.rmi.transport.proxy.eagerHttpFallback (1.4.1 and later)
If this value is true and the java.rmi.server.disableHttp system property is not set, then the default global RMISocketFactory will fall back to HTTP tunneling when any java.net.SocketException is thrown from an initial (direct) connection attempt, as opposed to the default behavior of falling back to HTTP tunneling only if an initial connection attempt throws a java.net.UnknownHostException or java.net.NoRouteToHostException .

This setting can be useful when dealing with firewalls that deny (instead of ignore) connection attempts to unauthorized ports, resulting in java.net.ConnectionException s inside the client's default global RMISocketFactory . If this system property is not enabled, then such ConnectionException s will not trigger HTTP fallback, since they are neither UnknownHostException s nor NoRouteToHostException s. However, if this system property is enabled, then the ConnectionException s will cause HTTP fallback to occur, because ConnectionException is a subclass of SocketException .

 

sun.rmi.transport.proxy.logLevel (1.1 and later)
This property controls the logging of events (createSocket and createServerSocket ) when the default RMISocketFactory class is used. This type of logging is likely to be useful for applications that use Java RMI over HTTP. Events in custom socket factories are not logged by this property. It sends some messages to the "proxy" log, and others to the "transport" log.

 

sun.rmi.transport.tcp.handshakeTimeout (1.4 and later)
The value of this property represents the length of time (in milliseconds) that the client-side Java RMI runtime will use as a socket read timeout when reading initial handshake data (protocol acknowledgment) when establishing a new JRMP connection. This property is used to configure how long the Java RMI runtime will wait before deciding that a TCP connection accepted by a remote server cannot actually be used, either because the entity listening on the remote host's port is not actually a Java RMI server, or because the server is somehow not functioning correctly. The maximum value is Integer.MAX_VALUE , and a value of zero indicates an infinite timeout. The default value is 60000 (one minute).

 

sun.rmi.transport.tcp.responseTimeout (1.4 and later)
The value of this property represents the length of time (in milliseconds) that the client-side Java RMI runtime will use as a socket read timeout on an established JRMP connection when reading response data for a remote method invocation. Therefore, this property can be used to impose a timeout on waiting for the results of remote invocations; if this timeout expires, the associated invocation will fail with a java.rmi.RemoteException . Setting this property should be done with due consideration, however, because it effectively places an upper bound on the allowed duration of any successful outgoing remote invocation. The maximum value is Integer.MAX_VALUE , and a value of zero indicates an infinite timeout. The default value is zero (no timeout).

 

sun.rmi.transport.tcp.logLevel (1.1 and later)
This property provides detailed logging for the TCP-specific transport sub-layer. It sends output to the "tcp" log.

<!-- Body text ends here --> <!-- ============================================================== -->


Copyright © 2006 Sun Microsystems, Inc. All Rights Reserved.

Please send comments to: rmi-comments@java.sun.com

Sun
Java Software

<script src="http://java.sun.com/js/omi/jsc/s_code_remote.js"></script><script src="http://www-cdn.sun.com/share/metrics/metrics_group1.js"></script>

分享到:
评论

相关推荐

    Spring RMI

    - **RMI超时设置**:可以配置RMI连接和操作的超时时间,防止因网络延迟导致的应用阻塞。 - **RMI缓存**:客户端可以缓存远程对象的引用,减少重复的远程查找操作。 - **负载均衡**:通过RMI注册表,可以将请求分发到...

    Spring-RMI.rar_spring rmi

    在Java世界中,远程方法调用(Remote Method Invocation...通过"SpringRMIClient"和"SpringRMIServer"示例,我们可以清晰地了解如何在Spring框架下设置和调用RMI服务。这在构建可扩展、高可用的企业级应用中非常有用。

    Spring-RMI (RMI调用, HTTP调用)

    Spring通过提供RMI集成,简化了RMI服务的创建和调用。以下是一些关键点: 1. **服务接口定义**:首先,你需要定义一个远程服务接口,这个接口将包含你希望在远程进程中执行的方法。 2. **服务实现**:接着,创建该...

    spring RMI 实用分享

    1. 远程调用超时:检查网络环境,调整Spring的`RmiClientInterceptor`或`RmiServerInterceptor`的超时设置。 2. 类装载问题:确保客户端和服务端有相同的类路径,或者使用`java.rmi.server.codebase`属性指定类库的...

    java RMI 总结

    RMI是构建分布式应用程序的基础,尤其适用于那些需要跨越网络进行交互的Java应用。在这个总结中,我们将深入探讨RMI的核心概念、工作原理以及常见问题。 **RMI核心概念** 1. **远程接口(Remote Interface)**:...

    Java RMI 例子 和一些常见问题

    RMI使得开发分布式应用程序变得更加简单,尤其适用于服务器端应用,如企业级服务、云应用等。以下是对Java RMI的详细解释以及一些常见问题的解答。 ### 1. RMI基本概念 RMI的核心是远程接口(Remote Interface),...

    Spring HttpInvoker的封装

    - **超时设置**:根据业务需求设置请求超时时间。 - **连接池管理**:对于高并发场景,可以考虑使用连接池来提高性能和资源利用率。 **4. 使用封装后的HttpInvoker** 使用封装后的HttpInvoker非常简单,只需要调用...

    Hessian和Spring集成示例

    Spring支持多种服务暴露方式,包括HTTP、RMI、JMS等,而Hessian就是其中一种。 Hessian与Spring的集成主要涉及以下几个步骤: 1. **添加依赖**:在项目中引入Hessian的库,这通常通过Maven或Gradle的依赖管理来...

    Spring攻略(第二版 中文高清版).part1

    12.1 设置Spring Roo开发环境 483 12.1.1 问题 483 12.1.2 解决方案 483 12.1.3 工作原理 483 12.2 创建第一个Spring Roo项目 486 12.2.1 问题 486 12.2.2 解决方案 486 12.2.3 工作原理 486 12.3 ...

    Spring攻略(第二版 中文高清版).part2

    12.1 设置Spring Roo开发环境 483 12.1.1 问题 483 12.1.2 解决方案 483 12.1.3 工作原理 483 12.2 创建第一个Spring Roo项目 486 12.2.1 问题 486 12.2.2 解决方案 486 12.2.3 工作原理 486 12.3 ...

    springboot-httpinvoker-demo.zip

    1. 性能优化:可以通过设置连接池、超时时间等参数来优化网络通信性能。另外,对于频繁调用的服务,可以考虑使用缓存来减少远程调用次数。 2. 安全性:由于HTTP Invoker基于HTTP,所以需要考虑HTTPS加密传输以防止...

    架构师面试题系列之Dubbo面试专题及答案(29题).docx

    - **rmi协议**:基于JDK标准的RMI实现,需要实现Serializable接口,使用Java标准序列化,适用于消费者和提供者数量相近的场景,支持文件传输,但存在安全漏洞。 - **http协议**:基于Http表单提交,适合提供者数量...

    JAVA面试_Dubbo常问面试题30个.pdf

    - 这是一种单一长连接和NIO异步通讯的协议,适用于高并发、小数据量的服务调用,特别是当消费者数量远大于提供者时。 - 使用TCP协议,采用Hessian序列化,能提高效率。 - 异步传输可以有效利用网络资源,但可能...

    Dubbo面试及答案(上).pdf

    1. **dubbo协议**:使用单一长连接和NIO异步通讯,适用于大并发、小数据量的服务调用,以及消费者数量远大于提供者的情况。它基于TCP协议,使用Hessian序列化,具有高效性和低延迟的特点。 2. **rmi协议**:遵循JDK...

    Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)

    在本篇博文中,我们将深入探讨如何将Dubbo、Zookeeper和Spring这三大技术整合,以构建一个基于Zookeeper的分布式服务系统。Dubbo是一个高性能、轻量级的Java RPC框架,Zookeeper则是一个分布式协调服务,而Spring是...

    Dubbo服务框架面试题及答案.pdf

    关于超时时间设置,Dubbo允许在服务提供者和服务消费者两端设置。服务提供者端的超时时间更接近服务的实际特性,而消费者端的设置优先级更高,可以更灵活地控制调用超时。 Dubbo支持多种注册中心,如Multicast、...

    14-Dubbo面试题.docx

    : Dubbo 提供了多种超时设置方式,包括连接超时、读取超时、写入超时等。 * Dubbo 使用的是什么通信框架?:Dubbo 使用了 Netty 框架作为通信框架。 * Dubbo 支持哪些协议,它们的优缺点有哪些?:Dubbo 支持多种...

    Dubbo面试.pdf

    在面试中被问到如何设置Dubbo的超时时间时,需要知道有以下两种设置方式: 1. 服务提供者端设置超时时间:这是推荐的方式,因为服务提供者更清楚自身服务的特性。 2. 服务消费者端设置超时时间:这种方式的优先级更...

Global site tag (gtag.js) - Google Analytics