Skip to content

Commit 05b7731

Browse files
committed
Test HealthCheck
1 parent 082b044 commit 05b7731

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.kill-bill.billing</groupId>
2424
<artifactId>killbill-oss-parent</artifactId>
25-
<version>0.146.30</version>
25+
<version>0.146.51-SNAPSHOT</version>
2626
</parent>
2727
<groupId>org.kill-bill.billing.plugin.java</groupId>
2828
<artifactId>hello-world-plugin</artifactId>

src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldActivator.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.servlet.Servlet;
2626
import javax.servlet.http.HttpServlet;
2727

28+
import org.killbill.billing.invoice.plugin.api.InvoiceFormatterFactory;
2829
import org.killbill.billing.invoice.plugin.api.InvoicePluginApi;
2930
import org.killbill.billing.osgi.api.Healthcheck;
3031
import org.killbill.billing.osgi.api.OSGIPluginProperties;
@@ -36,9 +37,12 @@
3637
import org.killbill.billing.plugin.core.config.PluginEnvironmentConfig;
3738
import org.killbill.billing.plugin.core.resources.jooby.PluginApp;
3839
import org.killbill.billing.plugin.core.resources.jooby.PluginAppBuilder;
40+
import org.killbill.commons.health.api.HealthCheckRegistry;
3941
import org.osgi.framework.BundleContext;
42+
import org.osgi.framework.ServiceReference;
4043
import org.osgi.util.tracker.ServiceTracker;
41-
import org.killbill.billing.invoice.plugin.api.InvoiceFormatterFactory;
44+
import org.slf4j.Logger;
45+
import org.slf4j.LoggerFactory;
4246

4347
public class HelloWorldActivator extends KillbillActivatorBase {
4448

@@ -48,16 +52,26 @@ public class HelloWorldActivator extends KillbillActivatorBase {
4852
//
4953
public static final String PLUGIN_NAME = "hello-world-plugin";
5054

55+
private static final Logger logger = LoggerFactory.getLogger(HelloWorldActivator.class);
56+
5157
private HelloWorldConfigurationHandler helloWorldConfigurationHandler;
5258
private OSGIKillbillEventDispatcher.OSGIKillbillEventHandler killbillEventHandler;
5359
private MetricsGeneratorExample metricsGenerator;
5460

5561
private ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker;
5662

63+
private HealthCheckRegistry healthCheckRegistry;
64+
5765
@Override
5866
public void start(final BundleContext context) throws Exception {
5967
super.start(context);
60-
68+
final ServiceReference<HealthCheckRegistry> reference = context.getServiceReference(HealthCheckRegistry.class);
69+
if (reference != null) {
70+
final HealthCheckRegistry healthCheckRegistry = context.getService(reference);
71+
if (healthCheckRegistry != null) {
72+
this.healthCheckRegistry = healthCheckRegistry;
73+
}
74+
}
6175
final String region = PluginEnvironmentConfig.getRegion(configProperties.getProperties());
6276

6377
// Register an event listener for plugin configuration (optional)
@@ -72,7 +86,7 @@ public void start(final BundleContext context) throws Exception {
7286

7387

7488
// Register an event listener (optional)
75-
killbillEventHandler = new HelloWorldListener(killbillAPI, invoiceFormatterTracker, configProperties.getProperties());
89+
killbillEventHandler = new HelloWorldListener(killbillAPI, invoiceFormatterTracker, healthCheckRegistry, configProperties.getProperties());
7690

7791
// As an example, this plugin registers a PaymentPluginApi (this could be
7892
// changed to any other plugin api)
@@ -99,6 +113,7 @@ public void start(final BundleContext context) throws Exception {
99113
registerServlet(context, httpServlet);
100114

101115
registerHandlers();
116+
// HealthCheck aviateHealthCheck = hea
102117
}
103118

104119
@Override

src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldListener.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import java.util.List;
2323
import java.util.Locale;
2424
import java.util.Properties;
25+
import java.util.Set;
2526

26-
import org.joda.time.LocalDate;
2727
import org.killbill.billing.account.api.Account;
2828
import org.killbill.billing.account.api.AccountApiException;
2929
import org.killbill.billing.invoice.api.Invoice;
@@ -36,6 +36,8 @@
3636
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillEventDispatcher;
3737
import org.killbill.billing.plugin.api.PluginTenantContext;
3838
import org.killbill.billing.util.callcontext.TenantContext;
39+
import org.killbill.commons.health.api.HealthCheckRegistry;
40+
import org.killbill.commons.health.api.Result;
3941
import org.osgi.util.tracker.ServiceTracker;
4042
import org.slf4j.Logger;
4143
import org.slf4j.LoggerFactory;
@@ -50,9 +52,12 @@ public class HelloWorldListener implements OSGIKillbillEventDispatcher.OSGIKillb
5052

5153
private final Properties configProperties;
5254

53-
public HelloWorldListener(final OSGIKillbillAPI killbillAPI, final ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker, Properties configProperties) {
55+
private final HealthCheckRegistry healthCheckRegistry;
56+
57+
public HelloWorldListener(final OSGIKillbillAPI killbillAPI, final ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker, final HealthCheckRegistry healthCheckRegistry, final Properties configProperties) {
5458
this.osgiKillbillAPI = killbillAPI;
5559
this.invoiceFormatterTracker = invoiceFormatterTracker;
60+
this.healthCheckRegistry = healthCheckRegistry;
5661
this.configProperties = configProperties;
5762
}
5863

@@ -66,6 +71,7 @@ public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
6671
killbillEvent.getObjectType());
6772

6873
final TenantContext context = new PluginTenantContext(killbillEvent.getAccountId(), killbillEvent.getTenantId());
74+
// HealthCheck aviateHealthCheck = hea
6975
switch (killbillEvent.getEventType()) {
7076
//
7177
// Handle ACCOUNT_CREATION and ACCOUNT_CHANGE only for demo purpose and just print the account
@@ -78,6 +84,14 @@ public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
7884
} catch (final AccountApiException e) {
7985
logger.warn("Unable to find account", e);
8086
}
87+
final Set<String> names = healthCheckRegistry.getNames();
88+
logger.info("names {}", names);
89+
Result result = healthCheckRegistry.runHealthCheck("org.killbill.billing.server.healthchecks.KillbillHealthcheck");
90+
logger.info("KB healthcheck result: {}", result.isHealthy());
91+
result = healthCheckRegistry.runHealthCheck("org.killbill.billing.server.healthchecks.KillbillPluginsHealthcheck");
92+
logger.info("Plugins healthcheck result: {}", result.isHealthy());
93+
result = healthCheckRegistry.runHealthCheck("com.killbill.billing.plugin.aviate.AviateHealthCheck"); //This line fails
94+
logger.info("Aviate healthcheck result: {}", result.isHealthy());
8195
break;
8296
case INVOICE_CREATION:
8397

0 commit comments

Comments
 (0)