(Original) When Server encounters Server, Socket connection pool simple implementation) Select BLOG from JSAND

xiaoxiao2021-03-06  13

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: *

Copyright: Copyright Tarena (C) 2005 *

Company: Tarena * @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 *

Copyright: Copyright Tarena (C) 2005 < / p> *

Company: tarena * @Author Chengxing * @version 1.0 * / public class connectionManager {// Test program default connection pool Implementation class public static final string provider_class = "com.tarena.socketpool .MyconnectionProvider "; // The default IP public static final string host" 127.0.0.1 "; // test program default port number public static final string port =" 9880 "; / ** * Register the static status of the hook program Anonymous block * / static {// increase the release cycle of hook control resources runtime runtime = runtime.getime (); class c = runtime.getClass (); try {method m = c.getMethod ("addshutdownhook", new class [] {Thread.class}; m.invoke (runtime, new object [] {new shutdownthread ();} catch (nosuchmethodexception e) {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 to pass the system parameters to pass, through the command line -DConnectionProvider = YourImplclass * If there is no designated implementation If you use the system default implementation class * The list of parameters passed through the command line is as follows * The other host name -Dhost = 192.168.0.200 * Other port number -dport = 9880 * minimum connection number -dmax_size = 10 * Maximum junction - DMin_size = * value of 20 or more can be changed, but the parameter can not be changed, * maximum coupling and 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; 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); // by Reflection gets a class system.ord.Println (provider_class); system.out.flush (); class provider_impl = class.forname (provider_class); // Due to the single child mode, use a 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});} catc H (Exception E) {E.PrintStackTrace ();} Return Provider;} / ** * *

A hook thread: call the logout connection pool *

description: < / p> *

Copyright: Copyright Tarena (C) 2005 *

Company: tarena * @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.util. *; Import Java.io.ioException; / ** * *

defined abstract class, all subclasses must be single mode to implement, * Unified method is public connectionProvider newInstance (); * Connect the information of the provider, each implementation Its subclass is best javabean. * This way it can be controlled by external control * @see jivebeaninfo *

Copyright: Copyright Tarena (C) 2005 *

Company : Tarena * @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"; / ** * Determined whether there is connection in the connection pool * @return true has a connection to Return true, otherwise returns false * / public boilean ispooled (); / ** * When this method is called Time to provide a socket * @see socket * @return socket a connection object. * / Public socket getConnection () throws java.net.socketexception; / ** * Connect pool initialization * / Public void init () throws UnknownHostException, IOException; / ** * restart the connection pool * / public void restart () throws UnknownHostException, IOException; / ** * cancellation of connection pool * / public void destroy ();} Fourth Class MyConnectionProvider, a simple implementation of a connection pool yourself Package CoM.Tarena.socketPool; import java.util. *; Import java.net. *; Import java.net.iocketexception; import java.ioException; / * * * *

This is a simple implementation of a connecting pipeline *

Description: imports the interface connectionProvider *

Copyright: Copyright Tarena (C) 2005 *

company: tarena * @

author chengxing * @version 1.0 * / public class MyConnectionProvider implements 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; / ** * Default minimum connection number * / private int min_size = 10; / ** * Socket Connection pool number * / 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 (); / / Constructive When the connection pool} / ** * determines if it is already pooling * @return bolean If Pihua returns Ture, vice versa return false * / public boolean ispooled () {if (socketpool! = Null) {Return True; } Else Return False;} / ** * Returns a connection * @return a connection object. * / Public socket getConnection () {socket s = null; for (int i = 0; i

SocketPool [I] = New ConnectionAdapter (IP, Integer.Parseint (port));} Catch (Exception E) {// NEVER ThROW}}} // If the connection is still empty, it exceeds the maximum number of connections IF (S == NULL) {TRY {// Generate a normal connection, shut down by the client, release resources, no longer manages S = New Socket (IP, Integer.Parseint (port));} catch (Exception E) { // This exception will never throw}} Return S;} / ** * 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

转载请注明原文地址:https://www.9cbs.com/read-49859.html

New Post(0)