Over a million developers have joined DZone.

RAP, OSGi & HibernateSearch Infinispan Cluster

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

While trying to setup Hibernate search on an Infinspan cluster for my Remote Application Platform application I came across another "Hibernate doesn't like OSGi" oddity.

The JMS setup for Hibernate Search is configured through JNDI. That wouldn't be a problem when running while deployed on my tomcat instances, but how should this work in my develepment setup, where I use the RAP tooling and run on the embedded Jetty HTTP service? No JNDI there.

So I created a replacement for the JMSBackendprocessor from Hibernate Search that looks up the JMS connection by means of OSGi. Bye bye good old JNDI, never liked you either.

Now if I only had a failover for the workbench...

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;

	public void prepareJMSTools() {
		if(jmsConnectionFactory!=null && jmsQueue!=null){
		ctx = Activator.getContext();
		ServiceTracker<QueueConnectionFactory, Object> tracker = new ServiceTracker<QueueConnectionFactory, Object>(ctx, QueueConnectionFactory.class, null) {

			public Object addingService(ServiceReference<QueueConnectionFactory> reference) {
				log.info("Found JMS Connection Factory");
				OSGIJmsBackendQueueProcessor.this.jmsConnectionFactory = ctx.getService(reference);
				return OSGIJmsBackendQueueProcessor.this.jmsConnectionFactory;

		Filter filter;
		try {
			filter = ctx.createFilter("(&(objectClass=javax.jms.Queue)(queue=hibernatesearch))");
			ServiceTracker<Queue, Object> tracker2 = new ServiceTracker<Queue, Object>(ctx, filter, null) {

				public Object addingService(ServiceReference<Queue> reference) {
					log.info("Found JMS Queue");
					OSGIJmsBackendQueueProcessor.this.jmsQueue = ctx.getService(reference);
					return OSGIJmsBackendQueueProcessor.this.jmsQueue;

		} catch (InvalidSyntaxException e) {
			log.error("Create filter", e);


	public QueueConnectionFactory getJMSFactory() {
			throw new IllegalStateException("Connection not ready.");
		return jmsConnectionFactory;

	public Queue getJmsQueue() {
			throw new IllegalStateException("Queue not ready");
		return jmsQueue;


Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.


Published at DZone with permission of Thomas Kratz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}