Java Code Examples for org.ehcache.StateTransitionException

Following code examples demonstrate how to use org.ehcache.StateTransitionExceptionfrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.StateTransitionExceptionand various code implementation of this class.

  public void testClusteredCacheWithXA() throws Exception {
    TransactionManagerServices.getConfiguration().setJournal("null");

    BitronixTransactionManager transactionManager =
        TransactionManagerServices.getTransactionManager();

    PersistentCacheManager persistentCacheManager = null;
    try {
      CacheManagerBuilder.newCacheManagerBuilder()
          .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
          .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB))
              )
                  .add(new XAStoreConfiguration("xaCache"))
                  .build()
          )
          .build(true);
    } catch (StateTransitionException e) {
      assertThat(e.getCause().getCause().getMessage(), is("Unsupported resource type : interface org.ehcache.clustered.client.config.DedicatedClusteredResourcePool"));
    }

    transactionManager.shutdown();
  } 


  public void testRecoverPersistentCacheFailsWhenConfiguringIncompatibleClass() throws Exception {
    {
      PersistentCacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
          .with(new CacheManagerPersistenceConfiguration(new File(getStoragePath(), "testRecoverPersistentCacheFailsWhenConfiguringIncompatibleClass")))
          .withCache("persistentCache",
              CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                  newResourcePoolsBuilder()
                      .heap(1, MemoryUnit.MB)
                      .offheap(2, MemoryUnit.MB)
                      .disk(5, MemoryUnit.MB, true)
                  )
          ).build(true);


      cacheManager.close();
    }

    {
        PersistentCacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .with(new CacheManagerPersistenceConfiguration(new File(getStoragePath(), "testRecoverPersistentCacheFailsWhenConfiguringIncompatibleClass")))
            .withCache("persistentCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, Serializable.class,
                    newResourcePoolsBuilder()
                        .heap(1, MemoryUnit.MB)
                        .offheap(2, MemoryUnit.MB)
                        .disk(5, MemoryUnit.MB, true)
                    )
            ).build();

      try {
        cacheManager.init();
        fail("expected StateTransitionException");
      } catch (StateTransitionException ste) {
        Throwable rootCause = findRootCause(ste);
        assertThat(rootCause, instanceOf(IllegalArgumentException.class));
        assertThat(rootCause.getMessage(), equalTo("Persisted value type 'java.lang.String' is not the same as the configured value type 'java.io.Serializable'"));
      }
    }
  } 

  public void testDiskTierWithoutPersistenceServiceFailsWithClearException() {
    try {
      newCacheManagerBuilder().withCache("failing", newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder()
          .heap(5, EntryUnit.ENTRIES)
          .disk(5, MemoryUnit.MB)).build()).build(true);
      fail("Should not initialize");
    } catch (StateTransitionException e) {
      assertThat(e.getCause().getCause().getMessage(), containsString("No service found for persistable resource: disk"));
    }
  } 

  public void testDestroyCacheManagerWithMultipleClients() throws CachePersistenceException {
    PersistentCacheManager persistentCacheManager1 = clusteredCacheManagerBuilder.build(true);
    PersistentCacheManager persistentCacheManager2 = clusteredCacheManagerBuilder.build(true);

    persistentCacheManager1.close();

    try {
      persistentCacheManager1.destroy();
      fail("StateTransitionException expected");
    } catch (StateTransitionException e) {
      assertThat(e.getMessage(), is("Couldn't acquire cluster-wide maintenance lease"));
    }

    assertThat(persistentCacheManager1.getStatus(), is(Status.UNINITIALIZED));

    assertThat(persistentCacheManager2.getStatus(), is(Status.AVAILABLE));

    Cache<Long, String> cache = persistentCacheManager2.createCache("test", newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
            .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))));

    cache.put(1L, "One");

    assertThat(cache.get(1L), is("One"));

    persistentCacheManager2.close();
  } 

  public void testDestroyCacheManagerDoesNotAffectsExistingCacheWithExistingClientsConnected() throws CachePersistenceException {

    CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilder = clusteredCacheManagerBuilder
        .withCache("test", newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))));

    PersistentCacheManager persistentCacheManager1 = cacheManagerBuilder.build(true);
    PersistentCacheManager persistentCacheManager2 = cacheManagerBuilder.build(true);

    persistentCacheManager1.close();
    try {
      persistentCacheManager1.destroy();
      fail("StateTransitionException expected");
    } catch (StateTransitionException e) {
      assertThat(e.getMessage(), is("Couldn't acquire cluster-wide maintenance lease"));
    }

    Cache<Long, String> cache = persistentCacheManager2.getCache("test", Long.class, String.class);

    cache.put(1L, "One");

    assertThat(cache.get(1L), is("One"));

    persistentCacheManager2.close();
  } 

    public void test() {
        try {
            createCacheManager(10);
        } catch(StateTransitionException e) {
            assertEquals(InvalidOperationException.class, getRootCause(e).getClass());
        }
        createCacheManager(100);
    } 

  private void assertFailingTransitionGoesToLowestStatus(InternalCache cache) throws Exception {
    final LifeCycled mock = mock(LifeCycled.class);
    cache.addHook(mock);
    doThrow(new Exception()).when(mock).init();
    try {
      cache.init();
      fail();
    } catch (StateTransitionException e) {
      assertThat(cache.getStatus(), CoreMatchers.is(Status.UNINITIALIZED));
    }

    reset(mock);
    cache.init();
    assertThat(cache.getStatus(), is(Status.AVAILABLE));
    doThrow(new Exception()).when(mock).close();
    try {
      cache.close();
      fail();
    } catch (StateTransitionException e) {
      assertThat(cache.getStatus(), is(Status.UNINITIALIZED));
    }

  } 

    public StateTransitionException failed(Throwable t) {
      if (st.done()) {
        if (t != null) {
          throw (AssertionError) new AssertionError("Throwable cannot be thrown if Transition is done.").initCause(t);
        }
        return null;
      }
      st.failed();
      if (t == null) {
        return null;
      }
      logger.error("{} failed.", action);
      if(t instanceof StateTransitionException) {
        return (StateTransitionException) t;
      }
      return new StateTransitionException(t);
    } 

    public void succeeded() {
      try {
        switch(st.to()) {
          case AVAILABLE:
            runInitHooks();
            break;
          case UNINITIALIZED:
            maintenanceLease = null;
            runCloseHooks();
            break;
          case MAINTENANCE:
            maintenanceLease = thread;
            break;
          default:
            throw new IllegalArgumentException("Didn't expect that enum value: " + st.to());
        }
        st.succeeded();
      } catch (Exception e) {
        st.failed();
        throw new StateTransitionException(e);
      }

      try {
        fireTransitionEvent(st.from().toPublicStatus(), st.to().toPublicStatus());
      } finally {
        maintenanceLease = thread;
        logger.debug("{} successful.", action);
      }
    } 

    switch (to) {

      case AVAILABLE: {
        NmsAgentEntity nmsAgentEntity;
        try {
          nmsAgentEntity = nmsAgentFactory.retrieve();
        } catch (EntityNotFoundException e) {
          throw (AssertionError) new AssertionError("Entity " + NmsAgentEntity.class.getSimpleName() + " not found").initCause(e.getCause());
        }
        nmsAgentService = new NmsAgentService(nmsAgentEntity);
        nmsAgentService.setOperationTimeout(configuration.getManagementCallTimeoutSec(), TimeUnit.SECONDS);
        nmsAgentService.setManagementRegistry(managementRegistryService);
        nmsAgentService.setManagementCallExecutor(new LoggingExecutor(
            managementCallExecutor,
            LoggerFactory.getLogger(getClass().getName() + ".managementCallExecutor")));

        try {
          nmsAgentService.init();
          nmsAgentService.setTags(managementRegistryService.getConfiguration().getTags());
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          throw new StateTransitionException(e);
        } catch (ExecutionException e) {
          throw new StateTransitionException(e.getCause());
        } catch (TimeoutException e) {
          throw new StateTransitionException(e);
        }

        break;
      }


      case UNINITIALIZED: {
        this.cacheManager.deregisterListener(this);
        break;
      }

      case MAINTENANCE:
        break;

      default:
        throw new AssertionError("Unsupported state: " + to);
    } 

Advertisement
Javadoc
Indicates that a state transition failed.
Advertisement