5.3. Remote Query
When executing remote queries the cacheManager must be an instance of
RemoteCacheManager
, and an example configuration utilizing a RemoteCacheManager
is found below for both Java and blueprint.xml:
Using only Java
from("direct:start") .setHeader(InfinispanConstants.OPERATION, InfinispanConstants.QUERY) .setHeader(InfinispanConstants.QUERY_BUILDER, new InfinispanQueryBuilder() { public Query build(QueryFactory<Query> queryFactory) { return queryFactory.from(User.class).having("name").like("%abc%") .toBuilder().build(); } }) .to("infinispan://localhost?cacheContainer=#cacheManager&cacheName=remote_query_cache") ;
Using Blueprint and Java
Java RemoteCacheManagerFactory
class:
public class RemoteCacheManagerFactory { ConfigurationBuilder clientBuilder; public RemoteCacheManagerFactory(String hostname, int port) { clientBuilder = new ConfigurationBuilder(); clientBuilder.addServer() .host(hostname).port(port); } public RemoteCacheManager newRemoteCacheManager() { return new RemoteCacheManager(clientBuilder.build()); } }Java
InfinispanQueryExample
class:
public class InfinispanQueryExample { public InfinispanQueryBuilder getBuilder() { return new InfinispanQueryBuilder() { public Query build(QueryFactory<Query> queryFactory) { return queryFactory.from(User.class) .having("name") .like("%abc%") .toBuilder().build(); } } } }blueprint.xml:
<bean id=”remoteCacheManagerFactory” class=“com.jboss.datagrid.RemoteCacheManagerFactory”> <argument value=”localhost”/> <argument value="11222”/> </bean> <bean id=”cacheManager” factory-ref=”remoteCacheManagerFactory” factory-method=“newRemoteCacheManager”> </bean> <bean id="queryBuilder" class="org.example.com.InfinispanQueryExample"/> <camelContext id="route" xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="direct:start"/> <setHeader headerName="CamelInfinispanOperation"> <constant>CamelInfinispanOperationQuery</constant> </setHeader> <setHeader headerName="CamelInfinispanQueryBuilder"> <method ref="queryBuilder" method="getBuilder"/> </setHeader> <to uri="infinispan://localhost?cacheContainer=#cacheManager&cacheName=remote_query_cache"/> </route> </camelContext>
The
remote_query_cache
is an arbitrary name for a cache that holds the data, and the results of the query will be a list of domain objects stored as a CamelInfinispanOperationResult
header.
In addition, there are the following requirements:
- The
RemoteCacheManager
must be configured to useProtoStreamMarshaller
. - The
ProtoStreamMarshaller
must be registered with theRemoteCacheManager
's serialization context. - The .proto descriptors for domain objects must be registered with the remote JBoss Data Grid server.
For more details on how to setup a
RemoteCacheManager
, see the Remote Querying
section of the Red Hat JBoss Data Grid Infinispan Query Guide.