Class CentralDogmaBeanFactory

java.lang.Object
com.linecorp.centraldogma.client.updater.CentralDogmaBeanFactory

@Named public class CentralDogmaBeanFactory extends Object
Creates a new bean instance that mirrors its properties from Central Dogma.

In the following example, mirroredFoo.getA() and getB() will return the latest known values retrieved from /myProject/myRepo/foo.json along with the latest revision in Central Dogma. If the latest values are not available yet, the revision will be set to null.


 > CentralDogma dogma = ...;

 > // Optionally, waits for the initial endpoints in order to fetch the first value without additional delay.
 > dogma.whenEndpointReady().get(10, TimeUnit.SECONDS);

 > CentralDogmaBeanFactory factory = new CentralDogmaBeanFactory(dogma, new ObjectMapper());
 > Foo mirroredFoo = factory.get(new Foo(), Foo.class);
 >
 > @CentralDogmaBean(project = "myProject",
 >                   repository = "myRepo",
 >                   path = "/foo.json")
 > public class Foo {
 >     private int a;
 >     private String b;
 >
 >     public int getA() { return a; }
 >     public void setA(int a) { this.a = a; }
 >     public String getB() { return b; }
 >     public void setB(String b) { this.b = b; }
 >     public Revision getRevision() { return null; }
 > }
 

In the following example, callback will be called when a property is updated with a new value:


 CentralDogmaBeanFactory factory = new CentralDogmaBeanFactory(dogma, new ObjectMapper());
 Consumer<Foo> fooUpdatedListener = (Foo f) -> {
     System.out.println("foo has updated to: " + f);
 };

 Foo mirroredFoo = factory.get(new Foo(), Foo.class, fooUpdatedListener);
 
  • Constructor Details

    • CentralDogmaBeanFactory

      @Inject public CentralDogmaBeanFactory(CentralDogma dogma, ObjectMapper objectMapper)
      Creates a new factory instance.
  • Method Details

    • get

      public <T> T get(T defaultValue, Class<T> beanType)
      Returns a newly-created bean instance with the settings specified by CentralDogmaBean annotation.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
    • get

      public <T> T get(T defaultValue, Class<T> beanType, Consumer<T> changeListener)
      Returns a newly-created bean instance with the settings specified by CentralDogmaBean annotation.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      changeListener - the Consumer of beanType, invoked when bean is updated. Will consume the new value of the bean.
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
    • get

      public <T> T get(T defaultValue, Class<T> beanType, Consumer<T> changeListener, CentralDogmaBeanConfig overrides)
      Returns a newly-created bean instance with some or all of its settings overridden.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      changeListener - the Consumer of beanType, invoked when bean is updated. Will consume the new value of the bean.
      overrides - the CentralDogmaBeanConfig whose properties will override the settings specified by the CentralDogmaBean annotation
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
    • get

      public <T> T get(T defaultValue, Class<T> beanType, Consumer<T> changeListener, long initialValueTimeout, TimeUnit initialValueTimeoutUnit) throws TimeoutException, InterruptedException
      Returns a newly-created bean instance with the settings specified by CentralDogmaBean annotation.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      changeListener - the Consumer of beanType, invoked when bean is updated. Will consume the new value of the bean.
      initialValueTimeout - when a value larger than zero given to this argument, this method tries to wait for the initial value to be fetched until the timeout
      initialValueTimeoutUnit - the TimeUnit of initialValueTimeout
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
      Throws:
      TimeoutException - when initialValueTimeoutMillis is positive and it failed to obtain the initial value within configured timeout
      InterruptedException - when initialValueTimeoutMillis is positive and it got interrupted while waiting for the initial value
    • get

      public <T> T get(T defaultValue, Class<T> beanType, CentralDogmaBeanConfig overrides)
      Returns a newly-created bean instance with some or all of its settings overridden.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      overrides - the CentralDogmaBeanConfig whose properties will override the settings specified by the CentralDogmaBean annotation
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
    • get

      public <T> T get(T defaultValue, Class<T> beanType, long initialValueTimeout, TimeUnit initialValueTimeoutUnit) throws TimeoutException, InterruptedException
      Returns a newly-created bean instance with the settings specified by CentralDogmaBean annotation.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      initialValueTimeout - when a value larger than zero given to this argument, this method tries to wait for the initial value to be fetched until the timeout
      initialValueTimeoutUnit - the TimeUnit of initialValueTimeout
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
      Throws:
      TimeoutException - when initialValueTimeoutMillis is positive and it failed to obtain the initial value within configured timeout
      InterruptedException - when initialValueTimeoutMillis is positive and it got interrupted while waiting for the initial value
    • get

      public <T> T get(T defaultValue, Class<T> beanType, CentralDogmaBeanConfig overrides, long initialValueTimeout, TimeUnit initialValueTimeoutUnit) throws TimeoutException, InterruptedException
      Returns a newly-created bean instance with some or all of its settings overridden.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      overrides - the CentralDogmaBeanConfig whose properties will override the settings specified by the CentralDogmaBean annotation
      initialValueTimeout - when a value larger than zero given to this argument, this method tries to wait for the initial value to be fetched until the timeout
      initialValueTimeoutUnit - the TimeUnit of initialValueTimeout
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
      Throws:
      TimeoutException - when initialValueTimeoutMillis is positive and it failed to obtain the initial value within configured timeout
      InterruptedException - when initialValueTimeoutMillis is positive and it got interrupted while waiting for the initial value
    • get

      public <T> T get(T defaultValue, Class<T> beanType, Consumer<T> changeListener, CentralDogmaBeanConfig overrides, long initialValueTimeout, TimeUnit initialValueTimeoutUnit) throws TimeoutException, InterruptedException
      Returns a newly-created bean instance with some or all of its settings overridden.
      Parameters:
      defaultValue - a Java bean annotated with CentralDogmaBean. The default value is used before initialization.
      beanType - the type of bean
      changeListener - the Consumer of beanType, invoked when bean is updated. Will consume the new value of the bean.
      overrides - the CentralDogmaBeanConfig whose properties will override the settings specified by the CentralDogmaBean annotation
      initialValueTimeout - when a value larger than zero given to this argument, this method tries to wait for the initial value to be fetched until the timeout
      initialValueTimeoutUnit - the TimeUnit of initialValueTimeout
      Returns:
      a new Java bean whose getters return the latest known values mirrored from Central Dogma
      Throws:
      TimeoutException - when initialValueTimeoutMillis is positive and it failed to obtain the initial value within configured timeout
      InterruptedException - when initialValueTimeoutMillis is positive and it got interrupted while waiting for the initial value