这篇文章主要介绍“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的源码是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程之家网站,小编会继续努力为大家带来更多实用的文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。