您好,登錄后才能下訂單哦!
這篇文章主要介紹“Connection.java的源碼是什么”,在日常操作中,相信很多人在Connection.java的源碼是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Connection.java的源碼是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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;//管道命令計數器 private int timeout = 2000;//socket超時時間 public int getTimeout() {//獲取socket超時時間 return timeout; } public void setTimeout(int timeout) {//設置socket超時時間 this.timeout = timeout; } public void setTimeoutInfinite() {//設置無窮大超時時間 try { socket.setSoTimeout(0); } catch (SocketException ex) { throw new JedisException(ex); } } public void rollbackTimeout() {//回滾超時時間設置 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);//發送命令 pipelinedCommands++;//增加計數器 return this; } public Connection(String host, int port) {//構造connection super(); this.host = host; this.port = port; } public String getHost() {//獲取IP return host; } public void setHost(String host) {//設置IP this.host = host; } public int getPort() {//獲取端口 return port; } public void setPort(int port) {//設置端口 this.port = port; } public Connection() {//構造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();//關閉流 if (!socket.isClosed()) { socket.close(); }//關閉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() {//獲取響應 pipelinedCommands--; return (String) protocol.read(inputStream); } public String getBulkReply() {//獲取響應 pipelinedCommands--; return (String) protocol.read(inputStream); } public int getIntegerReply() {//獲取響應 pipelinedCommands--; return ((Integer) protocol.read(inputStream)).intValue(); } @SuppressWarnings("unchecked") public List<String> getMultiBulkReply() {//獲取響應 pipelinedCommands--; return (List<String>) protocol.read(inputStream); } @SuppressWarnings("unchecked") public List<Object> getObjectMultiBulkReply() {//獲取響應 pipelinedCommands--; return (List<Object>) protocol.read(inputStream); } public List<Object> getAll() {//批量獲取響應 List<Object> all = new ArrayList<Object>(); while (pipelinedCommands > 0) { all.add(protocol.read(inputStream)); pipelinedCommands--; } return all; } }
這個函數很有意思。
public boolean isConnected() {//判斷是否連接上了 return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown(); }
到此,關于“Connection.java的源碼是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。