use of org.apache.geode.cache.CacheFactory in project geode by apache.
the class CacheUtils method init.
private static void init() throws TimeoutException, CacheWriterException, GatewayException, RegionExistsException {
if (GemFireCacheImpl.getInstance() == null) {
props.setProperty(MCAST_PORT, "0");
cache = (InternalCache) new CacheFactory(props).create();
} else {
cache = GemFireCacheImpl.getInstance();
}
ds = cache.getDistributedSystem();
qs = cache.getQueryService();
}
use of org.apache.geode.cache.CacheFactory in project geode by apache.
the class PdxQueryDUnitTest method testPutAllWithIndexes.
/**
* This test creates 3 cache servers with a PR and one client which puts some PDX values in PR and
* runs a query. This was failing randomly in a POC.
*/
@Test
public void testPutAllWithIndexes() {
final String name = "testRegion";
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
final Properties config = new Properties();
config.setProperty("locators", "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
// Start server
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Cache cache = new CacheFactory(config).create();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
prfactory.setRedundantCopies(0);
factory.setPartitionAttributes(prfactory.create());
cache.createRegionFactory(factory.create()).create(name);
try {
startCacheServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
// Create Index on empty region
try {
cache.getQueryService().createIndex("myFuncIndex", "intId", "/" + name);
} catch (Exception e) {
Assert.fail("index creation failed", e);
}
}
});
// Start server
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Cache cache = new CacheFactory(config).create();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
prfactory.setRedundantCopies(0);
factory.setPartitionAttributes(prfactory.create());
cache.createRegionFactory(factory.create()).create(name);
try {
startCacheServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
}
});
// Start server
vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Cache cache = new CacheFactory(config).create();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
prfactory.setRedundantCopies(0);
factory.setPartitionAttributes(prfactory.create());
cache.createRegionFactory(factory.create()).create(name);
try {
startCacheServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
}
});
// Create client region
final int port = vm0.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm2.getHost());
vm3.invoke(new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
Properties config = new Properties();
config.setProperty("mcast-port", "0");
ClientCache cache = new ClientCacheFactory(config).addPoolServer(host0, port).setPoolPRSingleHopEnabled(true).setPoolSubscriptionEnabled(true).create();
AttributesFactory factory = new AttributesFactory();
cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
}
});
vm3.invoke(new CacheSerializableRunnable("putAll() test") {
@Override
public void run2() throws CacheException {
try {
ClientCache cache = new ClientCacheFactory().create();
Region region = cache.getRegion(name);
QueryService queryService = cache.getQueryService();
String k;
for (int x = 0; x < 285; x++) {
k = Integer.valueOf(x).toString();
PortfolioPdx v = new PortfolioPdx(x, x);
region.put(k, v);
}
Query q = queryService.newQuery("SELECT DISTINCT * from /" + name + " WHERE ID = 2");
SelectResults qResult = (SelectResults) q.execute();
for (Object o : qResult.asList()) {
System.out.println("o = " + o);
}
} catch (Exception e) {
Assert.fail("Querying failed: ", e);
}
}
});
Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
// }
}
use of org.apache.geode.cache.CacheFactory in project geode by apache.
the class PdxCopyOnReadQueryJUnitTest method testCopyOnReadPdxSerialization.
@Test
public void testCopyOnReadPdxSerialization() throws Exception {
List<String> classes = new ArrayList<String>();
classes.add(PortfolioPdx.class.getCanonicalName());
ReflectionBasedAutoSerializer serializer = new ReflectionBasedAutoSerializer(classes.toArray(new String[0]));
CacheFactory cf = new CacheFactory();
cf.setPdxSerializer(serializer);
cf.setPdxReadSerialized(false);
cf.set(MCAST_PORT, "0");
cache = cf.create();
cache.setCopyOnRead(true);
Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create("SimpleObjects");
Region duplicates = cache.createRegionFactory(RegionShortcut.REPLICATE).create("SimpleObjects_Duplicates");
for (int i = 0; i < 10; i++) {
PortfolioPdx t = new PortfolioPdx(i);
region.put(i, t);
duplicates.put(i, t);
}
QueryService qs = cache.getQueryService();
SelectResults rs = (SelectResults) qs.newQuery("select * from /SimpleObjects").execute();
assertEquals(10, rs.size());
Query query = qs.newQuery("select * from /SimpleObjects_Duplicates s where s in ($1)");
SelectResults finalResults = (SelectResults) query.execute(new Object[] { rs });
assertEquals(10, finalResults.size());
}
use of org.apache.geode.cache.CacheFactory in project geode by apache.
the class RestInterfaceJUnitTest method setupGemFire.
@Before
public void setupGemFire() {
AgentUtil agentUtil = new AgentUtil(GemFireVersion.getGemFireVersion());
if (agentUtil.findWarLocation("geode-web-api") == null) {
fail("unable to locate geode-web-api WAR file");
}
if (gemfireCache == null) {
gemfireProperties = (gemfireProperties != null ? gemfireProperties : new Properties());
gemfireCache = new CacheFactory().setPdxSerializer(new ReflectionBasedAutoSerializer(Person.class.getName().replaceAll("\\$", "."))).setPdxReadSerialized(true).setPdxIgnoreUnreadFields(false).set("name", getClass().getSimpleName()).set(MCAST_PORT, "0").set(LOG_LEVEL, "config").set(HTTP_SERVICE_BIND_ADDRESS, "localhost").set(HTTP_SERVICE_PORT, String.valueOf(getHttpServicePort())).set(START_DEV_REST_API, "true").create();
RegionFactory<String, Object> peopleRegionFactory = gemfireCache.createRegionFactory();
peopleRegionFactory.setDataPolicy(DataPolicy.PARTITION);
peopleRegionFactory.setKeyConstraint(String.class);
peopleRegionFactory.setValueConstraint(Object.class);
people = peopleRegionFactory.create("People");
}
}
use of org.apache.geode.cache.CacheFactory in project geode by apache.
the class ClientServerRegisterInterestsDUnitTest method setupGemFireCacheServer.
private void setupGemFireCacheServer() {
Host localhost = Host.getHost(0);
gemfireServerVm = localhost.getVM(0);
serverPort.set(AvailablePortHelper.getRandomAvailableTCPPort());
gemfireServerVm.invoke(new SerializableRunnable() {
@Override
public void run() {
try {
Cache cache = new CacheFactory().set("name", "ClientServerRegisterInterestsTestGemFireServer").set(MCAST_PORT, "0").set(LOG_FILE, "clientServerRegisterInterestsTest.log").set(LOG_LEVEL, "config").create();
RegionFactory<String, String> regionFactory = cache.createRegionFactory();
regionFactory.setDataPolicy(DataPolicy.REPLICATE);
regionFactory.setKeyConstraint(String.class);
regionFactory.setValueConstraint(String.class);
Region<String, String> example = regionFactory.create("Example");
assertNotNull("The 'Example' Region was not properly configured and initialized!", example);
assertEquals("/Example", example.getFullPath());
assertEquals("Example", example.getName());
assertTrue(example.isEmpty());
example.put("1", "ONE");
assertFalse(example.isEmpty());
assertEquals(1, example.size());
CacheServer cacheServer = cache.addCacheServer();
cacheServer.setPort(serverPort.get());
cacheServer.setMaxConnections(10);
ClientSubscriptionConfig clientSubscriptionConfig = cacheServer.getClientSubscriptionConfig();
clientSubscriptionConfig.setCapacity(100);
clientSubscriptionConfig.setEvictionPolicy("entry");
cacheServer.start();
assertTrue("Cache Server is not running!", cacheServer.isRunning());
} catch (UnknownHostException ignore) {
throw new RuntimeException(ignore);
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to start the GemFire Cache Server listening on port (%1$d) due to IO error!", serverPort.get()), e);
}
}
});
}
Aggregations