後臺(tái)的架構(gòu)是 SpringMVC, Spring, jpa(HibernateJpaDialect),
DataSource(c3p0), Mysql(InnoBDB),
transactionManager(JpaTransactionManager)。
@Transactional(value = "transactionManager", isolation = Isolation.READ_UNCOMMITTED)
public Object addScenicSpot(int tourGuideID, String jsonStr) {
Djd_js entity = new Djd_js();
try{
_setEntity(entity, jsonStr);
entity.setDaoyouID(tourGuideID);
jdjsDao.save(entity);
int spotId = entity.getId();
//添加信息到消息隊(duì)列中
try {
Sender sender = new SenderImpl();
sender.getGPSFromBaiduAPI("jdjs", spotId, entity.getDizhi());
} catch (InterruptedException e) {
return false;
}
return spotId;
}catch (Exception e){
return false;
}
}
以上是保存的部分,并把得到的 ID 發(fā)送到消息隊(duì)列中,下邊是消息隊(duì)列的處理部分
public boolean updateLngAndLat(MessageVo messageVo) {
System.out.println("CreateTime--------"+messageVo.getCreateDate());
System.out.println("Address--------"+messageVo.getContent());
System.out.println("Id--------"+messageVo.getId());
Djd_js entity = jdjsDao.findOne(messageVo.getId());
System.out.println("entity-Address--------"+entity.getDizhi());
、、運(yùn)行到這里就直接卡住了,如果注釋掉查詢,其他的調(diào)用皆正常。
Map<String, Object> result = LngAndLatUtil.getLngAndLat(((MessageVo) messageVo).getContent());
System.out.println("result--------"+(int)result.get("result"));
if (1 == (int)result.get("result")){
entity.setJingdu(Double.valueOf(result.get("lng").toString()));
entity.setWeidu(Double.valueOf(result.get("lat").toString()));
System.out.println("message-------------------------------"+"lng:"+Double.valueOf(result.get("lng").toString())+", lat:"+Double.valueOf(result.get("lat").toString()));
jdjsDao.updateLngAndLatBySenciSpotID(messageVo.getId(), (Double) result.get("lng"), (Double) result.get("lat"));
}else {
System.out.println("message-------------------------------False");
}
return false;
}
前端呼叫addScenicSpot() 方法,會(huì)將資訊儲(chǔ)存到資料庫中,然後將儲(chǔ)存之後的資料控中的ID傳送到訊息佇列中,然後訂閱者處理佇列中的信息,根據(jù)ID 查詢到剛儲(chǔ)存的訊息,然後呼叫外部介面查詢到經(jīng)緯度,並將得到的經(jīng)緯度儲(chǔ)存到資料庫中。
現(xiàn)在的問題是,保存資訊正常,但是到了訂閱者處理這邊,根據(jù)得到的 ID 查找不到保存的資訊。
產(chǎn)生bug的原因是spring事務(wù)提交晚於訊息佇列的生產(chǎn)訊息,導(dǎo)致訊息佇列消費(fèi)訊息時(shí)所獲得的資料不正確,
靈感來自這裡:http://www.cnblogs.com/ taocon...
已經(jīng)解決了問題了,應(yīng)用的這裡的方法:http://www.cnblogs.com/taocon...