我最终从文件中排除了 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();
}