Compare commits
No commits in common. "c8c4a0d4c1af048af5c5215e34cdf93fb7f15a9c" and "0ad4d2b3e35cc9572c5576295b16531b8ecc17af" have entirely different histories.
c8c4a0d4c1
...
0ad4d2b3e3
@ -24,8 +24,8 @@ public enum S7Client {
|
|||||||
//TODO 步骤1 这里是配置多PLC 的,,,有多个plc 就在这里配置一个枚举类
|
//TODO 步骤1 这里是配置多PLC 的,,,有多个plc 就在这里配置一个枚举类
|
||||||
//1500 西门子200smart、1200、1500默认的 机架号=0 槽位号=1; 300/400 默认的 机架-0 插槽-2
|
//1500 西门子200smart、1200、1500默认的 机架号=0 槽位号=1; 300/400 默认的 机架-0 插槽-2
|
||||||
// S7_1200("192.168.0.52",0,1,1,PlcVarActual.HeartBeatFor1200),
|
// S7_1200("192.168.0.52",0,1,1,PlcVarActual.HeartBeatFor1200),
|
||||||
S7_15001("192.168.0.51",0,1,1),
|
S7_15001("192.168.0.51",0,1,1,PlcVarActual.HeartBeat),
|
||||||
S7_1500("192.168.0.51",0,1,1),
|
S7_1500("192.168.0.51",0,1,1,PlcVarActual.HeartBeat),
|
||||||
//1500 机架-0 插槽-1
|
//1500 机架-0 插槽-1
|
||||||
//后续 在这里扩展 多PLC应用。
|
//后续 在这里扩展 多PLC应用。
|
||||||
;
|
;
|
||||||
@ -49,7 +49,7 @@ public enum S7Client {
|
|||||||
|
|
||||||
|
|
||||||
//coreSize 是线程池的数量
|
//coreSize 是线程池的数量
|
||||||
S7Client(String host, Integer rack, Integer slot, Integer coreSize){
|
S7Client(String host, Integer rack, Integer slot, Integer coreSize, PlcVarActual heartBeat){
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.rack = rack;
|
this.rack = rack;
|
||||||
this.slot = slot;
|
this.slot = slot;
|
||||||
|
39
src/main/java/com/qgs/dc/s7/retrydemo/RetryDemoTask.java
Normal file
39
src/main/java/com/qgs/dc/s7/retrydemo/RetryDemoTask.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
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,抛出远程访问异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
58
src/main/java/com/qgs/dc/s7/retrydemo/RetryMain.java
Normal file
58
src/main/java/com/qgs/dc/s7/retrydemo/RetryMain.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
src/main/java/com/qgs/dc/s7/retrydemo/TestMain.java
Normal file
20
src/main/java/com/qgs/dc/s7/retrydemo/TestMain.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
49
src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java
Normal file
49
src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user