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);
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,21 @@ package com.wyl.chain;
|
||||
|
||||
/**
|
||||
* 责任链设计模式
|
||||
* @ClassName: Handler
|
||||
* @Date: 2022/4/24 9:43 下午
|
||||
*
|
||||
* @author wangyl
|
||||
* @version V1.0
|
||||
* @ClassName: Handler
|
||||
* @Date: 2022/4/24 9:43 下午
|
||||
*/
|
||||
public interface Handler {
|
||||
/**
|
||||
* 处理数据的借口
|
||||
*
|
||||
* @param date
|
||||
* @return java.lang.Boolean
|
||||
* @Date 2022/4/24 9:43 下午
|
||||
* @Author wangyl
|
||||
* @Version V1.0
|
||||
*/
|
||||
Boolean handleData(String date);
|
||||
void handleData(String date);
|
||||
}
|
||||
|
||||
@@ -11,13 +11,8 @@ public class HandlerChain {
|
||||
}
|
||||
|
||||
public void handle(String data) {
|
||||
for (Handler handler : handlers){
|
||||
Boolean aBoolean = handler.handleData(data);
|
||||
if (!aBoolean){
|
||||
System.out.println("数据处理失败结束");
|
||||
break;
|
||||
}
|
||||
|
||||
for (Handler handler : handlers) {
|
||||
handler.handleData(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,24 +4,16 @@ import com.wyl.chain.Handler;
|
||||
|
||||
/**
|
||||
* 第一个处理
|
||||
* @ClassName: FirstHandler
|
||||
* @Date: 2022/4/24 9:59 下午
|
||||
*
|
||||
* @author wangyl
|
||||
* @version V1.0
|
||||
* @ClassName: FirstHandler
|
||||
* @Date: 2022/4/24 9:59 下午
|
||||
*/
|
||||
public class FirstHandler implements Handler {
|
||||
@Override
|
||||
public Boolean handleData(String date) {
|
||||
if (date.startsWith("First")) {
|
||||
System.out.println("第一处理器开始处理。。。");
|
||||
if (date.length() < 6) {
|
||||
throw new RuntimeException("数据长度不够");
|
||||
}
|
||||
System.out.println("第一处理器处理完毕。。。");
|
||||
return true;
|
||||
}
|
||||
System.out.println("无需第一处理");
|
||||
return false;
|
||||
public void handleData(String date) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,17 +17,8 @@ import java.util.concurrent.RejectedExecutionException;
|
||||
*/
|
||||
public class SecondHandler implements Handler {
|
||||
@Override
|
||||
public Boolean handleData(String date) {
|
||||
if (date.endsWith("Second")) {
|
||||
System.out.println("第二处理器开始处理。。。");
|
||||
if (date.length() < 10) {
|
||||
throw new RuntimeException("第二处理数据长度不够");
|
||||
}
|
||||
System.out.println("第二处理器处理完成。。。");
|
||||
return true;
|
||||
}
|
||||
System.out.println("无需第二处理");
|
||||
return false;
|
||||
public void handleData(String date) {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.wyl.filter;
|
||||
|
||||
public interface Filter {
|
||||
String doFilter(String text, FilterChain chain);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.wyl.filter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FilterChain {
|
||||
List<Filter> fs = new ArrayList<Filter>();
|
||||
int index = 0;
|
||||
|
||||
|
||||
public FilterChain addFilter(Filter f) {
|
||||
fs.add(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String doFilter(String text, FilterChain chain) {
|
||||
if (index == fs.size()) {
|
||||
return text;
|
||||
}
|
||||
Filter f = fs.get(index);
|
||||
index++;
|
||||
return f.doFilter(text, chain);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.wyl.filter;
|
||||
|
||||
public class HTMLFilter implements Filter {
|
||||
|
||||
|
||||
@Override
|
||||
public String doFilter(String text, FilterChain chain) {
|
||||
text = text + "HTMLFilter";
|
||||
return chain.doFilter(text, chain);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.wyl.filter;
|
||||
|
||||
public class SensitiveFilter implements Filter {
|
||||
|
||||
|
||||
@Override
|
||||
public String doFilter(String text, FilterChain chain) {
|
||||
text = text + "SensitiveFilter";
|
||||
return chain.doFilter(text, chain);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.wyl.filter;
|
||||
|
||||
public class Test {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String msg = "";
|
||||
FilterChain fc = new FilterChain();
|
||||
fc.addFilter(new HTMLFilter()).addFilter(new SensitiveFilter());
|
||||
String s = fc.doFilter(msg, fc);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package concurrent.completableFuture;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
@Slf4j
|
||||
public class completableFutureTest {
|
||||
|
||||
/**
|
||||
* 有返回值的异步任务
|
||||
*/
|
||||
@Test
|
||||
public void supplyAsyncGet() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<String> supplyAsyncFuture = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task ...");
|
||||
return "this is supplyAsync";
|
||||
});
|
||||
String s = supplyAsyncFuture.get();
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supplyAsyncGetTimeOut() throws ExecutionException, InterruptedException, TimeoutException {
|
||||
CompletableFuture<String> supplyAsyncFuture = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task ...");
|
||||
return "this is supplyAsync";
|
||||
});
|
||||
String s = supplyAsyncFuture.get(10L, TimeUnit.MILLISECONDS);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supplyAsyncJoin() {
|
||||
CompletableFuture<String> supplyAsyncFuture = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task ...");
|
||||
return "this is supplyAsync";
|
||||
});
|
||||
String s = supplyAsyncFuture.join();
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 没有返回值的异步任务
|
||||
*/
|
||||
public void runAsyncGet() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<Void> runAsyncFuture = CompletableFuture.runAsync(() -> {
|
||||
log.info("executing runAsync task ...");
|
||||
});
|
||||
runAsyncFuture.get();
|
||||
}
|
||||
|
||||
public void runAsyncGetTimeOut() throws ExecutionException, InterruptedException, TimeoutException {
|
||||
CompletableFuture<Void> runAsyncFuture = CompletableFuture.runAsync(() -> {
|
||||
log.info("executing runAsync task ...");
|
||||
});
|
||||
runAsyncFuture.get(10L, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void runAsyncJoin() {
|
||||
CompletableFuture<Void> runAsyncFuture = CompletableFuture.runAsync(() -> {
|
||||
log.info("executing runAsync task ...");
|
||||
});
|
||||
runAsyncFuture.join();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多任务并行 等待都完成
|
||||
*/
|
||||
@Test
|
||||
public void allOf() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<String> cf11 = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task cf11 ...");
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(10L);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "this is supplyAsync";
|
||||
});
|
||||
CompletableFuture<String> cf12 = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task cf12 ...");
|
||||
return "this is supplyAsync1";
|
||||
});
|
||||
CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(cf11, cf12);
|
||||
String s = cf12.get();
|
||||
String s1 = cf11.get();
|
||||
System.out.println(s);
|
||||
System.out.println(s1);
|
||||
// allOfFuture.get();
|
||||
// String s = cf12.get();
|
||||
// String s1 = cf11.get();
|
||||
// System.out.println(s);
|
||||
// System.out.println(s1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多任务并行 一个完成就返回完成的
|
||||
*/
|
||||
@Test
|
||||
public void test() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<String> cf21 = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task cf21 ...");
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1L);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "this is supplyAsync cf21";
|
||||
});
|
||||
CompletableFuture<String> cf22 = CompletableFuture.supplyAsync(() -> {
|
||||
log.info("executing supplyAsync task cf22 ...");
|
||||
return "this is supplyAsync cf22";
|
||||
});
|
||||
CompletableFuture<Object> anyOfFuture = CompletableFuture.anyOf(cf21, cf22);
|
||||
log.info("{}", anyOfFuture.get()); // 输出结果:this is supplyAsync cf21或cf22
|
||||
String join = cf21.join();
|
||||
String join1 = cf22.join();
|
||||
System.out.println(join);
|
||||
System.out.println(join1);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,6 @@ package concurrent.hashmap;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.DelayQueue;
|
||||
import java.util.concurrent.Delayed;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HashMapTest {
|
||||
static final int MAXIMUM_CAPACITY = 1 << 30;
|
||||
|
||||
@@ -2,7 +2,10 @@ package concurrent.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
@@ -11,10 +14,10 @@ public class ModelTest {
|
||||
@Test
|
||||
public void ArrayBlockingQueueModelTest() throws InterruptedException {
|
||||
ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(5000);
|
||||
for (int i = 0; i < 1000; i++){
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
new QueueProducer(arrayBlockingQueue, i).start();
|
||||
}
|
||||
for (int i = 0; i < 200000; i++){
|
||||
for (int i = 0; i < 200000; i++) {
|
||||
new QueueConsumer(arrayBlockingQueue, i).start();
|
||||
}
|
||||
|
||||
@@ -24,10 +27,10 @@ public class ModelTest {
|
||||
@Test
|
||||
public void LinkedBlockingQueueModelTest() throws InterruptedException {
|
||||
LinkedBlockingDeque<Integer> arrayBlockingQueue = new LinkedBlockingDeque<>(1000);
|
||||
for (int i = 0; i < 1000; i++){
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
new QueueProducer(arrayBlockingQueue, i).start();
|
||||
}
|
||||
for (int i = 0; i < 200000; i++){
|
||||
for (int i = 0; i < 200000; i++) {
|
||||
new QueueConsumer(arrayBlockingQueue, i).start();
|
||||
}
|
||||
|
||||
@@ -39,12 +42,50 @@ public class ModelTest {
|
||||
Object o = new Object();
|
||||
Queue queue = new LinkedList();
|
||||
Integer maxSize = 100;
|
||||
for (int i = 0; i < 15; i++){
|
||||
for (int i = 0; i < 15; i++) {
|
||||
new WaitProducer(maxSize, o, queue).start();
|
||||
}
|
||||
for (int i = 0; i < 2; i++){
|
||||
for (int i = 0; i < 2; i++) {
|
||||
new WaitConsumer(o, queue).start();
|
||||
}
|
||||
Thread.sleep(10000000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
Map<WeakReference<Integer>, WeakReference<Integer>> map = new HashMap<>(8);
|
||||
// 注意这里~
|
||||
WeakReference<Integer> key = new WeakReference<>(1);
|
||||
WeakReference<Integer> value = new WeakReference<>(2);
|
||||
map.put(key, value);
|
||||
System.out.println("put success");
|
||||
Thread.sleep(1000);
|
||||
System.gc();
|
||||
System.out.println("get " + map.get(key).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() throws InterruptedException {
|
||||
Map<WeakReference<Integer>, WeakReference<Integer>> map = new HashMap<>(8);
|
||||
WeakReference<Integer> key = new WeakReference<>(1);
|
||||
WeakReference<Integer> value = new WeakReference<>(777);
|
||||
map.put(key,value);
|
||||
System.out.println("put success");
|
||||
Thread.sleep(1000);
|
||||
System.gc();
|
||||
System.out.println("get " + map.get(key).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() throws InterruptedException {
|
||||
Map<WeakReference<Integer>, Integer> map = new HashMap<>(8);
|
||||
WeakReference<Integer> key = new WeakReference<>(1);
|
||||
Integer value = 777;
|
||||
map.put(key,value);
|
||||
System.out.println("put success");
|
||||
Thread.sleep(1000);
|
||||
System.gc();
|
||||
System.out.println("get " + map.get(key));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
package generator.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 算法实验记录表
|
||||
* @TableName abtest_experiment_algorithm
|
||||
*/
|
||||
@TableName(value ="abtest_experiment_algorithm")
|
||||
public class AbtestExperimentAlgorithm implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 参数列表,json格式的字符串,如[{key:value},]
|
||||
*/
|
||||
private String parametersList;
|
||||
|
||||
/**
|
||||
* 分流模式
|
||||
*/
|
||||
private String shuntType;
|
||||
|
||||
/**
|
||||
* 版本号
|
||||
*/
|
||||
private Integer version;
|
||||
|
||||
/**
|
||||
* abtest_experiment_group表中的id,关联实验状态信息
|
||||
*/
|
||||
private Integer abtestExperimentGroupId;
|
||||
|
||||
/**
|
||||
* 实验细节描述
|
||||
*/
|
||||
private String detailsDescription;
|
||||
|
||||
/**
|
||||
* 分流字段
|
||||
*/
|
||||
private String shuntKey;
|
||||
|
||||
/**
|
||||
* 分流比例
|
||||
*/
|
||||
private Integer shuntProportion;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String employeeName;
|
||||
|
||||
/**
|
||||
* 工号
|
||||
*/
|
||||
private String employeeWorkCode;
|
||||
|
||||
/**
|
||||
* 已删除 1 未删除 0
|
||||
*/
|
||||
private Integer deleted;
|
||||
|
||||
/**
|
||||
* etl更新时间
|
||||
*/
|
||||
private LocalDateTime beidouEtlTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数列表,json格式的字符串,如[{key:value},]
|
||||
*/
|
||||
public String getParametersList() {
|
||||
return parametersList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数列表,json格式的字符串,如[{key:value},]
|
||||
*/
|
||||
public void setParametersList(String parametersList) {
|
||||
this.parametersList = parametersList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分流模式
|
||||
*/
|
||||
public String getShuntType() {
|
||||
return shuntType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分流模式
|
||||
*/
|
||||
public void setShuntType(String shuntType) {
|
||||
this.shuntType = shuntType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 版本号
|
||||
*/
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* 版本号
|
||||
*/
|
||||
public void setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
/**
|
||||
* abtest_experiment_group表中的id,关联实验状态信息
|
||||
*/
|
||||
public Integer getAbtestExperimentGroupId() {
|
||||
return abtestExperimentGroupId;
|
||||
}
|
||||
|
||||
/**
|
||||
* abtest_experiment_group表中的id,关联实验状态信息
|
||||
*/
|
||||
public void setAbtestExperimentGroupId(Integer abtestExperimentGroupId) {
|
||||
this.abtestExperimentGroupId = abtestExperimentGroupId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实验细节描述
|
||||
*/
|
||||
public String getDetailsDescription() {
|
||||
return detailsDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实验细节描述
|
||||
*/
|
||||
public void setDetailsDescription(String detailsDescription) {
|
||||
this.detailsDescription = detailsDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分流字段
|
||||
*/
|
||||
public String getShuntKey() {
|
||||
return shuntKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分流字段
|
||||
*/
|
||||
public void setShuntKey(String shuntKey) {
|
||||
this.shuntKey = shuntKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分流比例
|
||||
*/
|
||||
public Integer getShuntProportion() {
|
||||
return shuntProportion;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分流比例
|
||||
*/
|
||||
public void setShuntProportion(Integer shuntProportion) {
|
||||
this.shuntProportion = shuntProportion;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
public LocalDateTime getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
public void setUpdateTime(LocalDateTime updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
public LocalDateTime getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
public void setCreateTime(LocalDateTime createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
public void setEmployeeName(String employeeName) {
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 工号
|
||||
*/
|
||||
public String getEmployeeWorkCode() {
|
||||
return employeeWorkCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 工号
|
||||
*/
|
||||
public void setEmployeeWorkCode(String employeeWorkCode) {
|
||||
this.employeeWorkCode = employeeWorkCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 已删除 1 未删除 0
|
||||
*/
|
||||
public Integer getDeleted() {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 已删除 1 未删除 0
|
||||
*/
|
||||
public void setDeleted(Integer deleted) {
|
||||
this.deleted = deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* etl更新时间
|
||||
*/
|
||||
public LocalDateTime getBeidouEtlTime() {
|
||||
return beidouEtlTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* etl更新时间
|
||||
*/
|
||||
public void setBeidouEtlTime(LocalDateTime beidouEtlTime) {
|
||||
this.beidouEtlTime = beidouEtlTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object that) {
|
||||
if (this == that) {
|
||||
return true;
|
||||
}
|
||||
if (that == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != that.getClass()) {
|
||||
return false;
|
||||
}
|
||||
AbtestExperimentAlgorithm other = (AbtestExperimentAlgorithm) that;
|
||||
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
|
||||
&& (this.getParametersList() == null ? other.getParametersList() == null : this.getParametersList().equals(other.getParametersList()))
|
||||
&& (this.getShuntType() == null ? other.getShuntType() == null : this.getShuntType().equals(other.getShuntType()))
|
||||
&& (this.getVersion() == null ? other.getVersion() == null : this.getVersion().equals(other.getVersion()))
|
||||
&& (this.getAbtestExperimentGroupId() == null ? other.getAbtestExperimentGroupId() == null : this.getAbtestExperimentGroupId().equals(other.getAbtestExperimentGroupId()))
|
||||
&& (this.getDetailsDescription() == null ? other.getDetailsDescription() == null : this.getDetailsDescription().equals(other.getDetailsDescription()))
|
||||
&& (this.getShuntKey() == null ? other.getShuntKey() == null : this.getShuntKey().equals(other.getShuntKey()))
|
||||
&& (this.getShuntProportion() == null ? other.getShuntProportion() == null : this.getShuntProportion().equals(other.getShuntProportion()))
|
||||
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
|
||||
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
|
||||
&& (this.getEmployeeName() == null ? other.getEmployeeName() == null : this.getEmployeeName().equals(other.getEmployeeName()))
|
||||
&& (this.getEmployeeWorkCode() == null ? other.getEmployeeWorkCode() == null : this.getEmployeeWorkCode().equals(other.getEmployeeWorkCode()))
|
||||
&& (this.getDeleted() == null ? other.getDeleted() == null : this.getDeleted().equals(other.getDeleted()))
|
||||
&& (this.getBeidouEtlTime() == null ? other.getBeidouEtlTime() == null : this.getBeidouEtlTime().equals(other.getBeidouEtlTime()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
|
||||
result = prime * result + ((getParametersList() == null) ? 0 : getParametersList().hashCode());
|
||||
result = prime * result + ((getShuntType() == null) ? 0 : getShuntType().hashCode());
|
||||
result = prime * result + ((getVersion() == null) ? 0 : getVersion().hashCode());
|
||||
result = prime * result + ((getAbtestExperimentGroupId() == null) ? 0 : getAbtestExperimentGroupId().hashCode());
|
||||
result = prime * result + ((getDetailsDescription() == null) ? 0 : getDetailsDescription().hashCode());
|
||||
result = prime * result + ((getShuntKey() == null) ? 0 : getShuntKey().hashCode());
|
||||
result = prime * result + ((getShuntProportion() == null) ? 0 : getShuntProportion().hashCode());
|
||||
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
|
||||
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
|
||||
result = prime * result + ((getEmployeeName() == null) ? 0 : getEmployeeName().hashCode());
|
||||
result = prime * result + ((getEmployeeWorkCode() == null) ? 0 : getEmployeeWorkCode().hashCode());
|
||||
result = prime * result + ((getDeleted() == null) ? 0 : getDeleted().hashCode());
|
||||
result = prime * result + ((getBeidouEtlTime() == null) ? 0 : getBeidouEtlTime().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", id=").append(id);
|
||||
sb.append(", parametersList=").append(parametersList);
|
||||
sb.append(", shuntType=").append(shuntType);
|
||||
sb.append(", version=").append(version);
|
||||
sb.append(", abtestExperimentGroupId=").append(abtestExperimentGroupId);
|
||||
sb.append(", detailsDescription=").append(detailsDescription);
|
||||
sb.append(", shuntKey=").append(shuntKey);
|
||||
sb.append(", shuntProportion=").append(shuntProportion);
|
||||
sb.append(", updateTime=").append(updateTime);
|
||||
sb.append(", createTime=").append(createTime);
|
||||
sb.append(", employeeName=").append(employeeName);
|
||||
sb.append(", employeeWorkCode=").append(employeeWorkCode);
|
||||
sb.append(", deleted=").append(deleted);
|
||||
sb.append(", beidouEtlTime=").append(beidouEtlTime);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package generator.mapper;
|
||||
|
||||
import generator.domain.AbtestExperimentAlgorithm;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author wyl
|
||||
* @description 针对表【abtest_experiment_algorithm(算法实验记录表)】的数据库操作Mapper
|
||||
* @createDate 2023-05-26 10:46:15
|
||||
* @Entity generator.domain.AbtestExperimentAlgorithm
|
||||
*/
|
||||
public interface AbtestExperimentAlgorithmMapper extends BaseMapper<AbtestExperimentAlgorithm> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package generator.service;
|
||||
|
||||
import generator.domain.AbtestExperimentAlgorithm;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author wyl
|
||||
* @description 针对表【abtest_experiment_algorithm(算法实验记录表)】的数据库操作Service
|
||||
* @createDate 2023-05-26 10:46:15
|
||||
*/
|
||||
public interface AbtestExperimentAlgorithmService extends IService<AbtestExperimentAlgorithm> {
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package generator.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import generator.domain.AbtestExperimentAlgorithm;
|
||||
import generator.service.AbtestExperimentAlgorithmService;
|
||||
import generator.mapper.AbtestExperimentAlgorithmMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author wyl
|
||||
* @description 针对表【abtest_experiment_algorithm(算法实验记录表)】的数据库操作Service实现
|
||||
* @createDate 2023-05-26 10:46:15
|
||||
*/
|
||||
@Service
|
||||
public class AbtestExperimentAlgorithmServiceImpl extends ServiceImpl<AbtestExperimentAlgorithmMapper, AbtestExperimentAlgorithm>
|
||||
implements AbtestExperimentAlgorithmService{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="generator.mapper.AbtestExperimentAlgorithmMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="generator.domain.AbtestExperimentAlgorithm">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="parametersList" column="parameters_list" jdbcType="VARCHAR"/>
|
||||
<result property="shuntType" column="shunt_type" jdbcType="VARCHAR"/>
|
||||
<result property="version" column="version" jdbcType="INTEGER"/>
|
||||
<result property="abtestExperimentGroupId" column="abtest_experiment_group_id" jdbcType="INTEGER"/>
|
||||
<result property="detailsDescription" column="details_description" jdbcType="VARCHAR"/>
|
||||
<result property="shuntKey" column="shunt_key" jdbcType="VARCHAR"/>
|
||||
<result property="shuntProportion" column="shunt_proportion" jdbcType="INTEGER"/>
|
||||
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="employeeName" column="employee_name" jdbcType="VARCHAR"/>
|
||||
<result property="employeeWorkCode" column="employee_work_code" jdbcType="VARCHAR"/>
|
||||
<result property="deleted" column="deleted" jdbcType="TINYINT"/>
|
||||
<result property="beidouEtlTime" column="beidou_etl_time" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,parameters_list,shunt_type,
|
||||
version,abtest_experiment_group_id,details_description,
|
||||
shunt_key,shunt_proportion,update_time,
|
||||
create_time,employee_name,employee_work_code,
|
||||
deleted,beidou_etl_time
|
||||
</sql>
|
||||
</mapper>
|
||||
+7
-5
@@ -12,15 +12,17 @@
|
||||
<artifactId>javacv</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>javacv-platform</artifactId>
|
||||
<version>1.5.8</version>
|
||||
<groupId>org.opencv</groupId>
|
||||
<artifactId>opencv</artifactId>
|
||||
<version>470</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/src/main/resources/opencv-470.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.wyl.javacv;
|
||||
|
||||
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfByte;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
|
||||
public class DemoApplicationTests {
|
||||
/**
|
||||
* @return
|
||||
* @Description
|
||||
* @Param
|
||||
* @Author zhangsan
|
||||
* @Date 2020.09.05 9:43
|
||||
**/
|
||||
public static void main(String[] args) throws Exception {
|
||||
// 解决awt报错问题
|
||||
// 加载动态库
|
||||
URL url = ClassLoader.getSystemResource("opencv_java470.dll");
|
||||
System.load(url.getPath());
|
||||
// 读取图像
|
||||
// Mat image = Imgcodecs.imread("D:\\1.jpg");
|
||||
File file = new File("D:/1.jpg");
|
||||
InputStream fos = new FileInputStream(file);
|
||||
Mat image = Imgcodecs.imdecode(new MatOfByte(fos.readAllBytes()), Imgcodecs.IMREAD_COLOR);
|
||||
if (image.empty()) {
|
||||
throw new Exception("image is empty");
|
||||
}
|
||||
HighGui.imshow("Original Image", image);
|
||||
|
||||
// 创建输出单通道图像
|
||||
Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1);
|
||||
// 进行图像色彩空间转换
|
||||
Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);
|
||||
|
||||
HighGui.imshow("Processed Image", grayImage);
|
||||
Imgcodecs.imwrite("D:/hello.jpg", grayImage);
|
||||
HighGui.waitKey();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.wyl.javacv;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.videoio.VideoWriter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.opencv.imgcodecs.Imgcodecs.imread;
|
||||
|
||||
public class ImageToVideoExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 图像文件夹路径
|
||||
String imageFolderPath = "/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/";
|
||||
|
||||
// 获取图像文件列表
|
||||
File[] imageFiles = new File(imageFolderPath).listFiles();
|
||||
|
||||
// 设置视频编码器参数
|
||||
int fourcc = VideoWriter.fourcc('X', 'V', 'I', 'D'); // 使用XVID编码器
|
||||
double fps = 24; // 视频帧率
|
||||
Mat image = imread(imageFiles[0].getAbsolutePath());
|
||||
Size frameSize = new Size(image.cols(), image.rows()); // 视频帧尺寸
|
||||
|
||||
// 创建视频写入器
|
||||
VideoWriter writer = new VideoWriter("/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/output.avi", fourcc, fps, frameSize, true);
|
||||
|
||||
// 逐个读取图像并写入视频
|
||||
for (File imageFile : imageFiles) {
|
||||
// 加载图像
|
||||
image = imread(imageFile.getAbsolutePath());
|
||||
|
||||
// 检查图像是否加载成功
|
||||
if (image.empty()) {
|
||||
System.out.println("无法加载图像:" + imageFile.getName());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// 写入视频
|
||||
writer.write(image);
|
||||
|
||||
// 释放内存
|
||||
image.release();
|
||||
}
|
||||
|
||||
// 释放视频写入器
|
||||
writer.release();
|
||||
|
||||
System.out.println("视频合成完成!");
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.wyl.javacv;
|
||||
|
||||
|
||||
import org.bytedeco.opencv.opencv_core.Mat;
|
||||
|
||||
import static org.bytedeco.opencv.global.opencv_highgui.imshow;
|
||||
import static org.bytedeco.opencv.global.opencv_highgui.waitKey;
|
||||
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
|
||||
import static org.bytedeco.opencv.global.opencv_imgcodecs.imwrite;
|
||||
|
||||
public class JavaCVExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 加载图像
|
||||
Mat image = imread("/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/OIP.jpg");
|
||||
|
||||
// 检查图像是否加载成功
|
||||
if (image.empty()) {
|
||||
System.out.println("无法加载图像");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// 显示图像
|
||||
// imshow("图像窗口", image);
|
||||
// waitKey(0);
|
||||
|
||||
// 保存图像
|
||||
imwrite("/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/example_output.jpg", image);
|
||||
|
||||
// 释放内存
|
||||
image.release();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
+19
@@ -0,0 +1,19 @@
|
||||
<?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>jna</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -19,6 +19,8 @@
|
||||
<module>DelayQueue</module>
|
||||
<module>javacv</module>
|
||||
<module>mqtt</module>
|
||||
<module>Socket</module>
|
||||
<module>jna</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
<properties>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<module>spring-boot-common</module>
|
||||
<module>spring-boot-rabbitmq</module>
|
||||
<module>spring-boot-mybatis</module>
|
||||
<module>spring-boot-wxrobot</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>spring-boot</artifactId>
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<groupId>com.wyl.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-boot-wxrobot</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.83</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.wyl.spring.wxrobot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IBSApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IBSApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.wyl.spring.wxrobot.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.wyl.spring.wxrobot.entity.param.WxRobotMessageParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
* @date: 2023/2/6
|
||||
* @description:
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class RobotMessage {
|
||||
|
||||
@PostMapping("/getMessageData")
|
||||
public String getMessageData(@RequestBody WxRobotMessageParam skuListPageReq) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("code", 200);
|
||||
jsonObject.put("message", "成功");
|
||||
jsonObject.put("data", "123");
|
||||
System.out.println(jsonObject.toJSONString());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.wyl.spring.wxrobot.entity.param;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
* @date: 2023/2/6
|
||||
* @description: 接收消息参数
|
||||
*/
|
||||
@Data
|
||||
public class WxRobotMessageParam {
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String text;
|
||||
ThreadLocal<String> t = new ThreadLocal<>();
|
||||
|
||||
public String getText() {
|
||||
t.set("123");
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
server.port=65006
|
||||
Reference in New Issue
Block a user