0

我有一个通过 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 模块中包含的版本。这会引入所有必要的依赖项(包括luceneand hibernate-search-engine),因此我的项目中确实有必要的库。但是,在执行此处提到的初始步骤时:

  SearchManager searchManager = Search.getSearchManager( cache );

它调用ComponentRegistryUtils.getComponent(cache, SearchFactoryIntegrator.class)which 抛出失败IllegalArgumentException

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

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

我应该这样做吗?

4

3 回答 3

5

JBoss AS 7 包含一个org.infinispan模块,因为它被集群子系统内部使用,但是这个模块不包括lucenehibernate-search-engine依赖项。

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

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

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

于 2013-05-22T23:09:32.173 回答
1

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

然后我在我的pom.xml文件中添加了一个依赖项,org.infinispan:infinispan-query:5.2.1.Final以将所有罐子拉到我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();
   }
于 2013-05-28T19:36:44.900 回答
0

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

于 2013-05-22T22:35:15.313 回答