之前基础不是很扎实。尤其try catch这块
这是一段更新索引库的代码。功能是在更新商品,并在avtiveMQ发送了消息后,随之更新索引库里的信息
public TaotaoResult updateSearchItemById(Long itemId) throws Exception{
//注入mapper
//查询到记录
SearchItem item = mapper.getSearchItemById(itemId);System.out.println("查询到了商品"+item.getTitle());
//把记录更新到索引库
//创建solrserver 注入进来
//创建solrinputdocument对象
SolrInputDocument document = new SolrInputDocument();
//向文档对象中添加域
document.addField("id", item.getId().toString());//这里是字符串需要转换
document.addField("item_title", item.getTitle());
document.addField("item_sell_point", item.getSell_point());
document.addField("item_price", item.getPrice());
document.addField("item_image", item.getImage());
document.addField("item_category_name", item.getCategory_name());
document.addField("item_desc", item.getItem_desc());
//向索引库中添加文档
solrServer.add(document);
//提交
solrServer.commit();System.out.println("更新了索引");
return TaotaoResult.ok();
}
这里执行这两句的时候
solrServer.add(document);
solrServer.commit();System.out.println("更新了索引");
并未弹出红线和要求try catch
但是在我自己完成同步删除索引库的时候
public TaotaoResult deleteSearchItemByIds(List<Long> asList) {
for(Long id: asList){
String id2=id+"";
try {
solrServer.deleteById(id2);
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
solrServer.commit();
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
solrServer.deleteById(id2);和solrServer.commit();这两句他要求try..catch包围。这是为什么?
另问。消息监听器把消息转化成了ObjectMessage。长整型数组。然后用Arrays.asList转化为长整型列表。但是solrServer的deleteById方法中,没有接收这种参数的。只有接收List<String> list
的。和单个String id的,所以我又用for循环转化成String,然后才调用DeleteById进行删除。总感觉这样很笨。请问官方的,高手的写法是什么?就单单删除索引库这块。