Skip to content

Instantly share code, notes, and snippets.

@ariejan
Created July 5, 2010 21:12
Show Gist options
  • Save ariejan/464688 to your computer and use it in GitHub Desktop.
Save ariejan/464688 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nl.kabisa.mgl.rssninja.beans;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
/**
*
* @author ariejan
*/
@MessageDriven(mappedName = "jms/filterQueue", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class LuceneFilterBean implements MessageListener {
/**
* Make a Document object with an un-indexed title field and an
* indexed content field.
*/
private static Document createDocument(String title, String content) {
Document doc = new Document();
doc.add(new Field("title", title, Field.Store.YES, Field.Index.NO));
doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
return doc;
}
@Override
public void onMessage(Message message) {
// Construct a RAMDirectory to hold the in-memory representation
// of the index.
long start = System.currentTimeMillis();
RAMDirectory idx = new RAMDirectory();
TextMessage text = (TextMessage) message;
try {
// Make an writer to create the index
IndexWriter writer = new IndexWriter(idx,
new StandardAnalyzer(Version.LUCENE_30),
true,
IndexWriter.MaxFieldLength.UNLIMITED);
// Add some Document objects containing quotes
writer.addDocument(createDocument("Theodore Roosevelt",
"It behooves every man to remember that the work of the " +
"critic, is of altogether secondary importance, and that, " +
"in the end, progress is accomplished by the man who does " +
"things."));
writer.addDocument(createDocument("Friedrich Hayek",
"The case for individual freedom rests largely on the " +
"recognition of the inevitable and universal ignorance " +
"of all of us concerning a great many of the factors on " +
"which the achievements of our ends and welfare depend."));
writer.addDocument(createDocument("Ayn Rand",
"There is nothing to take a man's freedom away from " +
"him, save other men. To be free, a man must be free " +
"of his brothers."));
writer.addDocument(createDocument("Mohandas Gandhi",
"Freedom is not worth having if it does not connote " +
"freedom to err."));
writer.addDocument(createDocument("Incoming message", text.getText()));
// Optimize and close the writer to finish building the index
writer.optimize();
writer.close();
System.out.println("== Indexed in: " + (System.currentTimeMillis() - start) + "ms.");
// Build an IndexSearcher using the in-memory index
Searcher searcher = new IndexSearcher(idx);
long queryStart = System.currentTimeMillis();
for(int i = 0; i < 10000; i++) {
search(searcher, "freedom");
search(searcher, "free");
search(searcher, "progress or achievements");
}
System.out.println("Ran 30.000 queries in " + (System.currentTimeMillis() - queryStart) + "ms");
searcher.close();
Date endTime = new Date();
System.out.println("== Total elapsed: " + (System.currentTimeMillis() - start) + "ms.");
} catch (JMSException ex) {
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ParseException ex) {
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex);
}
catch (CorruptIndexException ex) {
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex);
}
catch (LockObtainFailedException ex) {
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
// Stuffs
}
}
private static void search(Searcher searcher, String queryString)
throws ParseException, IOException {
// Build a Query object
QueryParser parser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30));
Query query = parser.parse(queryString);
// Search for the query
// Hits hits = searcher.search(query);
TopDocs hits = searcher.search(query, 10);
// Examine the Hits object to see if there were any matches
int hitCount = hits.totalHits;
if (hitCount == 0) {
//System.out.println("No matches were found for \"" + queryString + "\"");
}
else {
// System.out.println("Hits for \"" + queryString + "\" were found in quotes by:");
// Iterate over the Documents in the Hits object
for (int i = 0; i < hitCount; i++) {
ScoreDoc doc = hits.scoreDocs[i];
// Print the value that we stored in the "title" field. Note
// that this Field was not indexed, but (unlike the
// "contents" field) was stored verbatim and can be
// retrieved.
// System.out.println(" " + (i + 1) + ". " + doc.toString());
}
}
// System.out.println();
}
}
[#|2010-07-05T23:09:53.759+0200|INFO|glassfish3.0.1|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=31;_ThreadName=AutoDeployer;|[AutoDeploy] Successfully autodeployed : /Users/ariejan/Code/Java/glassfishv3/glassfish/domains/domain1/autodeploy/RSSNinja-ear-1.0-SNAPSHOT.ear.|#]
[#|2010-07-05T23:09:56.803+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|Ran 30.000 queries in 3450ms|#]
[#|2010-07-05T23:09:56.803+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Total elapsed: 3461ms.|#]
[#|2010-07-05T23:09:56.823+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|Ran 30.000 queries in 3450ms|#]
[#|2010-07-05T23:09:56.824+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Total elapsed: 3479ms.|#]
[#|2010-07-05T23:09:56.825+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|Ran 30.000 queries in 3447ms|#]
[#|2010-07-05T23:09:56.825+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|Ran 30.000 queries in 3474ms|#]
[#|2010-07-05T23:09:56.826+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Total elapsed: 3483ms.|#]
[#|2010-07-05T23:09:56.826+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|== Total elapsed: 3479ms.|#]
[#|2010-07-05T23:09:56.843+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|Ran 30.000 queries in 3470ms|#]
[#|2010-07-05T23:09:56.843+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|== Total elapsed: 3498ms.|#]
[#|2010-07-05T23:10:00.008+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:10:00.083+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Indexed in: 4ms.|#]
[#|2010-07-05T23:10:01.277+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|Ran 30.000 queries in 1192ms|#]
[#|2010-07-05T23:10:01.277+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Total elapsed: 1198ms.|#]
[#|2010-07-05T23:10:10.001+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:10:10.018+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Indexed in: 3ms.|#]
[#|2010-07-05T23:10:11.205+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|Ran 30.000 queries in 1187ms|#]
[#|2010-07-05T23:10:11.206+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Total elapsed: 1191ms.|#]
[#|2010-07-05T23:10:20.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:10:20.383+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Indexed in: 3ms.|#]
[#|2010-07-05T23:10:21.566+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|Ran 30.000 queries in 1183ms|#]
[#|2010-07-05T23:10:21.566+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Total elapsed: 1186ms.|#]
[#|2010-07-05T23:10:30.001+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:10:30.017+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|== Indexed in: 4ms.|#]
[#|2010-07-05T23:10:31.296+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|Ran 30.000 queries in 1279ms|#]
[#|2010-07-05T23:10:31.296+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|== Total elapsed: 1283ms.|#]
[#|2010-07-05T23:10:40.001+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:10:40.015+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|== Indexed in: 2ms.|#]
[#|2010-07-05T23:10:41.200+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|Ran 30.000 queries in 1185ms|#]
[#|2010-07-05T23:10:41.201+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|== Total elapsed: 1188ms.|#]
[#|2010-07-05T23:10:50.001+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:10:50.017+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Indexed in: 5ms.|#]
[#|2010-07-05T23:10:51.233+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|Ran 30.000 queries in 1215ms|#]
[#|2010-07-05T23:10:51.234+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Total elapsed: 1222ms.|#]
[#|2010-07-05T23:11:00.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:11:00.032+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Indexed in: 3ms.|#]
[#|2010-07-05T23:11:01.233+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|Ran 30.000 queries in 1201ms|#]
[#|2010-07-05T23:11:01.233+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Total elapsed: 1204ms.|#]
[#|2010-07-05T23:11:10.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:11:10.016+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Indexed in: 2ms.|#]
[#|2010-07-05T23:11:11.202+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|Ran 30.000 queries in 1186ms|#]
[#|2010-07-05T23:11:11.203+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Total elapsed: 1189ms.|#]
[#|2010-07-05T23:11:20.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:11:20.017+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|== Indexed in: 3ms.|#]
[#|2010-07-05T23:11:21.197+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|Ran 30.000 queries in 1180ms|#]
[#|2010-07-05T23:11:21.198+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=p: thread-pool-1; w: 7;|== Total elapsed: 1184ms.|#]
[#|2010-07-05T23:11:30.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:11:30.015+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|== Indexed in: 2ms.|#]
[#|2010-07-05T23:11:31.260+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|Ran 30.000 queries in 1244ms|#]
[#|2010-07-05T23:11:31.260+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=p: thread-pool-1; w: 6;|== Total elapsed: 1247ms.|#]
[#|2010-07-05T23:11:40.001+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:11:40.054+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Indexed in: 2ms.|#]
[#|2010-07-05T23:11:41.252+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|Ran 30.000 queries in 1196ms|#]
[#|2010-07-05T23:11:41.252+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=p: thread-pool-1; w: 4;|== Total elapsed: 1200ms.|#]
[#|2010-07-05T23:11:50.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:11:50.048+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Indexed in: 3ms.|#]
[#|2010-07-05T23:11:51.241+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|Ran 30.000 queries in 1193ms|#]
[#|2010-07-05T23:11:51.241+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=19;_ThreadName=p: thread-pool-1; w: 3;|== Total elapsed: 1196ms.|#]
[#|2010-07-05T23:12:00.002+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Ejb-Timer-Thread-1;|Sending message: Shields up! Rrred Alert!|#]
[#|2010-07-05T23:12:00.017+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Indexed in: 2ms.|#]
[#|2010-07-05T23:12:01.240+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|Ran 30.000 queries in 1222ms|#]
[#|2010-07-05T23:12:01.240+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=35;_ThreadName=p: thread-pool-1; w: 5;|== Total elapsed: 1225ms.|#]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment