服务器其它

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 服务器其它 > 分布式Hibernate search

分布式Hibernate search详解

作者:liuyazhuang

这篇文章主要介绍了分布式Hibernate search的相关内容,小编觉得挺不错的,这里分享给大家,供需要的朋友参考。

分布式Hibernate Search与Apache Tomcat6,ActiveMQ 和Spring.今天我将跟大家分享我的经验,以master/slave(s)方式配置分布式Hibernate Search并整合Apache ActiveMQ,Spring,应用程序额容器是Apache Tomcat 6。

怎么工作:

-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详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Hibernate实现悲观锁和乐观锁代码介绍Hibernate核心思想与接口简介等,有什么问题可以随时留言,小编会及时回复大家的,感谢朋友们对本站的支持!

您可能感兴趣的文章:
阅读全文