千家信息网

Connection.java的源码是什么

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,这篇文章主要介绍"Connection.java的源码是什么",在日常操作中,相信很多人在Connection.java的源码是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对
千家信息网最后更新 2025年01月31日Connection.java的源码是什么

这篇文章主要介绍"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 getMultiBulkReply() {//获取响应        pipelinedCommands--;        return (List) protocol.read(inputStream);    }    @SuppressWarnings("unchecked")    public List getObjectMultiBulkReply() {//获取响应        pipelinedCommands--;        return (List) protocol.read(inputStream);    }    public List getAll() {//批量获取响应        List all = new ArrayList();        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的源码是什么"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0