如何使用JBoss7.1 Infinispan的查询(How to use Infinispan q

2019-10-29 11:39发布

我有过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 ),所以我有我的项目中必要库。 但是在做的第一步提到这里 :

  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的全球组件,而休眠的搜索库在我的WAR的WEB-INF/lib的目录)。

有另一种方式我应该这样做呢?

Answer 1:

JBoss应用服务器7包括org.infinispan模块,因为它的内部使用集群子系统,但该模块不包括Lucene休眠的搜索引擎的依赖。

通过在你的应用程序指定这些依赖你(正确)添加缺少的依赖关系, 包括org.infinispan不“看”扩展的模块不能从您的应用程序的类路径中加载扩展点。

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

另一种解决方案是不依靠由AS包括org.infinispan模块上,而是包括所有在你的应用程序。 这样,您还可以使用不同版本,可能是最近的一个更大的灵活性。



Answer 2:

我最终排除了Infinispan和休眠从(而不是包含它们,这相当于同样的事情) 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();
   }


Answer 3:

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



文章来源: How to use Infinispan query with JBoss7.1