When we launched a Server in the program, this Server needs to communicate with another Server (such as the old C program built on the old system). In order to increase efficiency, it is necessary to consider my solve when you need to build a connection pool. Program, deficiencies are clear.
1: SocketAdapter class, such inheritable Socket, overloaded the CLOSE method of the socket class, the purpose is to close it when the user shuts down the socket, it is not turned off inside the connection pool. Package com.tarena.socketpool; import java.net. *; import java.io iexception; / ** *
Socket connection simple implementation: p> *
Copyright: Copyright Tarena (C) 2005 P> *
Company: Tarena p> * @Author Chengxing * @version 1.0 * / public class connectAdapter Extends socket {/ ** * Connection status * / private boolean status = true; / ** * default constructor * / public ConnectionAdapter () {super ();} public ConnectionAdapter (String host, int port) throws UnknownHostException, IOException {super (host, port);} / ** * Judging whether this connection is idle * @Return Boolean idle returns Ture, otherwise false * / public boolean isfree () {return status;} / ** * When using this connection, set the status to false * / public void setbusy ( ) {This.status = false;} / ** * When the client is closed, the status is set to True (idle) * / public void close () {system.out.println ("Close: set the status is free" ); Status = true;} public void design () {// close socket connection close (); // system.out.println ("Close Success");}} second Class Connection Manager. Package com.tarena.socketpool;
Import java.lang.reflect. *; import java.util.properties; / ** *
Connection Manager p> *
Copyright: Copyright Tarena (C) 2005 P> *
Company: Tarena p> * @author chengxing * @version 1.0 * / public class ConnectionManager {// test procedure default connection pool implementation class public static final String PROVIDER_CLASS = "com.tarena.socketpool.MyConnectionProvider"; // test Default IP Public String Host = "127.0.0.1"; // Test Program Default port number public static final string port = "9880"; / ** * Register the static anonymous block of the hook program * / static {/ / Increase the release cycle of hook control resources runtime runtime = runtime.Getruntime (); class c = runtime.getClass (); try {method m = c.getMethod ("addshutdownhook", new class [] {thread.class}; M.Invoke (Runtime, New Object [] {new shutdownthread ();} catch (no.printstackTrace () {E.PrintStackTrace (); // ignore - the user might not be running jdk 1.3 or later.} catch ( Exception e) {E.PrintStackTrace ();}} / ** * Default constructor * / public ConnectionManager () {} / ** * Get and initialize a connection pool * Connection pool implementation class passed through system parameters, through the command line -DConnectionProvider = YourImplclass * If there is no specified implementation, the system default implementation class * The parameter list passed through the command line is below the other host name -Dhost = 192.168.0.200 * Other port number -Dport = 9880 * Minimum connection number -dmax_size = 10 * Maximum junction -dmin_size = 20 * The value can be changed , but can not change the parameters, the maximum number of links, and * the minimum number of connections may be omitted, default values are 20 and 10 * @return ConnectionProvider * / public static ConnectionProvider getConnectionProvider () throws Exception {String provider_class = System.getProperty ( "ConnectionProvider") ; If (provider_class ==
NULL) provider_class = provider_class; string host = system.getProperty ("host"); if (host == null) host = host;
String port = system.getProperty ("port"); if (port == null) port = port;
String max_size = system.getProperty ("max_size"); string min_size = system.getproperty ("min_size");
Properties pro = new Properties (); pro.setProperty (ConnectionProvider.SERVER_IP, host); pro.setProperty (ConnectionProvider.SERVER_PORT, port); if (! Max_size = null) pro.setProperty (ConnectionProvider.MAX_SIZE, max_size); if ( min_size = null) pro.setProperty (ConnectionProvider.MIN_SIZE, min_size);! // achieved class System.out.println (provider_class) by reflection; System.out.flush (); class provider_impl = Class.forName (provider_class); // Since a list mode, the static method callback method m = provider_impl.getMethod ( "newInstance", new Class [] {java.util.Properties.class}); ConnectionProvider provider = null; try {provider = (ConnectionProvider) m .invoke (provider_impl, new object [] {pro});} catch (exception e) {E.printStackTrace ();}
Return Provider;} / ** * *
A hook thread: call the logout connection pool at the end of the program p> *
Description: p> *
Copyright: Copyright Tarena (C ) 2005 p> *
Company: Tarena p> * @author chengxing * @version 1.0 * / private static class ShutdownThread extends Thread {public void run () {try {ConnectionProvider provider = ConnectionManager.getConnectionProvider () ; If (provider! = Null) {provider.destroy ();}} catch (exception e) {E.PrintStackTrace ();}}}}
The third class, the interface of the connection pool defines package com.tarena.socketpool;
Import java.net. *; import java.util. *; import java.io.ioException
/ ** * *
defined abstract class, all subclasses must be implemented in a single child mode, * Unified method is public connectionProvider newinstance (); * Connect to the abstract interface of the provider, each of the subclasses that implement it It is javabean, * such a method can be controlled by the outside world p> * @see jivebeaninfo *
Copyright: Copyright Tarena (C) 2005 p> *
Company: tarena p> * @author chengxing * @version 1.0 * / public interface ConnectionProvider {public static final String SERVER_IP = "SERVER_IP_ADDRESS"; public static final String SERVER_PORT = "SERVER_IP_PORT"; public static final String MAX_SIZE = "MAX_SIZE"; public static final String MIN_SIZE = "Min_size";
/ ** * Deconed whether there is a connection in the connection pool * @return true has a connection to Return true, otherwise returns false * / public boilean ispooled ();
/ ** * When this method is called, it provides a socket * @see socket * @return socket a connection object. * / Public socket getConnection (). Java.net.socketException;
/ ** * Connection pool initialization * / public void init () throws unknownHOSTEXCEPTION, IOException; / ** * Connection pool restart * / public void restart () THROWS UNKNOWNHOSTEXCEPTION, IOEXCEPTION;
/ ** * Logout Pool * / public void destroy ();} Fourth class MyConnectionProvider, a simple implementation of a connection pool written by Package CoM.Tarena.socketPool;
Import java.util. *; import java.net. *; import java.net.socketexception; import java.io iexception;
/ ** * *
This is a simple implementation of a connecting pipeline p> *
description: imports the interface connectionProvider p> *
Copyright: Copyright Tarena (C) 2005 P > *
company: tarena p> * @Author Chengxing * @version 1.0 * / public class myconnectionProvider imports connectionProvider {
Private Properties Pro = NULL; Private Static ConnectionProvider Provider = NULL; Private Static Object Object_lock = New Object (); Private String IP; Private String Port;
/ ** * Default maximum connection * / private int max_size = 20;
/ ** * The default minimum connection number * / private int min_size = 10;
/ ** * Socket Connection pool population * / private connectionAdapter [] socketpool = null;
/ * ** pool constructed object initialization * @throws UnknownHostException unknown host connection exception * @throws IOException * / private MyConnectionProvider (Properties pro) throws UnknownHostException, IOException {ip = pro.getProperty (SERVER_IP); port = pro.getProperty (SERVER_PORT); String max_size_s = pro.getProperty (MAX_SIZE); String min_size_s = pro.getProperty (MIN_SIZE); if (max_size_s = null!) {max_size = Integer.parseInt (max_size_s);} if (! min_size_s = null) { Min_size = integer.parseint (min_size_s);}
INIT (); // Initialization connection pool when constructing an object
/ ** * Decision has already been poolized * @return boolean If Pi is returned to Ture, it returns false * / public boolean ispooled () {if (socketpool! = Null) {return true;} else return false;} / ** * Return to a connection * @return a connection object. * / Public socket getConnection () {socket s = null; for (int i = 0; i / ** * Initializes connection pool * @throws unknownhostException host ip can't find * @throws ioException This port number does not Server monitor * / public void init () throws unknownhostException, ioException { Socketpool = new connectionAdapter [max_size]; for (int i = 0; i / ** * Restart connection pool * @throws unknownhostexception * @throws ooException * / public void restart () throws unknownhostException, ioException {design; init ();