Search in sources :

Example 31 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class ClientFunctionTimeoutRegressionTest method createClientCache.

private void createClientCache(String hostName, Integer port, Integer timeout) {
    if (timeout > 0) {
        System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "CLIENT_FUNCTION_TIMEOUT", String.valueOf(timeout));
    }
    Properties props = new Properties();
    props.setProperty(LOCATORS, "");
    props.setProperty(MCAST_PORT, "0");
    ClientCacheFactory ccf = new ClientCacheFactory(props);
    ccf.addPoolServer(hostName, port);
    clientCache = (InternalClientCache) ccf.create();
    ClientRegionFactory<String, String> crf = clientCache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY);
    crf.create(REGION_NAME);
}
Also used : Properties(java.util.Properties) DistributedRestoreSystemProperties(org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory)

Example 32 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class CacheServerTestUtil method createCacheClientFromXmlN.

public static void createCacheClientFromXmlN(URL url, String poolName, String durableClientId, int timeout, Boolean addControlListener) {
    ClientCacheFactory ccf = new ClientCacheFactory();
    try {
        File cacheXmlFile = new File(url.toURI().getPath());
        ccf.set(CACHE_XML_FILE, cacheXmlFile.toURI().getPath());
    } catch (URISyntaxException e) {
        throw new ExceptionInInitializerError(e);
    }
    ccf.set(MCAST_PORT, "0");
    ccf.set(DURABLE_CLIENT_ID, durableClientId);
    ccf.set(DURABLE_CLIENT_TIMEOUT, String.valueOf(timeout));
    ccf.set(LOG_FILE, "abs_client_system.log");
    ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
    cache = (Cache) ccf.create();
    expected = IgnoredException.addIgnoredException("java.net.ConnectionException||java.net.SocketException");
    pool = (PoolImpl) PoolManager.find(poolName);
}
Also used : URISyntaxException(java.net.URISyntaxException) File(java.io.File) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory)

Example 33 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory 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");
// }
}
Also used : Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) ClientCache(org.apache.geode.cache.client.ClientCache) Properties(java.util.Properties) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 34 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class PdxQueryDUnitTest method testPdxInstanceWithMethodButNoField.

/**
   * Test to query a field that is not present in the Pdx object but has a get method
   * 
   * @throws CacheException
   */
@Test
public void testPdxInstanceWithMethodButNoField() throws CacheException {
    final Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    final VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    final String name = "/" + regionName;
    final String[] qs = { "select * from " + name + " where status = 'active'", "select status from " + name + " where id >= 5" };
    // Start server1
    final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start server2
    final int port2 = (Integer) vm1.invoke(new SerializableCallable("Create Server2") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start server3
    final int port3 = (Integer) vm2.invoke(new SerializableCallable("Create Server3") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // create client
    vm3.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
            cf.addPoolServer(NetworkUtils.getServerHostName(vm1.getHost()), port2);
            cf.addPoolServer(NetworkUtils.getServerHostName(vm2.getHost()), port3);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
            return null;
        }
    });
    vm3.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            QueryService remoteQueryService = null;
            // Execute query remotely
            try {
                remoteQueryService = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            for (int i = 0; i < qs.length; i++) {
                try {
                    SelectResults sr = (SelectResults) remoteQueryService.newQuery(qs[i]).execute();
                    assertEquals(5, sr.size());
                } catch (Exception e) {
                    Assert.fail("Failed executing " + qs[i], e);
                }
            }
            return null;
        }
    });
    // create index
    vm0.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            QueryService qs = null;
            try {
                qs = getCache().getQueryService();
                qs.createIndex("status", "status", name);
            } catch (Exception e) {
                Assert.fail("Exception getting query service ", e);
            }
            return null;
        }
    });
    // create client
    vm3.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            QueryService remoteQueryService = null;
            // Execute query remotely
            try {
                remoteQueryService = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            for (int i = 0; i < qs.length; i++) {
                try {
                    SelectResults sr = (SelectResults) remoteQueryService.newQuery(qs[i]).execute();
                    assertEquals(5, sr.size());
                } catch (Exception e) {
                    Assert.fail("Failed executing " + qs[i], e);
                }
            }
            return null;
        }
    });
    Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
Also used : Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 35 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class PdxQueryDUnitTest method testLocalMapInPeerTypePdxRegistry.

/**
   * In PeerTypeRegistration when a PdxType is updated, a local map of class => PdxTypes is
   * populated. This map is used to search a field for a class in different versions (PdxTypes) This
   * test verifies that the map is being updated by the cacheListener
   *
   * @throws CacheException
   */
@Test
public void testLocalMapInPeerTypePdxRegistry() throws CacheException {
    final Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    final VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    final String name = "/" + regionName;
    final String[] qs = { "select * from " + name + " where pdxStatus = 'active'", "select pdxStatus from " + name + " where id > 4" };
    // Start server1
    final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start server2
    final int port2 = (Integer) vm1.invoke(new SerializableCallable("Create Server2") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // client1 loads version 1 objects on server1
    vm2.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
            // Load version 1 objects
            for (int i = 0; i < numberOfEntries; i++) {
                PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
                pdxInstanceFactory.writeInt("id", i);
                pdxInstanceFactory.writeString("pdxStatus", (i % 2 == 0 ? "active" : "inactive"));
                PdxInstance pdxInstance = pdxInstanceFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            return null;
        }
    });
    // client 2 loads version 2 objects on server2
    vm3.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(vm1.getHost()), port2);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
            // Load version 2 objects
            for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
                PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
                pdxInstanceFactory.writeInt("id", i);
                pdxInstanceFactory.writeString("status", (i % 2 == 0 ? "active" : "inactive"));
                PdxInstance pdxInstance = pdxInstanceFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            return null;
        }
    });
    // on server 1 verify local map in PeerTypeRegistration has fields
    vm0.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            TypeRegistration registration = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.").getPdxRegistry().getTypeRegistration();
            Assert.assertTrue(registration instanceof PeerTypeRegistration);
            Map<String, Set<PdxType>> m = ((PeerTypeRegistration) registration).getClassToType();
            assertEquals(1, m.size());
            assertEquals("PdxVersionedNewPortfolio", m.keySet().iterator().next());
            assertEquals(2, m.values().iterator().next().size());
            for (PdxType p : m.values().iterator().next()) {
                assertEquals("PdxVersionedNewPortfolio", p.getClassName());
            }
            return null;
        }
    });
    // on server 2 verify local map in PeerTypeRegistration has fields
    vm1.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            TypeRegistration registration = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.").getPdxRegistry().getTypeRegistration();
            Assert.assertTrue(registration instanceof PeerTypeRegistration);
            Map<String, Set<PdxType>> m = ((PeerTypeRegistration) registration).getClassToType();
            assertEquals(1, m.size());
            assertEquals("PdxVersionedNewPortfolio", m.keySet().iterator().next());
            assertEquals(2, m.values().iterator().next().size());
            for (PdxType p : m.values().iterator().next()) {
                assertEquals("PdxVersionedNewPortfolio", p.getClassName());
            }
            return null;
        }
    });
    Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxType(org.apache.geode.pdx.internal.PdxType) PeerTypeRegistration(org.apache.geode.pdx.internal.PeerTypeRegistration) ClientTypeRegistration(org.apache.geode.pdx.internal.ClientTypeRegistration) TypeRegistration(org.apache.geode.pdx.internal.TypeRegistration) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) PeerTypeRegistration(org.apache.geode.pdx.internal.PeerTypeRegistration) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) Map(java.util.Map) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)87 ClientCache (org.apache.geode.cache.client.ClientCache)65 Test (org.junit.Test)45 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)44 VM (org.apache.geode.test.dunit.VM)42 Host (org.apache.geode.test.dunit.Host)40 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)40 Region (org.apache.geode.cache.Region)37 Properties (java.util.Properties)23 QueryService (org.apache.geode.cache.query.QueryService)22 SelectResults (org.apache.geode.cache.query.SelectResults)22 CacheServer (org.apache.geode.cache.server.CacheServer)22 Cache (org.apache.geode.cache.Cache)19 CacheException (org.apache.geode.cache.CacheException)14 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)13 IOException (java.io.IOException)12 IgnoredException (org.apache.geode.test.dunit.IgnoredException)12 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)11 PdxInstance (org.apache.geode.pdx.PdxInstance)10