您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。