示例#1
0
  /**
   * Gets a pooled connection for id. The pooled connection might be newly created, as governed by
   * the maxSize and prefSize settings. If a pooled connection is unavailable and cannot be created
   * due to the maxSize constraint, this call blocks until the constraint is removed or until
   * 'timeout' ms has elapsed.
   *
   * @param id identity of the connection to get
   * @param timeout the number of milliseconds to wait before giving up
   * @param factory the factory to use for creating the connection if creation is necessary
   * @return a pooled connection
   * @throws NamingException the connection could not be created due to an error.
   */
  public PooledConnection getPooledConnection(
      Object id, long timeout, PooledConnectionFactory factory) throws NamingException {

    d("get(): ", id);
    d("size: ", map.size());

    expungeStaleConnections();

    Connections conns;
    synchronized (map) {
      conns = getConnections(id);
      if (conns == null) {
        d("get(): creating new connections list for ", id);

        // No connections for this id so create a new list
        conns = new Connections(id, initSize, prefSize, maxSize, factory);
        ConnectionsRef connsRef = new ConnectionsRef(conns);
        map.put(id, connsRef);

        // Create a weak reference to ConnectionsRef
        Reference<ConnectionsRef> weakRef = new ConnectionsWeakRef(connsRef, queue);

        // Keep the weak reference through the element of a linked list
        weakRefs.add(weakRef);
      }
    }

    d("get(): size after: ", map.size());

    return conns.get(timeout, factory); // get one connection from list
  }
示例#2
0
 void serv(int i) {
   try {
     S = new DatagramSocket(i);
     S.setSoTimeout(5000);
   } catch (Exception exception) {
     exception.printStackTrace();
     if (S != null)
       try {
         S.close();
       } catch (Exception exception2) {
       }
     return;
   }
   System.out.println(DF.format(new Date()) + "Waiting connections ...");
   do
     try {
       buf = new byte[256];
       P = new DatagramPacket(buf, buf.length);
       S.receive(P);
       Connection connection =
           (Connection) Connections.get(P.getAddress().toString() + P.getPort());
       if (connection == null) {
         System.out.println(
             DF.format(new Date())
                 + "Connect from "
                 + " ("
                 + P.getAddress().getHostAddress()
                 + ")");
         connection = new Connection(this, P);
         Connections.put(P.getAddress().toString() + P.getPort(), connection);
         connection.start();
       } else {
         connection.packet(P);
       }
     } catch (Exception exception1) {
     }
   while (true);
 }