<p>I have a microservice that is not a webservice.  </p>

<p>It is a Spring Boot (1.5) CommandLineRunner app that does not have a need to expose an API or do anything with http.</p>

<p>However, I need to give it a liveness probe for Kubernetes.</p>

<p>Can this be accomplished without refactoring it into a webservice app?</p>

<p>I have this configuration added to enable Spring's info endpoint</p>

<pre><code>management:
  endpoint:
    health:
      enabled: true
    info:
      enabled: true

# https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints
info:
  app:
    name: foo-parser
    description: parses binary files from S3 and updates the database
</code></pre>

<p>I implemented this health check class</p>

<pre><code>import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;

@Component
public class HealthCheck extends AbstractHealthIndicator {

  Logger log = LoggerFactory.getLogger("jsonLogger");

  private final MySQLAccessor mySQLAccessor;
  private static final String FOO_TABLE = "foo";

  @Autowired
  public HealthCheck(final MySQLAccessor mySQLAccessor) {
    this.mySQLAccessor = mySQLAccessor;
  }

  @Override
  protected void doHealthCheck(Builder builder) throws Exception {
    boolean result = mySQLAccessor.healthCheck(FOO_TABLE);
    if (result) {
      log.info("HELLO! the health check is good!");
      builder.up().withDetail("test", "good");
    }
    else {
      log.info("HELLO! OH NOES the health check is ungood!");
      builder.down().withDetail("test", "bad");
    }
  }
}


</code></pre>

<p>Can this idea work?  Or do I have to refactor it to serve web requests?</p>

<p>Thank you for any clues</p>

<p>Trying out stringy05's idea...</p>
<p>enabled jmx endpoints:</p>
<pre><code>management:
  endpoints:
    jmx:
      exposure:
        include: &quot;*&quot;
        exclude:
  endpoint:
    health:
      enabled: true
    info:
      enabled: true
</code></pre>
<p>Using this answer:</p>
<p><a href="https://stackoverflow.com/questions/1751130/calling-jmx-mbean-method-from-a-shell-script#answer-47869966">Calling JMX MBean method from a shell script</a></p>
<p>I tried this:</p>
<pre><code>import javax.management.*;
import javax.management.remote.*;

public class JMXInvoker {

  public static void main(String... args) throws Exception {
    Object result = JMXConnectorFactory.connect(new JMXServiceURL(args[0]))
        .getMBeanServerConnection().invoke(new ObjectName(args[1]), args[2], new Object[]{}, new String[]{});
    String status = &quot;&quot; + result;
    String state = status.substring(8,10);
    Boolean ok = state.compareTo(&quot;UP&quot;) == 0;
    if (!ok)
      System.exit(1);
  }
}

</code></pre>
<p>Added some JMX related beans to the main app class</p>
<pre><code>@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
public class FooParserApplication implements CommandLineRunner {

  Logger log = LoggerFactory.getLogger(&quot;jsonLogger&quot;);
  @Autowired
  private FooStuff fooStuff;

  @Bean
  public RmiRegistryFactoryBean rmi() {
    RmiRegistryFactoryBean rmi = new RmiRegistryFactoryBean();
    rmi.setPort(5555);
    return rmi;
  }
  
  @Bean
  public ConnectorServerFactoryBean server() throws Exception {
    ConnectorServerFactoryBean fb = new ConnectorServerFactoryBean();
    fb.setObjectName(&quot;connector:name=rmi&quot;);
    fb.setServiceUrl(&quot;service:jmx:rmi://localhost/jndi/rmi://localhost:5555/myconnector&quot;);
    return fb;
  }

  public static void main(final String[] args) {
    final SpringApplication springApplication = new SpringApplication(ECHParserApplication.class);
    springApplication.run(args);
  }

  @Override
  public void run(final String... args) {
     fooStuff.doIt()
  }
}
</code></pre>
<p>calling it from bash:</p>
<pre class="lang-bash prettyprint-override"><code>java -cp foo-parser.jar -Dloader.main=com.foo.JMXInvoker \
  org.springframework.boot.loader.PropertiesLauncher \
  service:jmx:rmi://localhost/jndi/rmi://localhost:5555/myconnector \
  org.springframework.boot:type=Endpoint,name=Health \
  health
</code></pre>
<p>Now I just need to put it in a bash script for Kubernetes.</p>
<p>Thanks!</p>
