Commit a8e0c944cc0d5f84135793fc96f8585cb7c6b046

Authored by 王通
1 parent 99c1870f

1.解决路单批量保存时可能失败的情况

src/main/java/com/bsth/data/schedule/thread/SchedulePstThread.java
1   -package com.bsth.data.schedule.thread;
2   -
3   -import com.bsth.data.schedule.DayOfSchedule;
4   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
5   -import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
6   -import org.slf4j.Logger;
7   -import org.slf4j.LoggerFactory;
8   -import org.springframework.beans.factory.annotation.Autowired;
9   -import org.springframework.jdbc.core.BatchPreparedStatementSetter;
10   -import org.springframework.jdbc.core.JdbcTemplate;
11   -import org.springframework.jdbc.datasource.DataSourceTransactionManager;
12   -import org.springframework.stereotype.Component;
13   -import org.springframework.transaction.TransactionDefinition;
14   -import org.springframework.transaction.TransactionStatus;
15   -import org.springframework.transaction.support.DefaultTransactionDefinition;
16   -
17   -import java.sql.PreparedStatement;
18   -import java.sql.SQLException;
19   -import java.util.ArrayList;
20   -import java.util.List;
21   -
22   -/**
23   - * @author PanZhao
24   - * @ClassName: SchedulePstThread
25   - * @Description: TODO(班次异步持久化)
26   - * @date 2016年8月24日 上午1:47:05
27   - */
28   -@Component
29   -public class SchedulePstThread extends Thread {
30   -
31   - @Autowired
32   - ScheduleRealInfoRepository scheduleRepository;
33   -
34   - @Autowired
35   - JdbcTemplate jdbcTemplate;
36   -
37   - @Autowired
38   - DayOfSchedule dayOfSchedule;
39   -
40   - Logger logger = LoggerFactory.getLogger(this.getClass());
41   -
42   - static List<ScheduleRealInfo> saveList = new ArrayList<>();
43   -
44   - @Override
45   - public void run() {
46   -
47   - try{
48   - ScheduleRealInfo schedule;
49   - for (int i = 0; i < 500; i++) {
50   - schedule = DayOfSchedule.pstBuffer.poll();
51   - if (null == schedule)
52   - break;
53   -
54   - if (schedule.isDeleted()) {
55   - logger.error("save 发现 deleted=true 的班次,id: " + schedule.getId());
56   - continue;
57   - }
58   -
59   - saveList.add(schedule);
60   - }
61   -
62   - //写入数据库
63   - save();
64   - }catch (Exception e){
65   - logger.error("", e);
66   - }
67   - }
68   -
69   - private void save(){
70   - if(saveList.size() == 0)
71   - return;
72   - //记录同步数据
73   - logger.info("real schedule update size: " + saveList.size());
74   -
75   - //批量入库
76   - update2Db();
77   -
78   - //清空容器
79   - saveList.clear();
80   - logger.info("update end! ");
81   - }
82   -
83   - private void update2Db(){
84   - final List<ScheduleRealInfo> pstList = saveList;
85   - //编程式事务
86   - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
87   - DefaultTransactionDefinition def = new DefaultTransactionDefinition();
88   - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
89   - TransactionStatus status = tran.getTransaction(def);
90   -
91   - try{
92   - //更新
93   - jdbcTemplate.batchUpdate("update bsth_c_s_sp_info_real set bc_type=?,bcs=?,bcsj=?,cl_zbh=?,create_date=?" +
94   - ",dfsj=?,directive_state=?,fcno=?,fcsj=?,fcsj_actual=?,j_gh=?,j_name=?,jhlc=?,lp_name=?,qdz_code=?" +
95   - ",qdz_name=?,real_exec_date=?,remarks=?,s_gh=?,s_name=?,schedule_date=?,schedule_date_str=?,sflj=?" +
96   - ",sp_id=?,status=?,update_date=?,xl_bm=?,xl_dir=?,xl_name=?,zdsj=?,zdsj_actual=?,zdz_code=?,zdz_name=?" +
97   - ",ccno=?,df_auto=?,fgs_bm=?,fgs_name=?,gs_bm=?,gs_name=?,online=?,adjust_exps=?,reissue=?,jhlc_orig=?" +
98   - ",sigin_compate=?,drift_status=?,cc_service=?,major_station_name=? where id=?", new BatchPreparedStatementSetter() {
99   - @Override
100   - public void setValues(PreparedStatement ps, int i) throws SQLException {
101   - ScheduleRealInfo sch = pstList.get(i);
102   - ps.setString(1, sch.getBcType());
103   - ps.setInt(2, sch.getBcs()==null?0:sch.getBcs());
104   - ps.setInt(3, sch.getBcsj()==null?0:sch.getBcsj());
105   - ps.setString(4, sch.getClZbh());
106   - ps.setTimestamp(5, new java.sql.Timestamp(sch.getCreateDate().getTime()));
107   - ps.setString(6, sch.getDfsj());
108   - ps.setInt(7, sch.getDirectiveState());
109   - ps.setInt(8, sch.getFcno()==null?0:sch.getFcno());
110   - ps.setString(9, sch.getFcsj());
111   - ps.setString(10, sch.getFcsjActual());
112   - ps.setString(11, sch.getjGh());
113   - ps.setString(12, sch.getjName());
114   - ps.setDouble(13, sch.getJhlc());
115   - ps.setString(14, sch.getLpName());
116   - ps.setString(15, sch.getQdzCode());
117   - ps.setString(16, sch.getQdzName());
118   - ps.setString(17, sch.getRealExecDate());
119   - ps.setString(18, sch.getRemarks());
120   - ps.setString(19, sch.getsGh());
121   - ps.setString(20, sch.getsName());
122   - ps.setTimestamp(21, new java.sql.Timestamp(sch.getScheduleDate().getTime()));
123   - ps.setString(22, sch.getScheduleDateStr());
124   - ps.setBoolean(23, sch.isSflj());
125   - ps.setLong(24, sch.getSpId());
126   - ps.setInt(25, sch.getStatus());
127   - ps.setTimestamp(26, new java.sql.Timestamp(sch.getUpdateDate().getTime()));
128   - ps.setString(27, sch.getXlBm());
129   - ps.setString(28, sch.getXlDir());
130   - ps.setString(29, sch.getXlName());
131   - ps.setString(30, sch.getZdsj());
132   - ps.setString(31, sch.getZdsjActual());
133   - ps.setString(32, sch.getZdzCode());
134   - ps.setString(33, sch.getZdzName());
135   - ps.setInt(34, sch.getCcno()==null?0:sch.getCcno());
136   - ps.setBoolean(35, sch.isDfAuto());
137   - ps.setString(36, sch.getFgsBm());
138   - ps.setString(37, sch.getFgsName());
139   - ps.setString(38, sch.getGsBm());
140   - ps.setString(39, sch.getGsName());
141   - ps.setBoolean(40, sch.isOnline());
142   - ps.setString(41, sch.getAdjustExps());
143   - ps.setBoolean(42, sch.isReissue());
144   - ps.setDouble(43, sch.getJhlcOrig()==null?0:sch.getJhlcOrig());
145   - ps.setInt(44, sch.getSiginCompate());
146   - ps.setInt(45, sch.getDriftStatus());
147   - ps.setBoolean(46, sch.isCcService());
148   - ps.setString(47, sch.getMajorStationName());
149   -
150   - ps.setLong(48, sch.getId());
151   - }
152   -
153   - @Override
154   - public int getBatchSize() {
155   - return pstList.size();
156   - }
157   - });
158   -
159   - tran.commit(status);
160   - }catch (Exception e){
161   - tran.rollback(status);
162   - logger.error("同步数据库失败," , e);
163   - }
164   - }
165   -}
  1 +package com.bsth.data.schedule.thread;
  2 +
  3 +import com.bsth.data.schedule.DayOfSchedule;
  4 +import com.bsth.email.SendEmailController;
  5 +import com.bsth.email.entity.EmailBean;
  6 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  7 +import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
  8 +import com.bsth.util.IpUtils;
  9 +import com.fasterxml.jackson.core.JsonProcessingException;
  10 +import com.fasterxml.jackson.databind.ObjectMapper;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.beans.factory.annotation.Value;
  15 +import org.springframework.jdbc.core.BatchPreparedStatementSetter;
  16 +import org.springframework.jdbc.core.JdbcTemplate;
  17 +import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  18 +import org.springframework.stereotype.Component;
  19 +import org.springframework.transaction.TransactionDefinition;
  20 +import org.springframework.transaction.TransactionStatus;
  21 +import org.springframework.transaction.support.DefaultTransactionDefinition;
  22 +
  23 +import java.sql.PreparedStatement;
  24 +import java.sql.SQLException;
  25 +import java.util.ArrayList;
  26 +import java.util.List;
  27 +
  28 +/**
  29 + * @author PanZhao
  30 + * @ClassName: SchedulePstThread
  31 + * @Description: TODO(班次异步持久化)
  32 + * @date 2016年8月24日 上午1:47:05
  33 + */
  34 +@Component
  35 +public class SchedulePstThread extends Thread {
  36 +
  37 + @Autowired
  38 + ScheduleRealInfoRepository scheduleRepository;
  39 +
  40 + @Autowired
  41 + JdbcTemplate jdbcTemplate;
  42 +
  43 + @Autowired
  44 + DayOfSchedule dayOfSchedule;
  45 +
  46 + @Autowired
  47 + private ObjectMapper mapper;
  48 +
  49 + @Autowired
  50 + private SendEmailController sendEmailController;
  51 +
  52 + @Value("${waybill.emails}")
  53 + private String[] emails;
  54 +
  55 + Logger logger = LoggerFactory.getLogger(this.getClass());
  56 +
  57 + static List<ScheduleRealInfo> saveList = new ArrayList<>();
  58 +
  59 + @Override
  60 + public void run() {
  61 +
  62 + try{
  63 + ScheduleRealInfo schedule;
  64 + for (int i = 0; i < 500; i++) {
  65 + schedule = DayOfSchedule.pstBuffer.poll();
  66 + if (null == schedule)
  67 + break;
  68 +
  69 + if (schedule.isDeleted()) {
  70 + logger.error("save 发现 deleted=true 的班次,id: " + schedule.getId());
  71 + continue;
  72 + }
  73 +
  74 + saveList.add(schedule);
  75 + }
  76 +
  77 + //写入数据库
  78 + save();
  79 + }catch (Exception e){
  80 + logger.error("", e);
  81 + }
  82 + }
  83 +
  84 + private void save(){
  85 + if(saveList.size() == 0)
  86 + return;
  87 + //记录同步数据
  88 + logger.info("real schedule update size: " + saveList.size());
  89 +
  90 + //批量入库
  91 + update2Db();
  92 +
  93 + //清空容器
  94 + saveList.clear();
  95 + logger.info("update end! ");
  96 + }
  97 +
  98 + private void update2Db(){
  99 + final List<ScheduleRealInfo> pstList = saveList;
  100 + //编程式事务
  101 + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
  102 + DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  103 + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  104 + TransactionStatus status = tran.getTransaction(def);
  105 +
  106 + try{
  107 + //更新
  108 + jdbcTemplate.batchUpdate("update bsth_c_s_sp_info_real set bc_type=?,bcs=?,bcsj=?,cl_zbh=?,create_date=?" +
  109 + ",dfsj=?,directive_state=?,fcno=?,fcsj=?,fcsj_actual=?,j_gh=?,j_name=?,jhlc=?,lp_name=?,qdz_code=?" +
  110 + ",qdz_name=?,real_exec_date=?,remarks=?,s_gh=?,s_name=?,schedule_date=?,schedule_date_str=?,sflj=?" +
  111 + ",sp_id=?,status=?,update_date=?,xl_bm=?,xl_dir=?,xl_name=?,zdsj=?,zdsj_actual=?,zdz_code=?,zdz_name=?" +
  112 + ",ccno=?,df_auto=?,fgs_bm=?,fgs_name=?,gs_bm=?,gs_name=?,online=?,adjust_exps=?,reissue=?,jhlc_orig=?" +
  113 + ",sigin_compate=?,drift_status=?,cc_service=?,major_station_name=? where id=?", new BatchPreparedStatementSetter() {
  114 + @Override
  115 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  116 + ScheduleRealInfo sch = pstList.get(i);
  117 + ps.setString(1, sch.getBcType());
  118 + ps.setInt(2, sch.getBcs()==null?0:sch.getBcs());
  119 + ps.setInt(3, sch.getBcsj()==null?0:sch.getBcsj());
  120 + ps.setString(4, sch.getClZbh());
  121 + ps.setTimestamp(5, new java.sql.Timestamp(sch.getCreateDate().getTime()));
  122 + ps.setString(6, sch.getDfsj());
  123 + ps.setInt(7, sch.getDirectiveState());
  124 + ps.setInt(8, sch.getFcno()==null?0:sch.getFcno());
  125 + ps.setString(9, sch.getFcsj());
  126 + ps.setString(10, sch.getFcsjActual());
  127 + ps.setString(11, sch.getjGh());
  128 + ps.setString(12, sch.getjName());
  129 + ps.setDouble(13, sch.getJhlc());
  130 + ps.setString(14, sch.getLpName());
  131 + ps.setString(15, sch.getQdzCode());
  132 + ps.setString(16, sch.getQdzName());
  133 + ps.setString(17, sch.getRealExecDate());
  134 + ps.setString(18, sch.getRemarks());
  135 + ps.setString(19, sch.getsGh());
  136 + ps.setString(20, sch.getsName());
  137 + ps.setTimestamp(21, new java.sql.Timestamp(sch.getScheduleDate().getTime()));
  138 + ps.setString(22, sch.getScheduleDateStr());
  139 + ps.setBoolean(23, sch.isSflj());
  140 + ps.setLong(24, sch.getSpId());
  141 + ps.setInt(25, sch.getStatus());
  142 + ps.setTimestamp(26, new java.sql.Timestamp(sch.getUpdateDate().getTime()));
  143 + ps.setString(27, sch.getXlBm());
  144 + ps.setString(28, sch.getXlDir());
  145 + ps.setString(29, sch.getXlName());
  146 + ps.setString(30, sch.getZdsj());
  147 + ps.setString(31, sch.getZdsjActual());
  148 + ps.setString(32, sch.getZdzCode());
  149 + ps.setString(33, sch.getZdzName());
  150 + ps.setInt(34, sch.getCcno()==null?0:sch.getCcno());
  151 + ps.setBoolean(35, sch.isDfAuto());
  152 + ps.setString(36, sch.getFgsBm());
  153 + ps.setString(37, sch.getFgsName());
  154 + ps.setString(38, sch.getGsBm());
  155 + ps.setString(39, sch.getGsName());
  156 + ps.setBoolean(40, sch.isOnline());
  157 + ps.setString(41, sch.getAdjustExps());
  158 + ps.setBoolean(42, sch.isReissue());
  159 + ps.setDouble(43, sch.getJhlcOrig()==null?0:sch.getJhlcOrig());
  160 + ps.setInt(44, sch.getSiginCompate());
  161 + ps.setInt(45, sch.getDriftStatus());
  162 + ps.setBoolean(46, sch.isCcService());
  163 + ps.setString(47, sch.getMajorStationName());
  164 +
  165 + ps.setLong(48, sch.getId());
  166 + }
  167 +
  168 + @Override
  169 + public int getBatchSize() {
  170 + return pstList.size();
  171 + }
  172 + });
  173 +
  174 + tran.commit(status);
  175 + }catch (Exception e){
  176 + tran.rollback(status);
  177 + DayOfSchedule.pstBuffer.addAll(pstList);
  178 + try {
  179 + logger.error(String.format("实际排班批量保存异常: %s", mapper.writeValueAsString(pstList)), e);
  180 + } catch (JsonProcessingException jsonProcessingException) {
  181 + jsonProcessingException.printStackTrace();
  182 + }
  183 + try {
  184 + //发送邮件
  185 + EmailBean mail = new EmailBean();
  186 + mail.setSubject("路单批量保存");
  187 + mail.setContent(IpUtils.getLocalIpAddress() + "路单批量保存异常,检查日志信息<br/>");
  188 + sendEmailController.sendMail(emails[0], mail);
  189 + logger.info("DataHandlerProcess:邮件发送成功!");
  190 + } catch (Exception exception){
  191 + logger.error("DataHandlerProcess:邮件发送失败!", exception);
  192 + }
  193 + }
  194 + }
  195 +}
... ...