Browse Source

add:异步编程

master
zhangzhiqiang 2 years ago
parent
commit
6303079e27
  1. 5
      transactional-demo/pom.xml
  2. 19
      transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/stream流/StreamDemo.java
  3. 12
      transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/函数式接口/FuntureDemo.java
  4. 10
      transactional-demo/src/main/java/nl/org/transactionaldemo/server/事务/编程事务/TransationDemo.java

5
transactional-demo/pom.xml

@ -30,6 +30,11 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>

19
transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/stream流/StreamDemo.java

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@ -14,15 +15,27 @@ import java.util.stream.Collectors;
public class StreamDemo {
public static void main(String[] args) {
List<DataEntity> list = extracted(100);
//1.字段汇总
Set<String> collect = list.stream()
.map(DataEntity::getName).collect(Collectors.toSet());
for (DataEntity entity : list) {
String classNo = entity.getClassNo();
}
//1.字段汇总 a->a.getName() DataEntity::getName
Set<Object> collect = list.stream()
.map(new Function<DataEntity, Object>() {
@Override
public Object apply(DataEntity dataEntity) {
return dataEntity.getName();
}
}).collect(Collectors.toSet());
//2.条件过滤
List<DataEntity> collect1 = list.stream().filter(dataEntity -> dataEntity.getClassNo().equals("NO.3")).collect(Collectors.toList());
//3.分组
Map<String, List<DataEntity>> map = list.stream().collect(Collectors.groupingBy(dataEntity -> dataEntity.getClassNo()));
//4.转换
//list;->point_code = '001'
Map<String, DataEntity> map2 = list.stream().collect(HashMap::new, (k, v) -> k.put(v.getName(), v), HashMap::putAll);
DataEntity dataEntity = map2.get("e4563a6b-94c1-4817-97aa-d66a8b21640f");
System.out.println(dataEntity.toString());
}

12
transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/函数式接口/FuntureDemo.java

@ -13,21 +13,23 @@ public class FuntureDemo {
numProcess.process(3);
//2.正常业务处理:文字处理业务中需要引入数字处理业务
StringProcess process = new StringProcess();
process.process();
StringProcess sringprocess = new StringProcess();
sringprocess.process();
// NumProcess numProcess = new NumProcess();
// return "NO."+numProcess.process(3);
//3.以参数的形式带入
process.process(new NumProcess());
sringprocess.process(new NumProcess());
//4.匿名内部类
process.process(new Process() {
sringprocess.process(new Process() {
@Override
public Object process(Object o) {
return (Integer)o*2;
}
});
//5.函数式接口
process.process((a)-> ((Integer) a)*2);
sringprocess.process((a)-> ((Integer) a)*2);
}

10
transactional-demo/src/main/java/nl/org/transactionaldemo/server/事务/编程事务/TransationDemo.java

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.CollectionUtils;
import java.util.*;
@ -28,7 +29,7 @@ public class TransationDemo {
@Autowired
private PlatformTransactionManager txManager;
// @Transactional
@Transactional
public void creteTask(JSONArray request){
List<TransactionStatus> statusCollent = new ArrayList<>();
@ -71,7 +72,7 @@ public class TransationDemo {
}
}
//封装参考
class TransactionManagerUtil{
public static void main(String[] args) {
@ -96,6 +97,10 @@ class TransactionManagerUtil{
PlatformTransactionManager txManager = null; //SpringContextHolder.getBean(PlatformTransactionManager.class);
Map<String, TransactionStatus> statusMap = new HashMap<>();
List<TransactionStatus> errTask;
DefaultTransactionDefinition init = new DefaultTransactionDefinition();
init.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus initStatus = txManager.getTransaction(init);
try {
//创建所有事务集合
Consumer<String> allTransactionConsumer = task -> {
@ -122,6 +127,7 @@ class TransactionManagerUtil{
txManager.commit(value);
}
}
txManager.commit(initStatus);
}
return null;
}

Loading…
Cancel
Save