您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。