Path: blob/master/src/java.rmi/share/classes/sun/rmi/transport/DGCAckHandler.java
41154 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.rmi.transport;2627import java.rmi.server.UID;28import java.security.AccessController;29import java.security.PrivilegedAction;30import java.util.ArrayList;31import java.util.Collections;32import java.util.HashMap;33import java.util.List;34import java.util.Map;35import java.util.concurrent.Future;36import java.util.concurrent.ScheduledExecutorService;37import java.util.concurrent.TimeUnit;38import sun.rmi.runtime.RuntimeUtil;3940/**41* Holds strong references to a set of remote objects, or live remote42* references to remote objects, after they have been marshalled (as43* remote references) as parts of the arguments or the result of a44* remote invocation. The purpose is to prevent remote objects or45* live remote references that might otherwise be determined to be46* unreachable in this VM from being locally garbage collected before47* the receiver has had an opportunity to register the unmarshalled48* remote references for DGC.49*50* The references are held strongly until an acknowledgment has been51* received that the receiver has had an opportunity to process the52* remote references or until a timeout has expired. For remote53* references sent as parts of the arguments of a remote invocation,54* the acknowledgment is the beginning of the response indicating55* completion of the remote invocation. For remote references sent as56* parts of the result of a remote invocation, a UID is included as57* part of the result, and the acknowledgment is a transport-level58* "DGCAck" message containing that UID.59*60* @author Ann Wollrath61* @author Peter Jones62**/63public class DGCAckHandler {6465/** timeout for holding references without receiving an acknowledgment */66@SuppressWarnings("removal")67private static final long dgcAckTimeout = // default 5 minutes68AccessController.doPrivileged((PrivilegedAction<Long>) () ->69Long.getLong("sun.rmi.dgc.ackTimeout", 300000));7071/** thread pool for scheduling delayed tasks */72@SuppressWarnings("removal")73private static final ScheduledExecutorService scheduler =74AccessController.doPrivileged(75new RuntimeUtil.GetInstanceAction()).getScheduler();7677/** table mapping ack ID to handler */78private static final Map<UID,DGCAckHandler> idTable =79Collections.synchronizedMap(new HashMap<UID,DGCAckHandler>());8081private final UID id;82private List<Object> objList = new ArrayList<>(); // null if released83private Future<?> task = null;8485/**86* Creates a new DGCAckHandler, associated with the specified UID87* if the argument is not null.88*89* References added to this DGCAckHandler will be held strongly90* until its "release" method is invoked or (after the91* "startTimer" method has been invoked) the timeout has expired.92* If the argument is not null, then invoking the static93* "received" method with the specified UID is equivalent to94* invoking this instance's "release" method.95**/96DGCAckHandler(UID id) {97this.id = id;98if (id != null) {99assert !idTable.containsKey(id);100idTable.put(id, this);101}102}103104/**105* Adds the specified reference to this DGCAckHandler.106**/107synchronized void add(Object obj) {108if (objList != null) {109objList.add(obj);110}111}112113/**114* Starts the timer for this DGCAckHandler. After the timeout has115* expired, the references are released even if the acknowledgment116* has not been received.117**/118synchronized void startTimer() {119if (objList != null && task == null) {120task = scheduler.schedule(new Runnable() {121public void run() {122if (id != null) {123idTable.remove(id);124}125release();126}127}, dgcAckTimeout, TimeUnit.MILLISECONDS);128}129}130131/**132* Releases the references held by this DGCAckHandler.133**/134synchronized void release() {135if (task != null) {136task.cancel(false);137task = null;138}139objList = null;140}141142/**143* Causes the DGCAckHandler associated with the specified UID to144* release its references.145**/146public static void received(UID id) {147DGCAckHandler h = idTable.remove(id);148if (h != null) {149h.release();150}151}152}153154155