1 package search.db;
2 
3 import util.ConnectionUtil;
4 import util.SearchUtil;
5 import util.TableOperations;
6 import chemaxon.jchem.db.JChemSearch;
7 import chemaxon.sss.SearchConstants;
8 import chemaxon.sss.search.JChemSearchOptions;
9 import chemaxon.util.ConnectionHandler;
10
11/**
12 * Example codes for running a search in a separate thread
13 * 
14 * @author JChem Base team, ChemAxon Ltd.
15 */
16public final class AsyncSearchExample {
17
18    private static final String TABLE_NAME = "largetable";
19
20    private ConnectionHandler connHandler;
21
22    public static void main(String[] args) {
23        try {
24            new AsyncSearchExample().run();
25        } catch (Exception e) {
26            e.printStackTrace();
27        }
28    }
29
30    private void run() throws Exception {
31        connHandler = ConnectionUtil.connectToDB();
32        try {
33            // Create large table to have longer search and to see progress
34            TableOperations.setupLargeMoleculeTable(connHandler, TABLE_NAME);
35            search();
36        } finally {
37            ConnectionUtil.closeConnection(connHandler);
38        }
39    }
40
41    private void search() throws Exception {
42        JChemSearchOptions searchOpts = new JChemSearchOptions(SearchConstants.SUBSTRUCTURE);
43        JChemSearch jcs =
44                SearchUtil.createJChemSearch(connHandler, "Brc1ccccc1", TABLE_NAME, searchOpts);
45
46        jcs.setRunMode(JChemSearch.RUN_MODE_ASYNCH_COMPLETE);
47        jcs.run();
48
49        while (jcs.isRunning()) {
50            String msg = jcs.getProgressMessage();
51            int count = jcs.getResultCount();
52            System.out.printf("Progress message: %s, result count: %d\n", msg, count);
53            Thread.sleep(50);
54        }
55
56        System.out.println(jcs.getResultCount() + " hit(s) found.");
57    }
58
59}
60