예제 #1
0
 public static NamedCache getCache(String s) {
   if (!cache.containsKey(s)) {
     CacheFactory.ensureCluster();
     NamedCache c = CacheFactory.getCache(s);
     cache.put(s, c);
   }
   return (cache.get(s));
 }
 @Test
 public void createCacheTest() {
   NamedCache cache1 = CacheFactory.getCache("local-noclassloader-cache", null);
   assertNotNull(cache1);
   NamedCache cache2 =
       CacheFactory.getCache("local-withclassloader-cache", this.getClass().getClassLoader());
   assertNotNull(cache2);
 }
  public static void main(String[] args) {
    long empId = 190L; // emp 190 - Timothy Gates

    NamedCache employees = CacheFactory.getCache("Employees");

    Employees emp = (Employees) employees.get(empId);

    System.out.println(
        "Employee "
            + emp.getFirstName()
            + " "
            + emp.getLastName()
            + ", salary = $"
            + emp.getSalary());

    NamedCache cache = CacheFactory.getCache("Departments");

    Departments dept = (Departments) cache.get(Long.valueOf(10));
    System.out.println("Department " + dept.getDepartmentName());

    IsNotNullFilter filter = new IsNotNullFilter("getDepartmentId");
    Set departmentsSet = cache.entrySet(filter);

    System.out.println("count departments " + departmentsSet.size());

    Departments deptNew = new Departments();
    deptNew.setDepartmentId(Long.valueOf(2));
    deptNew.setDepartmentName("Edwin");
    deptNew.setLocationId(Long.valueOf(1700));
    deptNew.setManagerId(Long.valueOf(200));
    cache.put(Long.valueOf(2), deptNew);

    dept = (Departments) cache.get(Long.valueOf(2));
    System.out.println("Department " + dept.getDepartmentName());

    cache.remove(Long.valueOf(2));

    try {
      Class department = Class.forName("nl.amis.jpa.model.Departments");
      Object departmentInst = department.newInstance();
      Method m = department.getMethod("setDepartmentId", new Class[] {Long.class});
      m.invoke(departmentInst, new Object[] {Long.valueOf(37)});
      m = department.getMethod("setDepartmentName", new Class[] {String.class});
      m.invoke(departmentInst, new Object[] {"hoihoi"});
      m = department.getMethod("setLocationId", new Class[] {Long.class});
      m.invoke(departmentInst, new Object[] {Long.valueOf(1700)});
      cache.put(Long.valueOf(37), departmentInst);
      System.out.println(department.getName());

    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (Exception eee) {
      eee.printStackTrace();
    }
  }
예제 #4
0
 public void tearDown() throws Exception {
   log.info("Relasing cache " + nc.getCacheName());
   try {
     CacheFactory.releaseCache(nc);
     nc = null;
   } catch (IllegalStateException e) {
     if (e.getMessage() != null && e.getMessage().indexOf("Cache is already released") >= 0) {
       log.info("This cache was already destroyed by another instance");
     }
   }
   CacheFactory.shutdown();
   log.info("Cache factory was shut down.");
 }
 @Test
 public void putNullValuesTest() {
   NamedCache cache1 = CacheFactory.getCache("local-noclassloader-cache", null);
   cache1.put("a", "b");
   cache1.put("a", null);
   cache1.put(null, null);
 }
예제 #6
0
 @Override
 public int getLocalSize() {
   if (nc != null) {
     synchronized (this) {
       if (mBeanServer == null || jmxCacheName == null) {
         int nodeId = CacheFactory.getCluster().getLocalMember().getId();
         jmxCacheName = String.format(CACHE_JMX_NAME_TEMPLATE, serviceName, cacheName, nodeId);
         mBeanServer = MBeanHelper.findMBeanServer();
       }
     }
     try {
       AttributeList list =
           mBeanServer.getAttributes(
               new ObjectName(jmxCacheName), new String[] {"Units", "UnitFactor"});
       return ((Integer) ((Attribute) list.get(0)).getValue())
           * ((Integer) ((Attribute) list.get(1)).getValue());
     } catch (Exception e) {
       log.warn("Failed to retrieve JMX info from object " + jmxCacheName + "\n" + e);
       return -1;
     }
   } else {
     log.info("Cache is not available.");
     return -1;
   }
 }
예제 #7
0
 private static TxSuperviser createSuperviser(String systemCache) {
   NamedCache cache = CacheFactory.getCache(systemCache);
   TxSuperviser sv = new TxSuperviser(cache);
   // TODO how to manage sweepers;
   //		TxSweeper txSweeper = new TxSweeper(sv);
   //		txSweeper.start();
   return sv;
 }
 public LockFactory createLockFactory(
     String path, String subContex, String subIndex, CompassSettings settings)
     throws SearchEngineException {
   String connection = settings.getSetting(CompassEnvironment.CONNECTION);
   NamedCache cache = CacheFactory.getCache(path);
   return new InvocableCoherenceLockFactory(
       cache, connection + "/" + subContex + "/" + subIndex + "/");
 }
예제 #9
0
 @Override
 public void setUp(
     String configuration, boolean isLocal, int nodeIndex, TypedProperties confAttributes)
     throws Exception {
   cacheName =
       confAttributes.containsKey(PROP_CACHE)
           ? confAttributes.getProperty(PROP_CACHE)
           : DEFAULT_CACHE_NAME;
   serviceName =
       confAttributes.containsKey(PROP_SERVICE)
           ? confAttributes.getProperty(PROP_SERVICE)
           : DEFAULT_SERVICE_NAME;
   CacheFactory.setConfigurableCacheFactory(new DefaultConfigurableCacheFactory(configuration));
   nc = CacheFactory.getCache(cacheName);
   log.debug("CacheFactory.getClusterConfig(): \n" + CacheFactory.getClusterConfig());
   log.debug(
       "CacheFactory.getConfigurableCacheFactoryConfig(): \n"
           + CacheFactory.getConfigurableCacheFactoryConfig());
   log.info("Started Coherence cache " + nc.getCacheName());
 }
예제 #10
0
  @Test
  public void simpleCacheTest() {
    String key = "k1";
    String value = "Hello World!";

    NamedCache cache = CacheFactory.getCache("local-example-cache");

    Object x = cache.put(key, value);
    System.out.println("x:" + x);
    System.out.println((String) cache.get(key));

    cache.put("k2", new Date());
  }
예제 #11
0
  @Test
  public void shouldQueryUsingCustomFilter() throws IOException {
    Map<TradeKey, Trade> trades = DataLoader.loadTradeData("trades.csv");
    NamedCache tradeCache = CacheFactory.getCache("TradeCache");
    tradeCache.putAll(trades);

    Filter filter = new CustomFilter();

    Set<TradeKey> result = tradeCache.keySet(filter);

    Assert.assertEquals(result.size(), 1);

    Trade tradeReturned = (Trade) tradeCache.get(result.iterator().next());
    Assert.assertEquals(tradeReturned.getInstrumentType(), "CDS");
  }
예제 #12
0
  @Test
  public void dynamicallySetCachePropertiesTest() throws Exception {
    String key = "key";
    String value = "Hello";

    String cacheName = "local-example-cache";
    NamedCache cache = CacheFactory.getCache(cacheName);

    // grab the backing map
    CacheService service = cache.getCacheService();
    LocalCache backingMap =
        (LocalCache)
            ((DefaultConfigurableCacheFactory.Manager) service.getBackingMapManager())
                .getBackingMap(cacheName);

    // change the size and time-to-live of the cache
    backingMap.setExpiryDelay(1000); // millisec
    backingMap.setHighUnits(10);

    /*
     * We can safely ignore the deprecated warnings, the public api will remain avaible
     * in the LocalCache, see also:
     * https://forums.oracle.com/forums/thread.jspa?messageID=2148979&#2148979
     */

    assertEquals(1000, backingMap.getExpiryDelay());
    assertEquals(10, backingMap.getHighUnits());

    // verify the max size of the cache
    for (int i = 0; i < 20; i++) {
      cache.put(key + i, value + i);
    }
    int cacheSize = cache.entrySet().size();
    assertTrue("max highunits test", cacheSize <= 10);

    // verify the expiry of the cache
    Thread.sleep(1000);
    cache.put(key + 21, value + 21);
    int newCacheSize = cache.entrySet().size();
    assertEquals("expiry test", 1, newCacheSize);
  }
예제 #13
0
  public void init() throws Exception {
    super.init();

    cache = CacheFactory.getCache(name);
  }
예제 #14
0
  public void process() {

    System.setProperty("tangosol.pof.enabled", "false");
    //	    System.setProperty("tangosol.pof.config", "capacity-benchmark-pof-config.xml");
    System.setProperty("tangosol.coherence.cacheconfig", "tx-lite-test-cache-config.xml");
    System.setProperty("tangosol.coherence.distributed.localstorage", "false");

    try {
      final NamedCache cache = CacheFactory.getCache("t-objects");
      final ObjectGenerator<?, ?> generator = new SimpleDomainObjGenerator();
      //			final ObjectGenerator<?, ?> generator = new SimpleDomainObjGenerator(100);
      //			final ObjectGenerator<?, ?> generator = new SimpleDomainObjGenerator(400);

      //			cache.addIndex(new ReflectionExtractor("getA0"), false, null);
      //			cache.addIndex(new ReflectionExtractor("getAs"), false, null);

      //			System.out.println(CacheFactory.getClusterConfig().toString());

      //			long objectCount = 1000000;
      long objectCount = 100000;
      //			long objectCount = 200000;
      //			long objectCount = 10000;

      long rangeStart = 1000000;
      long rangeFinish = 1000000 + objectCount;

      println("Loading " + objectCount + " objects ...");
      loadObjects(cache, generator, rangeStart, rangeFinish);

      println("Loaded " + cache.size() + " objects");
      println(
          "Key binary size: "
              + ExternalizableHelper.toBinary(generator.generate(1, 2).keySet().iterator().next())
                  .length());
      println(
          "Value binary size: "
              + ExternalizableHelper.toBinary(generator.generate(1, 2).values().iterator().next())
                  .length());
      System.gc();
      println("Mem. usage " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());

      //			checkAccess(cache, new EqualsFilter("getA0", new DomainObjAttrib("?")));
      //			checkAccess(cache, new EqualsFilter(new ReflectionPofExtractor("a0"), new
      // DomainObjAttrib("?")));
      //			checkAccess(cache, new EqualsFilter("getAs", Collections.EMPTY_LIST));
      //			checkAccess(cache, new ContainsAnyFilter("getAs", Collections.singleton(new
      // DomainObjAttrib("?"))));

      //			ContinuousQueryCache view = new ContinuousQueryCache(cache, new
      // EqualsFilter("getHashSegment", 0), true);
      //			System.out.println("View size " + view.size());
      //
      //			view.addIndex(new ReflectionExtractor("getA0"), false, null);
      //            checkAccess(view, new EqualsFilter("getA0", new DomainObjAttrib("?")));
      //            checkAccess(view, new EqualsFilter("getA1", new DomainObjAttrib("?")));

      while (true) {
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 protected void setUp() throws Exception {
   cache = CacheFactory.getCache("luceneDirectory");
   cache.clear();
 }