这篇文章将为大家详细讲解有关Hibernate search的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
怎么工作:
-Hibernate Search 支持使用JMS back-end 和 master/slave(s) 索引进行分布式配置
- mater通过网络共享暴露索引 (例如通过NFS。。。)
- slave(s) 复制mater的索引到本地
版本
- Apache Tomcat 6.0.20
- Hibernate Search 3.1.1 GA
- Apache ActiveMQ 5.3.0
- Spring 2.5.6
- XBean-Spring 3.6
${local.index.dir} - directory to store master index
${master.index.dir} - directory to copy master index to, it's shared network location for replication with slave(s)
Mater索引的配置
Mater的配置稍显复杂。以下是配置属性说明
$(local.index.dir) - 存储mater索引的目录
$(master.index.dir) -拷贝mater索引的目标目录,通过网络定位供slave(s)复制。
首先,为了简单起见,我们将ActiveMQ中介运行在同一台服务器上。因此,我们在WEB-INF/activemq.xml文件中使用简单的嵌入式中介配置。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <amq:broker brokerName="HibernateSearchBroker"> <amq:managementContext> <amq:managementContext createConnector="false"/> </amq:managementContext> <amq:transportConnectors> <amq:transportConnector uri="tcp://localhost:61616" /> </amq:transportConnectors> </amq:broker> <amq:queue name="queue/hibernatesearch" physicalName="hibernateSearchQueue" /> </beans>
其次,需要在web application MEAT-INF/context.xml(Tocmat)中配置配置JNDI资源(JMS Connection Factory 和 Quene)
<!-- ActiveMQ ConnectionFactory --> <Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://0.0.0.0:61616?trace=true" brokerName="HibernateSearchBroker" /> <!-- ActiveMQ HibernateSearch queue --> <Resource name="queue/hibernatesearch" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" description="Hibernate search queue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="hibernateSearchQueue" />
接下来在Hibernate.cfg.xml文件中配置Hibernate Searc
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSMasterDirectoryProvider</property> <property name="hibernate.search.default.indexBase">${local.index.dir}</property> <property name="hibernate.search.default.sourceBase">${master.index.dir}</property> <property name="hibernate.search.default.refresh">60</property>
Master和slave之间代码的一个重要区别是:master中必须包含实现 AbstractJMSHibernateSearchController 的子类作为监听器,例如
import javax.jms.MessageListener; import org.hibernate.Session; import org.hibernate.search.backend.impl.jms.AbstractJMSHibernateSearchController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class JMSHibernateSearchController extends AbstractJMSHibernateSearchController implements MessageListener { @Override protected void cleanSessionIfNeeded(Session session) { // clean session here ... } @Override protected Session getSession() { // return new session here ... } }
最后在Spring的配置文件applicationContext.xml文件中,加入以下配置
<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean"> <property name="config" value="WEB-INF/activemq.xml" /> <property name="start" value="true" /> </bean> <bean name="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jms/ConnectionFactory" /> </bean> <bean name="jmsHibernateSearchQueue" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/queue/hibernatesearch" /> <bean id="hibernateSearchController" class="<your implementation of AbstractJMSHibernateSearchController>" /> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" depends-on="broker"> <property name="connectionFactory" ref="jmsConnectionFactory"/> <property name="destination" ref="jmsHibernateSearchQueue"/> <property name="messageListener" ref="hibernateSearchController" /> </bean>
With those configurations in place Hibernate Search master is ready to run.
正确配置好之后,Hibernate Search master就可以运行了。
Slave 索引配置
Slave(s)的配置要简单得多,以下是配置属性说明
${server} - 运行ActiveMQ中介服务器
${lcoal.index.dir} - 存储本地索引的目录(master的拷贝)
${mater.index.share}-挂载mater索引的网络共享目录.
首先,我们需要在网站应用 MEAT-INF/context.xml(Tocmat)中配置配置JNDI资源(JMS Connection Factory 和 Quene)
... <!-- ActiveMQ ConnectionFactory --> <Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://${server}:61616?trace=true" brokerName="HibernateSearchBroker" /> <!-- ActiveMQ HibernateSearch queue --> <Resource name="queue/hibernatesearch" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" description="Hibernate search queue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="hibernateSearchQueue" /> ...
接下来我们需要在hibernate.cfg.xml中配置Hibernate Search
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSSlaveDirectoryProvider</property> <property name="hibernate.search.default.indexBase">${local.index.dir}</property> <property name="hibernate.search.default.sourceBase">${master.index.share}</property> <property name="hibernate.search.default.refresh">60</property> <property name="hibernate.search.worker.backend">jms</property> <property name="hibernate.search.worker.jms.connection_factory">java:comp/env/jms/ConnectionFactory</property> <property name="hibernate.search.worker.jms.queue">java:comp/env/queue/hibernatesearch</property> <property name="hibernate.search.worker.jndi.java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</property>
配置到此结束
说些关于JUnit测试的题外话.唯一的问题是需要使用Spring JNDI templates模拟JNDI,例如
<bean name="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jms/ConnectionFactory" /> <property name="jndiTemplate"> <bean class="org.springframework.mock.jndi.ExpectedLookupTemplate"> <constructor-arg index="0" value="java:comp/env/jms/ConnectionFactory" /> <constructor-arg index="1"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL"> <value>tcp://0.0.0.0:61616</value> </property> </bean> </constructor-arg> </bean> </property> </bean> <bean name="jmsHibernateSearchQueue" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/queue/hibernatesearch" /> <property name="jndiTemplate"> <bean class="org.springframework.mock.jndi.ExpectedLookupTemplate"> <constructor-arg index="0" value="java:comp/env/queue/hibernatesearch" /> <constructor-arg index="1"> <bean id="jmsHibernateSearchQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="queue/hibernateSearchQueue"/> </bean> </constructor-arg> </bean> </property> </bean>
关于“Hibernate search的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。