From c8c4a0d4c1af048af5c5215e34cdf93fb7f15a9c Mon Sep 17 00:00:00 2001 From: caixiang <939387484@qq.com> Date: Tue, 3 Jan 2023 16:32:30 +0800 Subject: [PATCH] =?UTF-8?q?S7=E5=8D=8F=E8=AE=AE=20=E5=A4=A7=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qgs/dc/s7/retrydemo/RetryDemoTask.java | 39 ------ .../com/qgs/dc/s7/retrydemo/RetryMain.java | 58 --------- .../retrydemo/SpringS7RetryTemplateTest.java | 66 ---------- .../com/qgs/dc/s7/retrydemo/TestMain.java | 20 --- .../s7/retrydemo/forTest/InitialS7Thread.java | 114 ------------------ .../qgs/dc/s7/retrydemo/lock/LockDemo.java | 49 -------- 6 files changed, 346 deletions(-) delete mode 100644 src/main/java/com/qgs/dc/s7/retrydemo/RetryDemoTask.java delete mode 100644 src/main/java/com/qgs/dc/s7/retrydemo/RetryMain.java delete mode 100644 src/main/java/com/qgs/dc/s7/retrydemo/SpringS7RetryTemplateTest.java delete mode 100644 src/main/java/com/qgs/dc/s7/retrydemo/TestMain.java delete mode 100644 src/main/java/com/qgs/dc/s7/retrydemo/forTest/InitialS7Thread.java delete mode 100644 src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/RetryDemoTask.java b/src/main/java/com/qgs/dc/s7/retrydemo/RetryDemoTask.java deleted file mode 100644 index a6896de..0000000 --- a/src/main/java/com/qgs/dc/s7/retrydemo/RetryDemoTask.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.qgs.dc.s7.retrydemo; - - -import org.apache.commons.lang3.RandomUtils; -import org.springframework.remoting.RemoteAccessException; - -/** - * @Author: cx - * @Description: - */ -public class RetryDemoTask { - /** - * 重试方法 - * @return - */ - public static boolean retryTask(String param) { - System.out.println("retry-task : 收到请求参数:"+param); - - int i = RandomUtils.nextInt(0,11); - System.out.println("retry-task : 随机生成的数:"+i); - if (i == 0) { - System.out.println("retry-task : 为0,抛出参数异常."); - //因为Illeague这个异常我们没有在exceptionMap里面配置,所以 抛出这个异常后 - //spring-retry不会进行重试,而是会直接进入recovery函数 - throw new IllegalArgumentException("retry-task : 参数异常"); - }else if (i == 1){ - System.out.println("retry-task : 为1,返回true."); - return true; - }else if (i == 2){ - System.out.println("retry-task : 为2,返回false."); - return false; - }else{ - //因为RemoteAccessExcep这个异常我们在exceptionMap里面配置了,所以 抛出这个异常后 - //spring-retry会进行重试 - System.out.println("retry-task : 大于2,抛出自定义异常."); - throw new RemoteAccessException("retry-task : 大于2,抛出远程访问异常"); - } - } -} diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/RetryMain.java b/src/main/java/com/qgs/dc/s7/retrydemo/RetryMain.java deleted file mode 100644 index 5f6cfbc..0000000 --- a/src/main/java/com/qgs/dc/s7/retrydemo/RetryMain.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.qgs.dc.s7.retrydemo; - -import org.springframework.remoting.RemoteAccessException; -import org.springframework.retry.backoff.FixedBackOffPolicy; -import org.springframework.retry.policy.SimpleRetryPolicy; -import org.springframework.retry.support.RetryTemplate; - -import java.util.HashMap; -import java.util.Map; - -public class RetryMain { - public static void main(String[] args) { - - /** - * 重试间隔时间ms,默认1000ms - * */ - long fixedPeriodTime = 1000L; - /** - * 最大重试次数,默认为3 - */ - int maxRetryTimes = 3; - /** - * 表示哪些异常需要重试,key表示异常的字节码,value为true表示需要重试 - */ - Map, Boolean> exceptionMap = new HashMap<>(); - - - exceptionMap.put(RemoteAccessException.class,true); - - // 构建重试模板实例 - RetryTemplate retryTemplate = new RetryTemplate(); - - // 设置重试回退操作策略,主要设置重试间隔时间 - FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); - backOffPolicy.setBackOffPeriod(fixedPeriodTime); - - // 设置重试策略,主要设置重试次数 - SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(maxRetryTimes, exceptionMap); - - retryTemplate.setRetryPolicy(retryPolicy); - retryTemplate.setBackOffPolicy(backOffPolicy); - - Boolean execute = retryTemplate.execute( - //RetryCallback - retryContext -> { - boolean b = RetryDemoTask.retryTask("abc"); - System.err.println("retry-main : 调用的结果:"+b+",times:"+retryContext.getRetryCount()); - return b; - }, - retryContext -> { - //RecoveryCallback - System.err.println("retry-main : 已达到最大重试次数或抛出了不重试的异常~~~"); - return false; - } - ); - System.err.println("retry-main : 执行结果:"+execute); - } -} diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/SpringS7RetryTemplateTest.java b/src/main/java/com/qgs/dc/s7/retrydemo/SpringS7RetryTemplateTest.java deleted file mode 100644 index 8c480ca..0000000 --- a/src/main/java/com/qgs/dc/s7/retrydemo/SpringS7RetryTemplateTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.qgs.dc.s7.retrydemo; - -import org.junit.Test; -import org.springframework.remoting.RemoteAccessException; -import org.springframework.retry.backoff.FixedBackOffPolicy; -import org.springframework.retry.policy.SimpleRetryPolicy; -import org.springframework.retry.support.RetryTemplate; -import java.util.HashMap; -import java.util.Map; - -/** - * @Author: zgd - * @Description: spring-retry 重试框架 - */ -public class SpringS7RetryTemplateTest { - - /** - * 重试间隔时间ms,默认1000ms - * */ - private long fixedPeriodTime = 1000L; - /** - * 最大重试次数,默认为3 - */ - private int maxRetryTimes = 3; - /** - * 表示哪些异常需要重试,key表示异常的字节码,value为true表示需要重试 - */ - private Map, Boolean> exceptionMap = new HashMap<>(); - - - @Test - public void test() { - - //文档:https://blog.csdn.net/minghao0508/article/details/123972703 - exceptionMap.put(RemoteAccessException.class,true); - - // 构建重试模板实例 - RetryTemplate retryTemplate = new RetryTemplate(); - - // 设置重试回退操作策略,主要设置重试间隔时间 - FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); - backOffPolicy.setBackOffPeriod(fixedPeriodTime); - - // 设置重试策略,主要设置重试次数 - SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(maxRetryTimes, exceptionMap); - - retryTemplate.setRetryPolicy(retryPolicy); - retryTemplate.setBackOffPolicy(backOffPolicy); - - Boolean execute = retryTemplate.execute( - //RetryCallback - retryContext -> { - boolean b = RetryDemoTask.retryTask("abc"); - System.out.println("调用的结果:"+b); - return b; - }, - retryContext -> { - //RecoveryCallback - System.out.println("已达到最大重试次数或抛出了不重试的异常~~~"); - return false; - } - ); - System.out.println("执行结果:"+execute); - } - -} diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/TestMain.java b/src/main/java/com/qgs/dc/s7/retrydemo/TestMain.java deleted file mode 100644 index fd73a10..0000000 --- a/src/main/java/com/qgs/dc/s7/retrydemo/TestMain.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.qgs.dc.s7.retrydemo; - -import java.util.ArrayList; -import java.util.List; - -public class TestMain { - public static void main(String[] args) { - Integer a = 1; - Integer a1 = 2; - Integer a2 = 3; - List list = new ArrayList(); - list.add(a); - list.add(a1); - list.add(a2); - Integer integer = list.get(0); - list.remove(integer); - System.out.println(); - - } -} diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/forTest/InitialS7Thread.java b/src/main/java/com/qgs/dc/s7/retrydemo/forTest/InitialS7Thread.java deleted file mode 100644 index e480dd2..0000000 --- a/src/main/java/com/qgs/dc/s7/retrydemo/forTest/InitialS7Thread.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.qgs.dc.s7.retrydemo.forTest; - -import com.qgs.dc.s7.my.s7connector.enmuc.PlcVarActual; -import com.qgs.dc.s7.my.s7connector.enmuc.S7Client; -import com.qgs.dc.s7.my.s7connector.exception.S7ParseDataException; -import com.qgs.dc.s7.my.s7connector.type.PlcVar; -import org.apache.commons.lang3.RandomStringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.core.annotation.Order; -import org.springframework.stereotype.Component; - -import java.util.Arrays; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - -/** - * @Desc: "" - * @Author: caixiang - * @DATE: 2022/12/23 12:33 - */ -@Component -@Order(value = 1) -public class InitialS7Thread implements ApplicationRunner { - private static final Logger logger = LoggerFactory.getLogger(InitialS7Thread.class); - - public InitialS7Thread(){ - this.executor = Executors.newScheduledThreadPool(10); - } - - private ScheduledExecutorService executor; - - private void write(S7Client s7Client, PlcVarActual var, Object newValue) { - if(var.getType().equals(PlcVar.STRING_Array)){ - String[] s = (String[])newValue; - String[] ss = (String[])newValue; - if(s.length > var.getLength() ){ - ss = new String[var.getLength()]; - for(int i=0;i< var.getLength();i++){ - ss[i] = s[i]; - } - } - s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),ss); - }else { - s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),newValue); - } - } - private Object read(S7Client s7Client, PlcVarActual var) { - return s7Client.read(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getLength(), var.getStrSize(), var.getType()); - } - - @Override - public void run(ApplicationArguments args) throws Exception { - - executor.execute(new Runnable() { - @Override - public void run() { - while (true){ - //read one - logger.info(Thread.currentThread().getName()+" , subId : "+Arrays.toString((String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays))); - try { - Thread.sleep(300); - } catch (InterruptedException e) { - throw new S7ParseDataException(e); - } - - - //write one - String[] toWrite = new String[60]; - for(int i=0;i<60;i++){ - toWrite[i] = RandomStringUtils.randomAlphanumeric(18); - - } - try { - write(S7Client.S7_1500,PlcVarActual.SubIdArrays,toWrite); - } catch (Exception e) { - throw new S7ParseDataException(e); - } - - } - } - }); - executor.execute(new Runnable() { - @Override - public void run() { - while (true){ - logger.info(Thread.currentThread().getName()+" , subId : "+Arrays.toString((String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays))); - - try { - Thread.sleep(300); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - - //write one - String[] toWrite = new String[60]; - for(int i=0;i<60;i++){ - toWrite[i] = RandomStringUtils.randomAlphanumeric(18); - - } - try { - write(S7Client.S7_15001,PlcVarActual.SubIdArrays,toWrite); - } catch (Exception e) { - throw new S7ParseDataException(e); - } - } - } - }); - - - } -} diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java b/src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java deleted file mode 100644 index 9e6a1dd..0000000 --- a/src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.qgs.dc.s7.retrydemo.lock; - -/** - * @Desc: "" - * @Author: caixiang - * @DATE: 2022/12/23 12:08 - */ -public class LockDemo { - - //对象锁 - public synchronized void func1() { - System.out.println("func1!"); - try { - Thread.sleep(10000); - System.out.println("sleep time over!"); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - public synchronized void func2() { - System.out.println("func2!"); - } - - public static void main(String[] args) { - - LockDemo test2 = new LockDemo(); - Thread t1 = new Thread(() -> { -// test.func1(); -// try { -// S7ClientNew.S7_1500.retry1(); -// } catch (InterruptedException e) { -// throw new RuntimeException(e); -// } - }, "t1"); - - Thread t2 = new Thread(() -> { -// test2.func2(); -// try { -// S7ClientNew.S7_1500.retry1(); -// } catch (InterruptedException e) { -// throw new RuntimeException(e); -// } - }, "t2"); - - t1.start(); - t2.start(); - } -}