use of org.springframework.boot.ApplicationRunner in project bitcoin-spring-boot-starter by theborakompanioni.
the class TorExampleApplication method torInfoRunner.
@Bean
@Profile("!test")
public ApplicationRunner torInfoRunner() {
String successPhrase = "Congratulations. This browser is configured to use Tor.";
String errorPhraseIgnoreCase = "not using Tor";
return args -> {
HttpGet req = new HttpGet("https://check.torproject.org/");
HttpResponse rsp = torHttpClient.execute(req);
String body = EntityUtils.toString(rsp.getEntity(), StandardCharsets.UTF_8);
boolean containsErrorPhrase = body.toLowerCase().contains(errorPhraseIgnoreCase.toLowerCase());
boolean containsSuccessPhrase = body.contains(successPhrase);
boolean torEnabled = containsSuccessPhrase && !containsErrorPhrase;
log.info("=================================================");
if (torEnabled) {
log.info("Tor is enabled.");
} else {
log.warn("Tor is NOT enabled.");
}
log.info("=================================================");
};
}
use of org.springframework.boot.ApplicationRunner in project bitcoin-spring-boot-starter by theborakompanioni.
the class TorContainerExampleApplication method mainRunner.
@Bean
@Profile("!test")
public ApplicationRunner mainRunner() {
String successPhrase = "Congratulations. This browser is configured to use Tor.";
String errorPhraseIgnoreCase = "not using Tor";
return args -> {
log.info("=================================================");
HttpGet req = new HttpGet("https://check.torproject.org/");
HttpResponse rsp = torHttpClient.execute(req);
String body = EntityUtils.toString(rsp.getEntity(), StandardCharsets.UTF_8);
boolean containsErrorPhrase = body.toLowerCase().contains(errorPhraseIgnoreCase.toLowerCase());
boolean containsSuccessPhrase = body.contains(successPhrase);
boolean torEnabled = containsSuccessPhrase && !containsErrorPhrase;
if (torEnabled) {
log.info("Tor is enabled.");
} else {
log.warn("Tor is NOT enabled.");
}
log.info("=================================================");
};
}
use of org.springframework.boot.ApplicationRunner in project spring-study by backtony.
the class AppConfig method applicationRunner.
// 그냥 스프링 뜰때 유저 하나 넣어준 거임
@Bean
public ApplicationRunner applicationRunner() {
return new ApplicationRunner() {
@Autowired
AccountService accountService;
@Autowired
AppProperties appProperties;
@Override
public void run(ApplicationArguments args) throws Exception {
Account admin = Account.builder().email(appProperties.getAdminUsername()).password(appProperties.getAdminPassword()).roles(Set.of(AccountRole.ADMIN, AccountRole.USER)).build();
accountService.saveAccount(admin);
Account user = Account.builder().email(appProperties.getUserUsername()).password(appProperties.getUserPassword()).roles(Set.of(AccountRole.USER)).build();
accountService.saveAccount(user);
}
};
}
use of org.springframework.boot.ApplicationRunner in project facete3 by hobbit-project.
the class LauncherServiceCapable method serviceLauncher.
@Bean
public ApplicationRunner serviceLauncher(ServiceCapable serviceCapable, ConfigurableApplicationContext ctx) {
ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext) getRoot(ctx, ApplicationContext::getParent);
// ConfigurableApplicationContext rootCtx = ctx;
ServiceDelegateEntity<? extends ServiceCapable> activeService = ServiceCapableWrapper.wrap(serviceCapable);
// Add a listener that closes the service's (root) context on service termination
activeService.addListener(new Listener() {
@Override
public void failed(State priorState, Throwable t) {
logger.info("ServiceCapable service wrapped [FAILED] for " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()), t);
// logger.info("ServiceCapable service wrapper stopped");
// ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext)getRoot(ctx.getParent(), ApplicationContext::getParent);
rootCtx.close();
}
@Override
public void terminated(State priorState) {
logger.info("ServiceCapable service wrapper [TERMINATED] for " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
// logger.info("ServiceCapable service wrapper stopped");
// ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext)getRoot(ctx.getParent(), ApplicationContext::getParent);
rootCtx.close();
}
}, MoreExecutors.directExecutor());
// If the service's context gets closed, terminate the service
ctx.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
logger.info("Context is closing - shutdown service " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
if (activeService != null && activeService.isRunning()) {
try {
activeService.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
// thread.interrupt();
} catch (TimeoutException e) {
try {
activeService.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
} catch (Exception f) {
throw new RuntimeException(f);
}
// e.printStackTrace();
throw new RuntimeException(e);
}
}
}
});
return args -> {
logger.info("LauncherServiceCapable::ApplicationRunner starting service... " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
// activeService.startAsync().awaitRunning();
activeService.startAsync().awaitTerminated();
logger.info("LauncherServiceCapable::ApplicationRunner service started... " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
};
}
use of org.springframework.boot.ApplicationRunner in project facete3 by hobbit-project.
the class MainServiceLauncher method serviceLauncher.
@Bean
public ApplicationRunner serviceLauncher(@Qualifier("MainService") Service activeService, ConfigurableApplicationContext ctx) {
ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext) getRoot(ctx, ApplicationContext::getParent);
// ConfigurableApplicationContext rootCtx = ctx;
// Add a listener that closes the service's (root) context on service termination
activeService.addListener(new Listener() {
@Override
public void failed(State priorState, Throwable t) {
logger.info("ServiceCapable service wrapped [FAILED] for " + (activeService == null ? "(no active service)" : activeService.getClass()), t);
// logger.info("ServiceCapable service wrapper stopped");
// ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext)getRoot(ctx.getParent(), ApplicationContext::getParent);
rootCtx.close();
}
@Override
public void terminated(State priorState) {
logger.info("ServiceCapable service wrapper [TERMINATED] for " + (activeService == null ? "(no active service)" : activeService.getClass()));
// logger.info("ServiceCapable service wrapper stopped");
// ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext)getRoot(ctx.getParent(), ApplicationContext::getParent);
rootCtx.close();
}
}, MoreExecutors.directExecutor());
// If the service's context gets closed, terminate the service
ctx.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
logger.info("Context is closing - shutdown service " + (activeService == null ? "(no active service)" : activeService.getClass()));
if (activeService != null && activeService.isRunning()) {
try {
activeService.stopAsync().awaitTerminated(1000, TimeUnit.SECONDS);
// thread.interrupt();
} catch (TimeoutException e) {
// e.printStackTrace();
throw new RuntimeException(e);
}
}
}
});
return args -> {
logger.info("MainServiceLauncher::ApplicationRunner starting service... " + (activeService == null ? "(no active service)" : activeService.getClass()));
// activeService.startAsync().awaitRunning();
activeService.startAsync().awaitTerminated();
logger.info("MainServiceLauncher::ApplicationRunner service started... " + (activeService == null ? "(no active service)" : activeService.getClass()));
};
}
Aggregations