這篇文章主要介紹“Connection.java的源碼是什么”,在日常操作中,相信很多人在Connection.java的源碼是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Connection.java的源碼是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)10多年成都定制網(wǎng)站服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)站及推廣,對成都雨棚定制等多個行業(yè)擁有豐富的網(wǎng)站推廣經(jīng)驗的網(wǎng)站建設(shè)公司。
package redis.clients.jedis; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; import java.io.*; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; public class Connection { private String host;//ip private int port = Protocol.DEFAULT_PORT;//端口 private Socket socket;//socket句柄 private Protocol protocol = new Protocol();//具體操作對象 private RedisOutputStream outputStream;//socket的輸出流 private RedisInputStream inputStream;//socket的輸入流 private int pipelinedCommands = 0;//管道命令計數(shù)器 private int timeout = 2000;//socket超時時間 public int getTimeout() {//獲取socket超時時間 return timeout; } public void setTimeout(int timeout) {//設(shè)置socket超時時間 this.timeout = timeout; } public void setTimeoutInfinite() {//設(shè)置無窮大超時時間 try { socket.setSoTimeout(0); } catch (SocketException ex) { throw new JedisException(ex); } } public void rollbackTimeout() {//回滾超時時間設(shè)置 try { socket.setSoTimeout(timeout); } catch (SocketException ex) { throw new JedisException(ex); } } public Connection(String host) {//夠早一個connection super(); this.host = host; } protected Connection sendCommand(String name, String... args) { try { connect();//連接server } catch (UnknownHostException e) { throw new JedisException("Could not connect to redis-server", e); } catch (IOException e) { throw new JedisException("Could not connect to redis-server", e); } protocol.sendCommand(outputStream, name, args);//發(fā)送命令 pipelinedCommands++;//增加計數(shù)器 return this; } public Connection(String host, int port) {//構(gòu)造connection super(); this.host = host; this.port = port; } public String getHost() {//獲取IP return host; } public void setHost(String host) {//設(shè)置IP this.host = host; } public int getPort() {//獲取端口 return port; } public void setPort(int port) {//設(shè)置端口 this.port = port; } public Connection() {//構(gòu)造connection } public void connect() throws UnknownHostException, IOException { if (!isConnected()) {//如果沒有連接,才會連接對方并且給本地變量賦值 socket = new Socket(host, port); socket.setSoTimeout(timeout); outputStream = new RedisOutputStream(socket.getOutputStream()); inputStream = new RedisInputStream(socket.getInputStream()); } } public void disconnect() {//斷開連接 if (isConnected()) { try { inputStream.close(); outputStream.close();//關(guān)閉流 if (!socket.isClosed()) { socket.close(); }//關(guān)閉socket. } catch (IOException ex) { throw new JedisException(ex); } } } public boolean isConnected() {//判斷是否連接上了 return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown(); } protected String getStatusCodeReply() {//獲取響應(yīng) pipelinedCommands--; return (String) protocol.read(inputStream); } public String getBulkReply() {//獲取響應(yīng) pipelinedCommands--; return (String) protocol.read(inputStream); } public int getIntegerReply() {//獲取響應(yīng) pipelinedCommands--; return ((Integer) protocol.read(inputStream)).intValue(); } @SuppressWarnings("unchecked") public ListgetMultiBulkReply() {//獲取響應(yīng) pipelinedCommands--; return (List ) protocol.read(inputStream); } @SuppressWarnings("unchecked") public List
這個函數(shù)很有意思。
public boolean isConnected() {//判斷是否連接上了 return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown(); }
到此,關(guān)于“Connection.java的源碼是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
新聞名稱:Connection.java的源碼是什么
鏈接分享:http://weahome.cn/article/iisjhj.html