Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the source code of Connection.java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the source code of Connection.java". In daily operation, I believe many people have doubts about what the source code of Connection.java is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what is the source code of Connection.java?" Next, please follow the editor to study!

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;// port private Socket socket;//socket handle private Protocol protocol = new Protocol (); / / specific Operand private RedisOutputStream outputStream / / the output stream of socket, the input stream of private RedisInputStream inputStream;//socket, private int pipelinedCommands = 0, the pipeline command counter, private int timeout = 2000, the command counter, public int getTimeout () {/ / get the socket timeout, return timeout;} public void setTimeout (int timeout) {/ / set socket timeout this.timeout = timeout } public void setTimeoutInfinite () {/ / set infinite timeout try {socket.setSoTimeout (0);} catch (SocketException ex) {throw new JedisException (ex);}} public void rollbackTimeout () {/ / rollback timeout setting try {socket.setSoTimeout (timeout) } catch (SocketException ex) {throw new JedisException (ex);}} public Connection (String host) {/ / earlier than a connection super (); this.host = host;} protected Connection sendCommand (String name, String...) Args) {try {connect (); / / 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); / send the command pipelinedCommands++;// to increase the counter return this;} public Connection (String host, int port) {/ / construct connection super (); this.host = host; this.port = port;} public String getHost () {/ / get IP return host } public void setHost (String host) {/ / set IP this.host = host;} public int getPort () {/ / get port return port;} public void setPort (int port) {/ / set port this.port = port } public Connection () {/ / construct connection} public void connect () throws UnknownHostException, IOException {if (! isConnected ()) {/ / if there is no connection, it will connect to each other and assign the local variable socket = new Socket (host, port); socket.setSoTimeout (timeout); outputStream = new RedisOutputStream (socket.getOutputStream ()) InputStream = new RedisInputStream (socket.getInputStream ());}} public void disconnect () {/ / disconnect if (isConnected ()) {try {inputStream.close (); outputStream.close (); / / close the stream if (! socket.isClosed ()) {socket.close () } / / close socket. } catch (IOException ex) {throw new JedisException (ex);} public boolean isConnected () {/ / determine whether return socket is connected! = null & & socket.isBound () & &! socket.isClosed () & & socket.isConnected () & &! socket.isInputShutdown () & &! socket.isOutputShutdown () } protected String getStatusCodeReply () {/ / get response pipelinedCommands--; return (String) protocol.read (inputStream);} public String getBulkReply () {/ / get response pipelinedCommands--; return (String) protocol.read (inputStream);} public int getIntegerReply () {/ / get response pipelinedCommands--; return ((Integer) protocol.read (inputStream)) .intValue () } @ SuppressWarnings ("unchecked") public List getMultiBulkReply () {/ / get response pipelinedCommands--; return (List) protocol.read (inputStream);} @ SuppressWarnings ("unchecked") public List getObjectMultiBulkReply () {/ / get response pipelinedCommands--; return (List) protocol.read (inputStream);} public List getAll () {/ / batch get response List all = new ArrayList () While (pipelinedCommands > 0) {all.add (protocol.read (inputStream)); pipelinedCommands--;} return all;}}

This function is very interesting.

Public boolean isConnected () {/ / determine whether return socket! = null & & socket.isBound () & &! socket.isClosed () & & socket.isConnected () & & socket.isInputShutdown () & &! socket.isOutputShutdown ();} at this point, the study of "what is the source code of Connection.java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report