Java tutorial
package com.hx.sync.service; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.alibaba.fastjson.JSONObject; import com.hx.sync.db.DatabaseUtil; import com.hx.sync.utils.Constants; import com.hx.sync.utils.DateUtil; import com.hx.sync.utils.MemberUtils; import com.hx.sync.utils.ThreadPool.IThreadCallBack; import com.hx.sync.vo.TmApplicationNodes; import com.hx.sync.vo.TmOvLog; import com.hx.sync.vo.TmOwner; import com.hx.sync.vo.TmVehicle; import com.hx.sync.vo.TmVehicleId; public class ThreadCallBackTask implements IThreadCallBack { private static final Log logger = LogFactory.getLog("ThreadCallBackTask"); @Override public void onTask(Object _objTask) { // TODO Auto-generated method stub TmApplicationNodes node = (TmApplicationNodes) _objTask; if (node != null) { Date beginDate = new Date(); //System.out.println("?...?" + node.getNodeName() + ","+beginDate); logger.info("?...?" + node.getNodeName() + "," + beginDate); DatabaseUtil dest = new DatabaseUtil(Constants.DB_DRIVER_MYSQL, node.getDatabaseUrl(), node.getDatabaseUser(), node.getDatabasePwd()); int start = 1; int size = 200; int registFailCount = 0; String logInfo = ""; try { while (true) { List<TmOwner> toList = this.getRegistMember(dest, start, size); if (toList == null || toList.size() == 0) { break; } for (TmOwner to : toList) { //dest.setAutoCommit(false); TmOvLog ownLog = new TmOvLog(); ownLog.setDataNode(node.getNodeName()); ownLog.setEntityCode(to.getEntityCode()); ownLog.setSyncType(Constants.SYNC_OWNER); //ownLog.setLogTime(DateUtil.getCurrentTimestamp()); logInfo = "???" + node.getNodeName() + "entityCode=" + to.getEntityCode() + ",ownerNo=" + to.getOwnerNo() + ",mobile=" + to.getMobile(); String ownKeyData = "entityCode=" + to.getEntityCode() + ",ownerNo=" + to.getOwnerNo() + ",mobile=" + to.getMobile(); // ? JSONObject memberJson = MemberUtils.saveMember(to, true); String memberNo = null; if (memberJson != null && memberJson.containsKey("code") && (memberJson.getInteger("code") == 200 || memberJson.getInteger("code") == 402) && memberJson.containsKey("data") && memberJson.getJSONObject("data") != null) { JSONObject returnData = memberJson.getJSONObject("data"); memberNo = returnData.getString("user_guid"); } ownKeyData += ",memberNo=" + memberNo; ownLog.setKeyData(ownKeyData); if (memberNo != null && !memberNo.equals("")) { ownLog.setSyncStatus(Constants.SYNC_SUCCESS); // ??? logInfo += ",memberNo=" + memberNo + ",??"; updateTmOwnerInfo(dest, to, memberNo); to.setMemberNo(memberNo); // ??? List<TmVehicle> vehs = this.getVehicleByOwnerNo(dest, to.getEntityCode(), to.getOwnerNo()); for (TmVehicle veh : vehs) { this.updateTmVehicleInfo(dest, veh, memberNo); // ?? /* String vehKeyData = "entityCode="+to.getEntityCode()+",ownerNo="+to.getOwnerNo()+",mobile="+to.getMobile()+",vin="+veh.getVin()+",license="+veh.getLicense(); TmOvLog vehLog = new TmOvLog(); vehLog.setDataNode(node.getNodeName()); vehLog.setEntityCode(to.getEntityCode()); vehLog.setSyncType(Constants.SYNC_VEHICLE); vehLog.setKeyData(vehKeyData); TmVehicleId id = new TmVehicleId(veh.getVin(),veh.getEntityCode()); veh.setId(id); veh.setMemberNo(memberNo); veh.setUserGuid(memberNo); JSONObject json = MemberUtils.sendVehicleTMemberCenter(veh); String vehLogInfo = "???"+node.getNodeName()+"entityCode="+to.getEntityCode()+",ownerNo="+to.getOwnerNo()+",vin="+veh.getVin(); if(json!=null && json.containsKey("code") && (json.getInteger("code")==200 ||json.getInteger("code")==402 )){ vehLogInfo += "???"; this.updateTmVehicleInfo(dest, veh, memberNo); vehLog.setSyncStatus(Constants.SYNC_SUCCESS); }else{ vehLog.setSyncStatus(Constants.SYNC_FAIL); vehLogInfo += "??"; if(json!=null){ vehLogInfo += ""+json.toJSONString(); vehLog.setSyncLog(json.toJSONString()); }else{ vehLogInfo += ""+json; } } logger.info(vehLogInfo); // ? this.writeLog(dest, vehLog); */ } } else { ownLog.setSyncStatus(Constants.SYNC_FAIL); logInfo += ",?"; if (memberJson != null) { logInfo += "" + memberJson.toJSONString(); ownLog.setSyncLog(memberJson.toJSONString()); } else { logInfo += "" + memberJson; } registFailCount++; } // ? this.writeLog(dest, ownLog); // ??? ??? //dest.getConn().commit(); } logger.info(logInfo); //System.out.println(logInfo); start = registFailCount; } } catch (Exception e) { // setAutoCommit(false) ???????? /* try { dest.getConn().rollback(); } catch (Exception err) { err.printStackTrace(); } */ e.printStackTrace(); } finally { dest.close(); } Date endDate = new Date(); logger.info("...?" + node.getNodeName() + "," + endDate); } } public List<TmOwner> getRegistMember(DatabaseUtil dest, int start, int size) { List<TmOwner> list = null; String sql = "SELECT * from tm_owner where ( member_no is null or member_no ='' ) AND mobile !='' and mobile is not null AND owner_property=" + Constants.SYSTEM_STATUS_OWNER_PROPERTY2 + " order by entity_code,owner_no "; list = dest.getResult2Dbean(TmOwner.class, sql, start, size); return list; } public void updateTmOwnerInfo(DatabaseUtil dest, TmOwner to, String memberNo) { String updateSql = " update tm_owner set member_no = '" + memberNo + "' where owner_no ='" + to.getOwnerNo() + "' and entity_code='" + to.getEntityCode() + "'"; dest.executeUpdate(updateSql); } public List<TmVehicle> getVehicleByOwnerNo(DatabaseUtil dest, String entityCode, String ownerNo) { StringBuffer sql = new StringBuffer(); sql.append( " SELECT tv.*,tb.`brand_name`,ts.`series_name`,tm.`model_name` FROM tm_vehicle tv "); sql.append( " LEFT JOIN tm_brand tb "); sql.append( " ON tv.`entity_code` = tb.`entity_code` "); sql.append( " AND tv.`brand` = tb.`brand_code` "); sql.append( " LEFT JOIN tm_series ts "); sql.append( " ON tv.`entity_code` = ts.`entity_code` "); sql.append( " AND tv.`brand` = ts.`brand_code` "); sql.append( " AND tv.`series` = ts.`series_code` "); sql.append( " LEFT JOIN tm_model tm "); sql.append( " ON tv.`entity_code` = tm.`entity_code` "); sql.append( " AND tv.`brand` = tm.`brand_code` "); sql.append( " AND tv.`series` = tm.`series_code` "); sql.append( " AND tv.`model` = tm.`model_code` "); sql.append(" WHERE tv.`entity_code` = '").append(entityCode) .append("' "); sql.append(" AND tv.`owner_no` ='").append(ownerNo).append("' "); List<TmVehicle> list = dest.getResult2Dbean(TmVehicle.class, sql.toString(), -1, -1); return list; } public void updateTmVehicleInfo(DatabaseUtil dest, TmVehicle tv, String memberNo) { String updateSql = " update tm_vehicle set member_no = '" + memberNo + "' where vin ='" + tv.getVin() + "' and entity_code='" + tv.getEntityCode() + "'"; dest.executeUpdate(updateSql); } public void writeLog(DatabaseUtil dest, TmOvLog log) { String sql = " insert into tm_ov_log (sync_type,key_data,sync_log,log_time,sync_status,entity_code,data_node) values (?,?,?,?,?,?,?);"; List<Object> params = new ArrayList<Object>(); params.add(log.getSyncType()); params.add(log.getKeyData()); params.add(log.getSyncLog()); params.add(DateUtil.getCurrentTimestamp()); params.add(log.getSyncStatus()); params.add(log.getEntityCode()); params.add(log.getDataNode()); Connection conn = dest.getConn(); PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); for (int i = 0; i < params.size(); i++) { ps.setObject(i + 1, params.get(i)); } ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }