1. 程式人生 > >java開發之netty裡整合spring注入mysq連線池(一)

java開發之netty裡整合spring注入mysq連線池(一)

netty的效能非常高,能達到8000rps以上,見

1.準備好需要的jar包

點選(此處)摺疊或開啟

  1. spring.jar //spring包
  2. netty-3.2.4.Final.jar // netty庫
  3. commons-dbcp.jar // dbcp資料庫連線池
  4. mysql-connector-java-5.1.6.jar // dbcp資料庫連線池需要依賴
  5. commons-logging.jar //spring.jar需要依賴
  6. commons-pool.jar

2.新建java工程TestNettyServer

2.1匯入netty的例子
HttpServer.java


點選(此處

)摺疊或開啟

  1. package org.jboss.netty.example.http.snoop;
  2. import java.net.InetSocketAddress;
  3. import java.util.concurrent.Executors;
  4. import org.jboss.netty.bootstrap.ServerBootstrap;
  5. import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
  6. public class HttpServer {
  7.     public static void main(
    String[] args) {
  8.         // Configure the server.
  9.         ServerBootstrap bootstrap = new ServerBootstrap(
  10.                 new NioServerSocketChannelFactory(
  11.                         Executors.newCachedThreadPool(),
  12.                         Executors.newCachedThreadPool()));
  13.         // Set up the event pipeline factory.

  14.         bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
  15.         // Bind and start to accept incoming connections.
  16.         bootstrap.bind(new InetSocketAddress(8081));
  17.     }
  18. }
HttpServerPipelineFactory.java

點選(此處)摺疊或開啟

  1. package org.jboss.netty.example.http.snoop;
  2.   import static org.jboss.netty.channel.Channels.*;
  3.   import org.jboss.netty.channel.ChannelPipeline;
  4.   import org.jboss.netty.channel.ChannelPipelineFactory;
  5.   import org.jboss.netty.handler.codec.http.HttpContentCompressor;
  6.   import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
  7.   import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
  8.   public class HttpServerPipelineFactory implements ChannelPipelineFactory {
  9.       public ChannelPipeline getPipeline() throws Exception {
  10.           // Create a default pipeline implementation.
  11.           ChannelPipeline pipeline = pipeline();
  12.           // Uncomment the following line if you want HTTPS
  13.           //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
  14.           //engine.setUseClientMode(false);
  15.           //pipeline.addLast("ssl", new SslHandler(engine));
  16.           pipeline.addLast("decoder", new HttpRequestDecoder());
  17.           // Uncomment the following line if you don't want to handle HttpChunks.
  18.           //pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
  19.           pipeline.addLast("encoder", new HttpResponseEncoder());
  20.           // Remove the following line if you don't want automatic content compression.
  21.           pipeline.addLast("deflater", new HttpContentCompressor());
  22.           pipeline.addLast("handler", new HttpRequestHandler());
  23.           return pipeline;
  24.       }
  25.   }
HttpRequestHandler.java

點選(此處)摺疊或開啟

  1. package org.jboss.netty.example.http.snoop;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import org.apache.commons.dbcp.BasicDataSource;
  8. /**
  9.  * 連線和使用資料庫資源的工具類
  10.  *
  11.  * @author yifangyou
  12.  * @version gtm 2010-09-27
  13.  */
  14. public class DatabaseUtil {
  15.     /**
  16.      * 資料來源
  17.      */
  18.     private BasicDataSource dataSource;
  19.     /**
  20.      * 資料庫連線
  21.      */
  22.     public Connection conn;
  23.     /**
  24.      * 獲取資料來源
  25.      * @return 資料來源
  26.      */
  27.     public BasicDataSource getDataSource() {
  28.         return dataSource;
  29.     }
  30.     /**
  31.      * 設定資料來源
  32.      * @param dataSource 資料來源
  33.      */
  34.     public void setDataSource(BasicDataSource dataSource) {
  35.         this.dataSource = dataSource;
  36.     }
  37.     /**
  38.      * 獲取資料庫連線
  39.      * @return conn
  40.      */
  41.     public Connection getConnection() {
  42.         try {
  43.             conn = dataSource.getConnection();
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.             return null;
  47.         }
  48.         return conn;
  49.     }
  50.     /**
  51.      * 關閉資料庫連線
  52.      * @param conn
  53.      */
  54.     public void closeConnection(Connection conn) {
  55.         if (null != conn) {
  56.             try {
  57.                 conn.close();
  58.                 conn = null;
  59.             } catch (SQLException e) {
  60.                 e.printStackTrace();
  61.             }
  62.         }
  63.     }
  64.     /**
  65.      * 獲取執行SQL的工具
  66.      * @param conn 資料庫連線
  67.      * @param sql SQL語句
  68.      * @return prepStmt
  69.      */
  70.     public PreparedStatement getPrepStatement(Connection conn, String sql) {
  71.         PreparedStatement prepStmt = null;
  72.         try {
  73.             prepStmt = conn.prepareStatement(sql);
  74.         } catch (SQLException e) {
  75.             e.printStackTrace();
  76.         }
  77.         return prepStmt;
  78.     }
  79.     /**
  80.      * 關閉資料庫資源
  81.      * @param prepStmt
  82.      */
  83.     public void closePrepStatement(PreparedStatement prepStmt) {
  84.         if (null != prepStmt) {
  85.             try {
  86.                 prepStmt.close();
  87.                 prepStmt = null;
  88.             } catch (SQLException e) {
  89.                 e.printStackTrace();
  90.             }
  91.         }
  92.     }
  93. }
2.2 匯入jar包
在工程下新增lib目錄
把jar包拷進去
  點選工程的右鍵“propertis”->Java Build Path->Libraries->Add JARS 3.分析如何注入
Netty的執行過程是 因此我們需要
Spring注入入口在HttpServer裡的main函式
把HttpRequestHandler注入到HttpServerPipelineFactory,
把HttpServerPipelineFactory注入到HttpServer
另外我們在HttpRequestHandler需要用到mysql連線池,因此還要把mysql連線池注入到HttpRequestHandler