Interface ReloadInformant


  • public interface ReloadInformant
    Informs when the server should be reloaded, during development.

    During development mode, all reload informants present in the server registry will be asked if the server should reload before serving each request. The term “reload” here specifically refers to rebuilding the server definition by re-executing the function given to the RatpackServer.of(Action) method that defined the server.

    Reload informants will never be queried concurrently so can be safely stateful. Calls to shouldReload(Registry) are serialised for any given informant, and informants are queried in sequence.

    Reload informants are queried in the order they are returned by the server registry. If an informant indicates that the server should reload, no further informants will be queried.

    As reload informants are queried for every request, it is sometimes desirable to internally use some kind of polling technique internally to avoid creating too much overhead. However, implementations do not need to be too performance sensitive as reload informants only apply during development.

    Reload informants are never queried when not in development mode. It is completely benign for informants to be in the server registry when not in development.

    Below shows a contrived reload informant that simply asks that the server reload on every other request.

    
     import ratpack.core.server.ReloadInformant;
     import ratpack.core.server.ServerConfig;
     import ratpack.test.embed.EmbeddedApp;
     import ratpack.exec.registry.Registry;
    
     import static org.junit.jupiter.api.Assertions.assertEquals;
    
     public class Example {
       static class ReloadEveryOtherRequest implements ReloadInformant {
         private int i = 0;
    
         public boolean shouldReload(Registry registry) {
           return ++i % 2 == 0;
         }
    
         public String toString() {
           return "every other request informant";
         }
       }
    
       static int counter = 0;
    
       public static void main(String... args) throws Exception {
         EmbeddedApp.of(s -> s
             .serverConfig(ServerConfig.embedded().development(true))
             .registryOf(r -> r
                 .add(ReloadInformant.class, new ReloadEveryOtherRequest())
                 .add(Integer.class, Example.counter++)
             )
             .handler(r -> ctx -> ctx.render(ctx.get(Integer.class).toString()))
         ).test(httpClient -> {
           assertEquals("0", httpClient.getText()); // first request never queries informants
           assertEquals("1", httpClient.getText()); // reload triggered
           assertEquals("1", httpClient.getText());
           assertEquals("2", httpClient.getText()); // reload triggered
           assertEquals("2", httpClient.getText());
         });
       }
     }
     
    • Method Detail

      • shouldReload

        boolean shouldReload​(Registry registry)
        Whether the server should reload.
        Parameters:
        registry - the server registry
        Returns:
        whether the server should reload
      • toString

        String toString()
        The description of this reload informant.

        This value will be logged if the informant requests a reload, indicating which informant requested a reload.

        Overrides:
        toString in class Object
        Returns:
        the description of this reload informant