1.15.3. Example

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

Cache Service example

The cache service delegates most of the work to the CacheServiceManaged class by using the @ManagedBy annotation. At runtime when a new cache is created, it calls the CacheServiceManaged class in order to let the CacheServiceManaged object register the cache.

@ManagedBy(CacheServiceManaged.class)

public class CacheServiceImpl implements CacheService {
  CacheServiceManaged managed;
  ...
  synchronized private ExoCache createCacheInstance(String region) throws Exception {
    ...
    if (managed != null) {
      managed.registerCache(simple);
    }
    ...
  }
}

The ExoCache interface is annotated to define its management view. The @NameTemplate is used to produce object name values when ExoCache instances are registered.

@Managed

@NameTemplate({@Property(key="service", value="cache"), @Property(key="name", value="{Name}")})
@ManagedDescription("Exo Cache")
public interface ExoCache {
  @Managed
  @ManagedName("Name")
  @ManagedDescription("The cache name")
  public String getName();
  @Managed
  @ManagedName("Capacity")
  @ManagedDescription("The maximum capacity")
  public int getMaxSize();
  @Managed
  @ManagedDescription("Evict all entries of the cache")
  public void clearCache() throws Exception;
  ...
}

The CacheServiceManaged is the glue code between the CacheService and the management view. The main reason is that only eXo services are registered automatically against the management view. Any other managed bean must be registered manually for now. Therefore, it needs to know about the management layer via the management context. The management context allows an object implementing the ManagementAware interface to receive a context to perform further registration of managed objects.

@Managed

public class CacheServiceManaged implements ManagementAware {
  /** . */
  private ManagementContext context;
  /** . */
  private CacheServiceImpl cacheService;
  public CacheServiceManaged(CacheServiceImpl cacheService) {
    this.cacheService = cacheService;
    //
    cacheService.managed = this;
  }
  public void setContext(ManagementContext context) {
    this.context = context;
  }
  void registerCache(ExoCache cache) {
    if (context != null) {
      context.register(cache);
    }
  }
}

Note

When the object of an annotated component is created, it will be registered to MBean server. It does not mean you always obtain an MBean object right after the server started. A component instance is created at its first called, or if it is required during creation of another component. Otherwise to have your component created (and registered to MBean server automatically) during server startup, you may implement org.picocontainer.Startable, as the example below.

import org.picocontainer.Startable;


@Managed
@ManagedDescription("a sample MBean")
@NameTemplate({ @Property(key = "name", value = "MyService"),
    @Property(key = "view", value = "samples"),
    @Property(key = "type", value = "com.acme") })
public class MyService implements Startable {
    ...
    public void start() {}
    public void stop() {}
    ...
}
Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus