public

OSGI bound Backend Processor for Hibernate Search

  • Download Gist
OSGIJmsBackendQueueProcessor.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
package de.eiswind.mango.hibernate.osgi;
 
import java.util.List;
 
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
 
import org.apache.log4j.Logger;
import org.hibernate.search.backend.IndexingMonitor;
import org.hibernate.search.backend.LuceneWork;
import org.hibernate.search.backend.impl.jms.JMSBackendQueueProcessor;
import org.hibernate.search.backend.impl.jms.JMSBackendQueueTask;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
 
import de.eiswind.org.hibernate.Activator;
 
public class OSGIJmsBackendQueueProcessor extends JMSBackendQueueProcessor {
 
private static final Logger log = Logger.getLogger(OSGIJmsBackendQueueProcessor.class);
private QueueConnectionFactory jmsConnectionFactory;
private Queue jmsQueue;
 
private BundleContext ctx;
 
@Override
public void prepareJMSTools() {
if(jmsConnectionFactory!=null && jmsQueue!=null){
return;
}
ctx = Activator.getContext();
ServiceTracker<QueueConnectionFactory, Object> tracker = new ServiceTracker<QueueConnectionFactory, Object>(ctx, QueueConnectionFactory.class, null) {
 
@Override
public Object addingService(ServiceReference<QueueConnectionFactory> reference) {
log.info("Found JMS Connection Factory");
OSGIJmsBackendQueueProcessor.this.jmsConnectionFactory = ctx.getService(reference);
return OSGIJmsBackendQueueProcessor.this.jmsConnectionFactory;
}
 
};
tracker.open();
Filter filter;
try {
filter = ctx.createFilter("(&(objectClass=javax.jms.Queue)(queue=hibernatesearch))");
ServiceTracker<Queue, Object> tracker2 = new ServiceTracker<Queue, Object>(ctx, filter, null) {
 
@Override
public Object addingService(ServiceReference<Queue> reference) {
log.info("Found JMS Queue");
OSGIJmsBackendQueueProcessor.this.jmsQueue = ctx.getService(reference);
return OSGIJmsBackendQueueProcessor.this.jmsQueue;
}
 
};
tracker2.open();
} catch (InvalidSyntaxException e) {
log.error("Create filter", e);
}
 
}
 
public QueueConnectionFactory getJMSFactory() {
if(this.jmsConnectionFactory==null){
throw new IllegalStateException("Connection not ready.");
}
return jmsConnectionFactory;
}
 
public Queue getJmsQueue() {
if(this.jmsQueue==null){
throw new IllegalStateException("Queue not ready");
}
return jmsQueue;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.