Commit bd859681faac5e9ef2707ec9d392775407185e11
1 parent
d2623603
update...
Showing
3 changed files
with
94 additions
and
12 deletions
src/main/java/com/bsth/server_rs/base_info/car/buffer/CarBufferData.java
| @@ -9,10 +9,7 @@ import org.springframework.boot.CommandLineRunner; | @@ -9,10 +9,7 @@ import org.springframework.boot.CommandLineRunner; | ||
| 9 | import org.springframework.core.annotation.Order; | 9 | import org.springframework.core.annotation.Order; |
| 10 | import org.springframework.stereotype.Component; | 10 | import org.springframework.stereotype.Component; |
| 11 | 11 | ||
| 12 | -import java.util.ArrayList; | ||
| 13 | -import java.util.HashMap; | ||
| 14 | -import java.util.List; | ||
| 15 | -import java.util.Map; | 12 | +import java.util.*; |
| 16 | import java.util.concurrent.TimeUnit; | 13 | import java.util.concurrent.TimeUnit; |
| 17 | 14 | ||
| 18 | /** | 15 | /** |
| @@ -30,7 +27,7 @@ public class CarBufferData implements CommandLineRunner { | @@ -30,7 +27,7 @@ public class CarBufferData implements CommandLineRunner { | ||
| 30 | /** | 27 | /** |
| 31 | * 待入库的bus car | 28 | * 待入库的bus car |
| 32 | */ | 29 | */ |
| 33 | - private static List<Car> pstList = new ArrayList<>(); | 30 | + public static LinkedList<Car> pstList = new LinkedList<>(); |
| 34 | 31 | ||
| 35 | @Autowired | 32 | @Autowired |
| 36 | CarRefreshThread carRefreshThread; | 33 | CarRefreshThread carRefreshThread; |
src/main/java/com/bsth/server_rs/base_info/person/buffer/PersonBufferData.java
| @@ -9,10 +9,7 @@ import org.springframework.boot.CommandLineRunner; | @@ -9,10 +9,7 @@ import org.springframework.boot.CommandLineRunner; | ||
| 9 | import org.springframework.core.annotation.Order; | 9 | import org.springframework.core.annotation.Order; |
| 10 | import org.springframework.stereotype.Component; | 10 | import org.springframework.stereotype.Component; |
| 11 | 11 | ||
| 12 | -import java.util.ArrayList; | ||
| 13 | -import java.util.HashMap; | ||
| 14 | -import java.util.List; | ||
| 15 | -import java.util.Map; | 12 | +import java.util.*; |
| 16 | import java.util.concurrent.TimeUnit; | 13 | import java.util.concurrent.TimeUnit; |
| 17 | 14 | ||
| 18 | /** | 15 | /** |
| @@ -32,7 +29,7 @@ public class PersonBufferData implements CommandLineRunner { | @@ -32,7 +29,7 @@ public class PersonBufferData implements CommandLineRunner { | ||
| 32 | /** | 29 | /** |
| 33 | * 待入库的personnel | 30 | * 待入库的personnel |
| 34 | */ | 31 | */ |
| 35 | - private static List<Personnel> pstList = new ArrayList<>(); | 32 | + public static LinkedList<Personnel> pstList = new LinkedList<>(); |
| 36 | public static List<Personnel> findAll() { | 33 | public static List<Personnel> findAll() { |
| 37 | return data; | 34 | return data; |
| 38 | } | 35 | } |
src/main/java/com/bsth/server_rs/thread/RfidCardInfoPersistenceThread.java
| 1 | package com.bsth.server_rs.thread; | 1 | package com.bsth.server_rs.thread; |
| 2 | 2 | ||
| 3 | +import com.bsth.server_rs.base_info.car.Car; | ||
| 4 | +import com.bsth.server_rs.base_info.car.buffer.CarBufferData; | ||
| 5 | +import com.bsth.server_rs.base_info.person.Personnel; | ||
| 6 | +import com.bsth.server_rs.base_info.person.buffer.PersonBufferData; | ||
| 7 | +import org.slf4j.Logger; | ||
| 8 | +import org.slf4j.LoggerFactory; | ||
| 3 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.jdbc.core.BatchPreparedStatementSetter; | ||
| 4 | import org.springframework.jdbc.core.JdbcTemplate; | 11 | import org.springframework.jdbc.core.JdbcTemplate; |
| 12 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
| 5 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
| 14 | +import org.springframework.transaction.TransactionDefinition; | ||
| 15 | +import org.springframework.transaction.TransactionStatus; | ||
| 16 | +import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
| 17 | + | ||
| 18 | +import java.sql.PreparedStatement; | ||
| 19 | +import java.sql.SQLException; | ||
| 20 | +import java.util.ArrayList; | ||
| 21 | +import java.util.List; | ||
| 6 | 22 | ||
| 7 | /** | 23 | /** |
| 8 | * 场站提交的RFID卡信息入库线程 | 24 | * 场站提交的RFID卡信息入库线程 |
| @@ -14,23 +30,95 @@ public class RfidCardInfoPersistenceThread extends Thread{ | @@ -14,23 +30,95 @@ public class RfidCardInfoPersistenceThread extends Thread{ | ||
| 14 | @Autowired | 30 | @Autowired |
| 15 | JdbcTemplate jdbcTemplate; | 31 | JdbcTemplate jdbcTemplate; |
| 16 | 32 | ||
| 33 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
| 34 | + | ||
| 17 | @Override | 35 | @Override |
| 18 | public void run() { | 36 | public void run() { |
| 19 | - personCardSave(); | ||
| 20 | - carCardSave(); | 37 | + try{ |
| 38 | + personCardSave(); | ||
| 39 | + carCardSave(); | ||
| 40 | + }catch (Exception e){ | ||
| 41 | + logger.error("", e); | ||
| 42 | + } | ||
| 21 | } | 43 | } |
| 22 | 44 | ||
| 23 | /** | 45 | /** |
| 24 | * 人卡数据入库 | 46 | * 人卡数据入库 |
| 25 | */ | 47 | */ |
| 26 | private void personCardSave(){ | 48 | private void personCardSave(){ |
| 49 | + List<Personnel> list = new ArrayList<>(); | ||
| 50 | + for(int i = 0; i < 1000; i ++){ | ||
| 51 | + list.add(PersonBufferData.pstList.poll()); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + final List<Personnel> finalList = list; | ||
| 55 | + | ||
| 56 | + //编程式事务 | ||
| 57 | + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | ||
| 58 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | ||
| 59 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | ||
| 60 | + TransactionStatus status = tran.getTransaction(def); | ||
| 61 | + try{ | ||
| 62 | + | ||
| 63 | + jdbcTemplate.update("update bsth_c_personnel set id_rfid=?, tag_rfid=? where job_code=?", new BatchPreparedStatementSetter() { | ||
| 64 | + @Override | ||
| 65 | + public void setValues(PreparedStatement ps, int i) throws SQLException { | ||
| 66 | + Personnel p = finalList.get(i); | ||
| 67 | + ps.setString(1, p.getIdRfid()); | ||
| 68 | + ps.setString(2, p.getTagRfid()); | ||
| 69 | + ps.setString(3, p.getJobCode()); | ||
| 70 | + } | ||
| 27 | 71 | ||
| 72 | + @Override | ||
| 73 | + public int getBatchSize() { | ||
| 74 | + return finalList.size(); | ||
| 75 | + } | ||
| 76 | + }); | ||
| 77 | + | ||
| 78 | + tran.commit(status); | ||
| 79 | + }catch (Exception e){ | ||
| 80 | + tran.rollback(status); | ||
| 81 | + logger.error("", e); | ||
| 82 | + } | ||
| 28 | } | 83 | } |
| 29 | 84 | ||
| 30 | /** | 85 | /** |
| 31 | * 车卡数据入库 | 86 | * 车卡数据入库 |
| 32 | */ | 87 | */ |
| 33 | private void carCardSave(){ | 88 | private void carCardSave(){ |
| 89 | + List<Car> list = new ArrayList<>(); | ||
| 90 | + for(int i = 0; i < 1000; i ++){ | ||
| 91 | + list.add(CarBufferData.pstList.poll()); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + final List<Car> finalList = list; | ||
| 95 | + | ||
| 96 | + //编程式事务 | ||
| 97 | + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | ||
| 98 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | ||
| 99 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | ||
| 100 | + TransactionStatus status = tran.getTransaction(def); | ||
| 101 | + try{ | ||
| 102 | + | ||
| 103 | + jdbcTemplate.update("update bsth_c_cars set id_rfid=?, tag_rfid=? where nbbm=?", new BatchPreparedStatementSetter() { | ||
| 104 | + @Override | ||
| 105 | + public void setValues(PreparedStatement ps, int i) throws SQLException { | ||
| 106 | + Car c = finalList.get(i); | ||
| 107 | + ps.setString(1, c.getIdRfid()); | ||
| 108 | + ps.setString(2, c.getTagRfid()); | ||
| 109 | + ps.setString(3, c.getNbbm()); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + @Override | ||
| 113 | + public int getBatchSize() { | ||
| 114 | + return finalList.size(); | ||
| 115 | + } | ||
| 116 | + }); | ||
| 34 | 117 | ||
| 118 | + tran.commit(status); | ||
| 119 | + }catch (Exception e){ | ||
| 120 | + tran.rollback(status); | ||
| 121 | + logger.error("", e); | ||
| 122 | + } | ||
| 35 | } | 123 | } |
| 36 | } | 124 | } |