feat: 增加ttl 使用实例
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>JavaBasiceDemo</artifactId>
|
||||
<groupId>com.wyl.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>Socket</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
<version>2.6.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.wyl.socket.client;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* 客户端
|
||||
*
|
||||
* @author 刘正
|
||||
*
|
||||
*/
|
||||
public class clienttextnet {
|
||||
public static String _pattern = "yyyy-MM-dd HH:mm:ss SSS";
|
||||
public static SimpleDateFormat format = new SimpleDateFormat(_pattern);
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
while (true) {
|
||||
// 与服务端建立连接
|
||||
Socket socket = new Socket("127.0.0.1", 9999);
|
||||
// 获得输出流,给服务端发送信息
|
||||
OutputStream dout = socket.getOutputStream();
|
||||
String str = "数据传输------";
|
||||
dout.write(str.getBytes());
|
||||
// 通过shutdownOutput高速服务器已经发送完数据,后续只能接受数据
|
||||
socket.shutdownOutput();
|
||||
|
||||
// 接收服务端发送的消息
|
||||
InputStream din = socket.getInputStream();
|
||||
byte[] outPut = new byte[4096];
|
||||
while (din.read(outPut) > 0) {
|
||||
String result = new String(outPut);
|
||||
System.out.println("服务端反回的的消息是:" + result);
|
||||
}
|
||||
din.close();
|
||||
dout.close();
|
||||
socket.close();
|
||||
Thread.sleep(3000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.wyl.socket.client.pool;
|
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* 连接池工厂
|
||||
*
|
||||
* @author admin
|
||||
*/
|
||||
public class ConnectionPoolFactory {
|
||||
|
||||
private GenericObjectPool<Socket> pool = null;
|
||||
|
||||
private static ConnectionPoolFactory instance = null;
|
||||
|
||||
public static ConnectionPoolFactory getInstance() {
|
||||
|
||||
if (instance == null) {
|
||||
synchronized (ConnectionPoolFactory.class) {
|
||||
if (instance == null) {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMaxIdle(500);
|
||||
config.setMaxWaitMillis(30000);
|
||||
config.setMinEvictableIdleTimeMillis(1800000);
|
||||
config.setMinIdle(50);
|
||||
config.setTestOnBorrow(false);
|
||||
config.setTestOnCreate(false);
|
||||
config.setTestOnReturn(false);
|
||||
config.setTestWhileIdle(true);
|
||||
config.setTimeBetweenEvictionRunsMillis(10000);
|
||||
config.setMaxTotal(1500);
|
||||
config.setNumTestsPerEvictionRun(1);
|
||||
config.setLifo(true);
|
||||
String hosts = "127.0.0.1:9999";
|
||||
instance = new ConnectionPoolFactory(config, hosts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private ConnectionPoolFactory(GenericObjectPoolConfig config, String hosts) {
|
||||
SocketConnectionFactory factory = new SocketConnectionFactory(hosts);
|
||||
pool = new GenericObjectPool<Socket>(factory, config);
|
||||
}
|
||||
|
||||
public Socket getConnection() throws Exception {
|
||||
return pool.borrowObject();
|
||||
}
|
||||
|
||||
public void releaseConnection(Socket socket) {
|
||||
try {
|
||||
pool.returnObject(socket);
|
||||
} catch (Throwable e) {
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (Exception ex) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.wyl.socket.client.pool;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.pool2.BasePooledObjectFactory;
|
||||
import org.apache.commons.pool2.PooledObject;
|
||||
import org.apache.commons.pool2.impl.DefaultPooledObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* Socket连接创建工厂
|
||||
*
|
||||
* @author admin
|
||||
*/
|
||||
@Slf4j
|
||||
public class SocketConnectionFactory extends BasePooledObjectFactory<Socket> {
|
||||
|
||||
|
||||
private List<InetSocketAddress> socketAddress = null;
|
||||
|
||||
private final AtomicLong atomicLongCount;
|
||||
|
||||
public SocketConnectionFactory(String hosts) {
|
||||
|
||||
socketAddress = new ArrayList<InetSocketAddress>();
|
||||
|
||||
String[] hostsAdd = hosts.split(";");
|
||||
if (hostsAdd.length > 0) {
|
||||
for (String tmpHost : hostsAdd) {
|
||||
String[] dataStrings = tmpHost.split(":");
|
||||
InetSocketAddress address = new InetSocketAddress(dataStrings[0], Integer.parseInt(dataStrings[1]));
|
||||
socketAddress.add(address);
|
||||
}
|
||||
}
|
||||
|
||||
atomicLongCount = new AtomicLong();
|
||||
}
|
||||
|
||||
private InetSocketAddress getSocketAddress() {
|
||||
int index = (int) (atomicLongCount.getAndIncrement() % socketAddress.size());
|
||||
log.info("调用C服务器地址:" + socketAddress.get(index).getHostName());
|
||||
return socketAddress.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyObject(PooledObject<Socket> p) throws Exception {
|
||||
Socket socket = p.getObject();
|
||||
log.info("销毁Socket:" + socket);
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateObject(PooledObject<Socket> p) {
|
||||
|
||||
Socket socket = p.getObject();
|
||||
if (socket != null) {
|
||||
if (!socket.isConnected()) {
|
||||
return false;
|
||||
}
|
||||
if (socket.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
OutputStream outputStream = socket.getOutputStream();
|
||||
outputStream.write(1);
|
||||
outputStream.flush();
|
||||
int read = socket.getInputStream().read();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket create() throws Exception {
|
||||
Socket socket = new Socket();
|
||||
socket.connect(getSocketAddress());
|
||||
return socket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PooledObject<Socket> wrap(Socket obj) {
|
||||
return new DefaultPooledObject<Socket>(obj);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.wyl.socket.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 服务端
|
||||
* @author 刘正
|
||||
*
|
||||
*/
|
||||
public class servernettext {
|
||||
public static String _pattern = "yyyy-MM-dd HH:mm:ss SSS";
|
||||
public static SimpleDateFormat format = new SimpleDateFormat(_pattern);
|
||||
// 设置超时间
|
||||
public static int _sec = 0;
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
//监听指定的端口
|
||||
ServerSocket server=new ServerSocket(9999);
|
||||
while (true) {
|
||||
//建立连接
|
||||
Socket soket=server.accept();
|
||||
System.out.println(format.format(new Date()));
|
||||
System.out.println("建立了链接\n");
|
||||
//接收客户端消息(从socket中获取输入流,并建立缓冲区进行读取)
|
||||
InputStream din=soket.getInputStream();
|
||||
System.out.println("客户端ip地址是:"+soket.getInetAddress());
|
||||
System.out.println("客户端端口号是:"+soket.getPort());
|
||||
System.out.println("本地端口号是:"+soket.getLocalPort());
|
||||
byte[] outPut=new byte[4096];
|
||||
while (din.read(outPut)>0) {
|
||||
//注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8
|
||||
String result=new String(outPut);
|
||||
System.out.println("客户端的消息是:"+result);
|
||||
}
|
||||
|
||||
//给客户端发送消息
|
||||
OutputStream dout=soket.getOutputStream();
|
||||
dout.write("已收到你发来的消息!!".getBytes());
|
||||
|
||||
din.close();
|
||||
dout.close();
|
||||
soket.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.wyl.socker.test;
|
||||
|
||||
import com.wyl.socket.client.pool.ConnectionPoolFactory;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class PoolTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
ConnectionPoolFactory instance = ConnectionPoolFactory.getInstance();
|
||||
for (int i = 0; i < 1; i++) {
|
||||
Socket connection = instance.getConnection();
|
||||
OutputStream outputStream = connection.getOutputStream();
|
||||
outputStream.write("123".getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.flush();
|
||||
}
|
||||
Thread.sleep(100000000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user