Reputation: 6347
How can I refactor an abstract class that implements Runnable into an interface?
import com.codahale.metrics.health.HealthCheck;
public abstract class ApplicationProcessor implements Runnable {
public abstract HealthCheck getHealthCheck();
}
UPDATE: And also how can I remove the usage of a generic wildcard type for the future?
import io.dropwizard.lifecycle.Managed;
import java.util.concurrent.Future;
import com.codahale.metrics.health.HealthCheck;
public class ManagedBean extends HealthCheck implements Managed {
private final String name;
private final ManagedThreadPool threadPool;
private final ApplicationProcessor processor;
private Future<?> future; // HERE sonarqube complains..
public ManagedBean( String name, ManagedThreadPool threadPool, ApplicationProcessor processor ) {
this.name = name;
this.threadPool = threadPool;
this.processor = processor;
}
@Override
public void start() throws Exception {
future = threadPool.submit( processor );
}
@Override
public void stop() throws Exception {
if ( !future.isDone() ) {
future.cancel( true );
}
}
@Override
protected Result check() throws Exception {
return processor.getHealthCheck().execute();
}
}
Thanks
Upvotes: 0
Views: 1879
Reputation: 4496
public interface ApplicationProcessor extends Runnable {
// interface methods are by default public and abstract
HealthCheck getHealthCheck();
}
As for 2nd part use
Future<Void> or just Future
Upvotes: 1