001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.connectioncache.spi.transport;
038:
039: import java.io.IOException;
040:
041: /** A concurrent mostly non-blocking connection cache. Here a Connection is an
042: * abstraction of a Socket or SocketChannel: basically some sort of resource
043: * that is expensive to acquire, and can be re-used freely. The cache
044: * maintains a loose upper bound on the number of cached connections, and
045: * reclaims connections as needed.
046: * <P>
047: * This cache places minimal requirements on the Connections that it contains:
048: * <ol>
049: * <li>A Connection must implement a close() method. This is called when idle
050: * connections are reclaimed.
051: * <li>A Connection must be usable as a HashMap key.
052: * <li>There must be a ContactInfo class that is used to create Connection
053: * instances. The ContactInfo class must support a create() method that
054: * returns a Connection.
055: * <li>The ContactInfo must be usable as a HashMap key.
056: * <li>All instances created from a ContactInfo are equal; that is, any request
057: * sent to a particular ContactInfo can used an instance created from that
058: * ContactInfo. For example, in the CORBA case, IP host and port is not always
059: * sufficient: we may also need the Codeset type that indicates how Strings are
060: * encoded. Basically, protocols (like GIOP) that bind session state to a
061: * Connection may need more than transport information in the ContactInfo.
062: * </ol>
063: * <P>
064: * Some simple methods are provided for monitoring the state of the cache:
065: * numbers of busy and idle connections, and the total number of connections in
066: * the cache.
067: */
068: public interface OutboundConnectionCache<C extends Connection> extends
069: ConnectionCache<C> {
070: /** Configured maximum number of connections supported per ContactInfo.
071: */
072: int maxParallelConnections();
073:
074: /** Determine whether a new connection could be created by the
075: * ConnectionCache or not.
076: */
077: boolean canCreateNewConnection(ContactInfo<C> cinfo);
078:
079: /** Return a Connection corresponding to the given ContactInfo.
080: * This works as follows:
081: * <ul>
082: * <li>Call the finder. If it returns non-null, use that connection;
083: * (Note that this may be a new connection, created in the finder)
084: * <li>otherwise, Use an idle connection, if one is available;
085: * <li>otherwise, create a new connection, if not too many connections are
086: * open;
087: * <li>otherwise, use a busy connection.
088: * </ul>
089: * Note that creating a new connection requires EITHER:
090: * <ul>
091: * <li>there is no existing connection for the ContactInfo
092: * <li> OR the total number of connections in the cache is less than the
093: * HighWaterMark and the number of connections for this ContactInfo
094: * is less than MaxParallelConnections.
095: * </ul>
096: * We will always return a
097: * Connection for a get call UNLESS we have no existing connection and
098: * an attempt to create a new connection fails. In this case, the
099: * IOException thrown by ContactInfo.create is propagated out of this
100: * method.
101: * <P>
102: * It is possible that the cache contains connections that no longer connect
103: * to their destination. In this case, it is the responsibility of the
104: * client of the cache to close the broken connection as they are detected.
105: * Connection reclamation may also handle the cleanup, but note that a
106: * broken connection with pending responses will never be reclaimed.
107: * <P>
108: * Note that the idle and busy connection collections that are
109: * passed to the finder are unmodifiable collections. They have iterators
110: * that return connections in LRU order, with the least recently used
111: * connection first. This is done to aid a finder that wishes to consider
112: * load balancing in its determination of an appropriate connection.
113: * <P>
114: */
115: C get(ContactInfo<C> cinfo, ConnectionFinder<C> finder)
116: throws IOException;
117:
118: /** Behaves the same as get( ContactInfo<C>, ConnectionFinder<C> )
119: * except that no connection finder is provided, so that step is
120: * ignored.
121: */
122: C get(ContactInfo<C> cinfo) throws IOException;
123:
124: /** Release a Connection previously obtained from get. Connections that
125: * have been released as many times as they have been returned by
126: * get are idle; otherwise a Connection is busy. Some number of
127: * responses (usually 0 or 1) may be expected ON THE SAME CONNECTION
128: * even for an idle connection. We maintain a count of the number of
129: * outstanding responses we expect for protocols that return the response
130: * on the same connection on which the request was received. This is
131: * necessary to prevent reclamation of a Connection that is idle, but
132: * still needed to send responses to old requests.
133: */
134: void release(C conn, int numResponseExpected);
135:
136: /** Inform the cache that a response has been received on a particular
137: * connection. This must also be called in the event that no response
138: * is received, but the client times out waiting for a response, and
139: * decides to abandon the request.
140: * <P>
141: * When a Connection is idle, and has no pending responses, it is
142: * eligible for reclamation.
143: */
144: void responseReceived(C conn);
145: }
|