您好,登錄后才能下訂單哦!
這篇文章主要介紹“Connection.java的源碼是什么”,在日常操作中,相信很多人在Connection.java的源碼是什么問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Connection.java的源碼是什么”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
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();//具體操作對(duì)象 private RedisOutputStream outputStream;//socket的輸出流 private RedisInputStream inputStream;//socket的輸入流 private int pipelinedCommands = 0;//管道命令計(jì)數(shù)器 private int timeout = 2000;//socket超時(shí)時(shí)間 public int getTimeout() {//獲取socket超時(shí)時(shí)間 return timeout; } public void setTimeout(int timeout) {//設(shè)置socket超時(shí)時(shí)間 this.timeout = timeout; } public void setTimeoutInfinite() {//設(shè)置無窮大超時(shí)時(shí)間 try { socket.setSoTimeout(0); } catch (SocketException ex) { throw new JedisException(ex); } } public void rollbackTimeout() {//回滾超時(shí)時(shí)間設(shè)置 try { socket.setSoTimeout(timeout); } catch (SocketException ex) { throw new JedisException(ex); } } public Connection(String host) {//夠早一個(gè)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++;//增加計(jì)數(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()) {//如果沒有連接,才會(huì)連接對(duì)方并且給本地變量賦值 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 List<String> getMultiBulkReply() {//獲取響應(yīng) pipelinedCommands--; return (List<String>) protocol.read(inputStream); } @SuppressWarnings("unchecked") public List<Object> getObjectMultiBulkReply() {//獲取響應(yīng) pipelinedCommands--; return (List<Object>) protocol.read(inputStream); } public List<Object> getAll() {//批量獲取響應(yīng) List<Object> all = new ArrayList<Object>(); while (pipelinedCommands > 0) { all.add(protocol.read(inputStream)); pipelinedCommands--; } return all; } }
這個(gè)函數(shù)很有意思。
public boolean isConnected() {//判斷是否連接上了 return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown(); }
到此,關(guān)于“Connection.java的源碼是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。