spring+hibernate+struts2+compass整合(五)

2014-11-24 07:34:31 · 作者: · 浏览: 7
次启动时重建索引,可以设置buildIndex属性为false来禁止此功能. 也可以不用本Builder,
10 * 编写手动调用compassGps.index()的代码.
11 *
12 */
13 public class CompassIndexBuilder implements InitializingBean {
14 // 是否需要建立索引,可被设置为false使本Builder失效.
15 private boolean buildIndex = false;
16
17 // 索引操作线程延时启动的时间,单位为秒
18 private int lazyTime = 10;
19
20 // Compass封装
21 private CompassGps compassGps;
22
23 // 索引线程
24 private Thread indexThread = new Thread() {
25
26 @Override
27 public void run() {
28 try {
29 Thread.sleep(lazyTime * 1000);
30 System.out.println("begin compass index...");
31 long beginTime = System.currentTimeMillis();
32 // 重建索引.
33 // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,
34 // 索引完成后再进行覆盖.
35 compassGps.index();
36 long costTime = System.currentTimeMillis() - beginTime;
37 System.out.println("compss index finished.");
38 System.out.println("costed " + costTime + " milliseconds");
39 } catch (InterruptedException e) {
40 e.printStackTrace();
41 }
42 }
43 };
44
45 /**
46 * 实现InitializingBean接口,在完成注入后调用启动索引线程.
47 *
48 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
49 */
50 public void afterPropertiesSet() throws Exception {
51 if (buildIndex) {
52 indexThread.setDaemon(true);
53 indexThread.setName("Compass Indexer");
54 indexThread.start();
55 }
56 }
57
58 public void setBuildIndex(boolean buildIndex) {
59 this.buildIndex = buildIndex;
60 }
61
62 public void setLazyTime(int lazyTime) {
63 this.lazyTime = lazyTime;
64 }
65
66 public void setCompassGps(CompassGps compassGps) {
67 this.compassGps = compassGps;
68 }
69 }

/compass1/src/com/v512/example/service/impl/ProductManagerImpl.java
1 package com.v512.example.service.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.compass.core.CompassHits;
7 import org.compass.core.CompassSession;
8 import org.compass.core.CompassTemplate;
9 import org.compass.core.CompassTransaction;
10
11 import com.v512.example.dao.ProductDao;
12 import com.v512.example.model.Product;
13 import com.v512.example.service.ProductManager;
14 import org.compass.core.Compass;
15
16 public class ProductManagerImpl implements ProductManager {
17 private ProductDao productDao;
18 private CompassTemplate compassTemplate;
19
20 public void setCompassTemplate(CompassTemplate compassTemplate) {
21 this.compassTemplate = compassTemplate;
22 }
23
24 public void setProductDao(ProductDao productDao) {
25 this.productDao = productDao;
26 }
27
28 public Product findProdcut(String id) {
29 return productDao.getProduct(id);
30 }
31
32 public void insertProduct(Product p) {
33 productDao.create(p);
34
35 }
36
37 public List searchProducts(String queryString) {
38 Compass compass = compassTemplate.getCompass();
39 CompassSession session = compass.openSession();
40 List list = new ArrayList();
41
42 CompassHits hits = session.queryBuilder().queryString(
43