java開發之netty裡整合spring注入mysq連線池(一)
阿新 • • 發佈:2019-01-27
netty的效能非常高,能達到8000rps以上,見
1.準備好需要的jar包
點選(此處)摺疊或開啟
- spring.jar
//spring包
- netty-3.2.4.Final.jar
// netty庫
- commons-dbcp.jar
// dbcp資料庫連線池
- mysql-connector-java-5.1.6.jar
// dbcp資料庫連線池需要依賴
- commons-logging.jar
//spring.jar需要依賴
- commons-pool.jar
2.新建java工程TestNettyServer
2.1匯入netty的例子
HttpServer.java
點選(此處
- package
org.jboss.netty.example.http.snoop;
- import java.net.InetSocketAddress;
- import java.util.concurrent.Executors;
- import org.jboss.netty.bootstrap.ServerBootstrap;
- import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
- public class HttpServer
{
- public static
void main(
- // Configure the server.
- ServerBootstrap bootstrap
= new ServerBootstrap(
- new NioServerSocketChannelFactory(
- Executors.newCachedThreadPool(),
- Executors.newCachedThreadPool()));
- // Set up the event pipeline factory.
- bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
- // Bind and start to accept incoming connections.
- bootstrap.bind(new
InetSocketAddress(8081));
- }
- }
點選(此處)摺疊或開啟
- package
org.jboss.netty.example.http.snoop;
- import static
org.jboss.netty.channel.Channels.*;
- import org.jboss.netty.channel.ChannelPipeline;
- import org.jboss.netty.channel.ChannelPipelineFactory;
- import org.jboss.netty.handler.codec.http.HttpContentCompressor;
- import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
- import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
- public class HttpServerPipelineFactory
implements ChannelPipelineFactory
{
- public ChannelPipeline getPipeline()
throws Exception
{
- // Create a default pipeline implementation.
- ChannelPipeline pipeline = pipeline();
- // Uncomment the following line if you want HTTPS
- //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
- //engine.setUseClientMode(false);
- //pipeline.addLast("ssl", new SslHandler(engine));
- pipeline.addLast("decoder",
new HttpRequestDecoder());
- // Uncomment the following line if you don't want to handle HttpChunks.
- //pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
- pipeline.addLast("encoder",
new HttpResponseEncoder());
- // Remove the following line if you don't want automatic content compression.
- pipeline.addLast("deflater",
new HttpContentCompressor());
- pipeline.addLast("handler",
new HttpRequestHandler());
- return pipeline;
- }
- }
點選(此處)摺疊或開啟
- package
org.jboss.netty.example.http.snoop;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.apache.commons.dbcp.BasicDataSource;
- /**
- * 連線和使用資料庫資源的工具類
- *
- * @author yifangyou
- * @version gtm 2010-09-27
- */
- public class DatabaseUtil
{
- /**
- * 資料來源
- */
- private BasicDataSource
dataSource;
- /**
- * 資料庫連線
- */
- public Connection conn;
- /**
- * 獲取資料來源
- * @return 資料來源
- */
- public BasicDataSource getDataSource()
{
- return
dataSource;
- }
- /**
- * 設定資料來源
- * @param dataSource 資料來源
- */
- public void setDataSource(BasicDataSource
dataSource)
{
- this.dataSource
= dataSource;
- }
- /**
- * 獲取資料庫連線
- * @return conn
- */
- public Connection
getConnection()
{
- try {
- conn =
dataSource.getConnection();
- } catch
(Exception e)
{
- e.printStackTrace();
- return
null;
- }
- return conn;
- }
- /**
- * 關閉資料庫連線
- * @param conn
- */
- public void closeConnection(Connection conn)
{
- if (null
!= conn)
{
- try
{
- conn.close();
- conn =
null;
- }
catch (SQLException e)
{
- e.printStackTrace();
- }
- }
- }
- /**
- * 獲取執行SQL的工具
- * @param conn 資料庫連線
- * @param sql SQL語句
- * @return prepStmt
- */
- public PreparedStatement getPrepStatement(Connection conn,
String sql)
{
- PreparedStatement prepStmt
= null;
- try {
- prepStmt = conn.prepareStatement(sql);
- } catch
(SQLException e)
{
- e.printStackTrace();
- }
- return prepStmt;
- }
- /**
- * 關閉資料庫資源
- * @param prepStmt
- */
- public void closePrepStatement(PreparedStatement prepStmt)
{
- if (null
!= prepStmt)
{
- try
{
- prepStmt.close();
- prepStmt =
null;
- }
catch (SQLException e)
{
- e.printStackTrace();
- }
- }
- }
- }
在工程下新增lib目錄
把jar包拷進去
點選工程的右鍵“propertis”->Java Build Path->Libraries->Add JARS 3.分析如何注入
Netty的執行過程是 因此我們需要
Spring注入入口在HttpServer裡的main函式
把HttpRequestHandler注入到HttpServerPipelineFactory,
把HttpServerPipelineFactory注入到HttpServer
另外我們在HttpRequestHandler需要用到mysql連線池,因此還要把mysql連線池注入到HttpRequestHandler