如何在JBoss7.1中使用Infinispan查询

时间:2013-05-22 20:55:28

标签: java lucene jboss7.x hibernate-search infinispan

我有一个通过JBoss7.1 Web界面创建的Infinispan缓存。它被配置为索引的分布式缓存。

在我的jboss-deployment-structure.xml文件中,我添加了org.infinispanorg.hibernate的依赖项,因此我可以访问我的缓存。我还在以下内容中添加了maven依赖项:

  <dependency>
     <groupId>org.infinispan</groupId>
     <artifactId>infinispan-core</artifactId>
     <version>5.1.7.Final</version>
     <scope>provided</scope>
  </dependency>
  <dependency>
     <groupId>org.infinispan</groupId>
     <artifactId>infinispan-query</artifactId>
     <version>5.1.7.Final</version>
  </dependency>

5.1.7.Final是我正在使用的JBoss7.1.3中的org.infinispan模块中包含的版本。这会引入所有必需的依赖项(包括lucenehibernate-search-engine),因此我在项目中拥有必要的库。但是,在执行提到here的初始步骤时:

  SearchManager searchManager = Search.getSearchManager( cache );

它调用ComponentRegistryUtils.getComponent(cache, SearchFactoryIntegrator.class),无法投掷IllegalArgumentException

  

此缓存未启用索引。接口   注册表中找不到org.hibernate.search.spi.SearchFactoryIntegrator

我的缓存启用了索引,cache.getCacheConfiguration().indexing().enabled()返回true可以看到。但应用程序认为不是。也许这是因为缓存的ComponentRegistry无法访问org.hibernate.search.spi.SearchFactoryIntegrator类(缓存是JBoss全局组件,而hibernate搜索库位于我的WAR的WEB-INF/lib目录中)。 / p>

我应该采取另一种方式吗?

3 个答案:

答案 0 :(得分:5)

JBoss AS 7包含一个 org.infinispan 模块,因为它在集群子系统内部使用,但该模块不包含 lucene hibernate-search -engine 依赖。

通过在应用程序中指定这些依赖项,您(正确地)添加缺少的依赖项包含的 org.infinispan 不会“看到”扩展名为模块无法从应用程序的类路径加载扩展点。

所以可能的解决方案是将这些依赖项添加到AS7模块并修补 org.infinispan 模块以从自定义模块导入这些资源。

另一种解决方案是不依赖AS包含的 org.infinispan 模块,而是将其全部包含在您的应用程序中。这样,您在使用不同版本时也可以更灵活,可能是更新版本。

答案 1 :(得分:1)

我最终从jboss-deployment-structure.xml文件中排除了infinispan和hibernate(而不是包括它们,这相同)。

然后我在pom.xml上的org.infinispan:infinispan-query:5.2.1.Final文件中添加了一个依赖项,将所有jar放入我的WAR工件的WEB-INF/lib目录。

然后我以编程方式构建了缓存:

package com.myproduct.cache;

import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.cache.ConfigurationChildBuilder;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationChildBuilder;
import org.infinispan.eviction.EvictionStrategy;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.transaction.LockingMode;
import org.infinispan.transaction.TransactionMode;
import org.infinispan.transaction.lookup.GenericTransactionManagerLookup;
import org.infinispan.util.concurrent.IsolationLevel;
import org.jetbrains.annotations.NotNull;

class MyObjectCacheFactory
{
   private static EmbeddedCacheManager m_cacheManager = null;
   private static Cache<String, MyObject> m_cache = null;

   @NotNull
   public static Cache<String, MyObject> getMyObjectCache ()
   {
      if( m_cache == null )
         createMyObjectCache();
      return m_cache;
   }

   private static synchronized void createMyObjectCache ()
   {
      if( m_cacheManager == null )
         createCacheManager();

      if( !m_cacheManager.cacheExists( "MyObjects" ) )
      {
         Configuration cacheConfig = createMyObjectCacheConfig();
         m_cacheManager.defineConfiguration( "MyObjects", cacheConfig );
      }

      if( !m_cacheManager.isRunning( "MyObjects" ) )
         m_cacheManager.getCache( "MyObjects" ).start();
      m_cache = m_cacheManager.getCache( "MyObjects" );
   }

   private static void createCacheManager ()
   {
      GlobalConfiguration globalConfiguration = createGlobalConfiguration();
      Configuration defaultCacheConfig = createDefaultCacheConfiguration();
      m_cacheManager = new DefaultCacheManager( globalConfiguration, defaultCacheConfig );
   }

   @NotNull
   private static Configuration createDefaultCacheConfiguration ()
   {
      ConfigurationChildBuilder builder = new ConfigurationBuilder();
      builder = builder.jmxStatistics().enable();
      builder = builder.clustering()
         .cacheMode( CacheMode.DIST_SYNC )
         .stateTransfer()
         .timeout( 100 );
      builder = builder.transaction()
         .transactionMode( TransactionMode.TRANSACTIONAL )
         .autoCommit( false )
         .lockingMode( LockingMode.OPTIMISTIC );
      return builder.build();
   }

   @NotNull
   private static GlobalConfiguration createGlobalConfiguration ()
   {
      GlobalConfigurationChildBuilder builder = new GlobalConfigurationBuilder().clusteredDefault();
      builder = builder.globalJmxStatistics()
         .enable()
         .cacheManagerName( "MyCacheManager" )
         .jmxDomain( "com.myproduct.cache" );
      return builder.build();
   }

   @NotNull
   private static Configuration createMyObjectCacheConfig ()
   {
      ConfigurationChildBuilder builder = new ConfigurationBuilder();
      builder = builder.jmxStatistics().enable();
      builder = builder.clustering().cacheMode( CacheMode.DIST_SYNC );
      builder = builder.transaction()
         .transactionMode( TransactionMode.TRANSACTIONAL )
         .autoCommit( false )
         .lockingMode( LockingMode.OPTIMISTIC )
         .transactionManagerLookup( new GenericTransactionManagerLookup() );
      builder = builder.locking().isolationLevel( IsolationLevel.REPEATABLE_READ );
      builder = builder.eviction().maxEntries( 100 ).strategy( EvictionStrategy.LRU );
      builder = builder.expiration().maxIdle( 30000 ).lifespan( -1 ).enableReaper();
      builder = builder.loaders().passivation( true ).addFileCacheStore().purgeOnStartup( true );
      builder = builder.indexing().enable().addProperty( "default.directory_provider", "ram" );
      return builder.build();
   }
}

并将@Field注释添加到MyObject

package com.myproduct.cache;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

import java.io.Serializable;

@Indexed
public class MyObject
   implements Serializable
{
   @NotNull private String m_id;

   @Field(name = "searchNumber")
   private int m_searchNumber;

   public MyObject (
      @NotNull String id,
      int searchNumber )
   {
      m_id = id;
      m_searchNumber = searchNumber;
   }

   @NotNull
   public String getId ()
   {
      return m_id;
   }

   public int getSearchNumber ()
   {
      return m_searchNumber;
   }
}

和搜索方法:

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.infinispan.Cache;
import org.infinispan.query.CacheQuery;
import org.infinispan.query.Search;
import org.infinispan.query.SearchManager;

[...]

   private Cache<String, MyObject> m_cache;

   @PostConstruct
   public void setup ()
   {
      m_cache = MyObjectCacheFactory.getMyObjectCache();
   }

   @NotNull
   public List<MyObject> getMyObjects ( int searchNumber )
   {
      SearchManager searchManager = Search.getSearchManager( m_cache );
      QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass( MyObject.class ).get();
      Query luceneQuery = queryBuilder.keyword().onField( "searchNumber" ).matching( searchNumber ).createQuery();
      CacheQuery cacheQuery = searchManager.getQuery( luceneQuery, MyObject.class );
      //noinspection unchecked
      return (List)cacheQuery.list();
   }

答案 2 :(得分:0)

我发现:https://community.jboss.org/message/807112#807112我希望,它也可能对你有所帮助。