S7协议 大版本合并
这个提交包含在:
父节点
33bc99c697
当前提交
c8c4a0d4c1
@ -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,抛出远程访问异常");
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Class<? extends Throwable>, 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);
|
||||
}
|
||||
}
|
@ -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<Class<? extends Throwable>, 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Integer> list = new ArrayList<Integer>();
|
||||
list.add(a);
|
||||
list.add(a1);
|
||||
list.add(a2);
|
||||
Integer integer = list.get(0);
|
||||
list.remove(integer);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
正在加载...
在新工单中引用
屏蔽一个用户