Search in sources :

Example 1 with ApplicationRunner

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("=================================================");
    };
}
Also used : SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) ApplicationRunner(org.springframework.boot.ApplicationRunner) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationListener(org.springframework.context.ApplicationListener) Tor(org.berndpruenster.netlayer.tor.Tor) Profile(org.springframework.context.annotation.Profile) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) EntityUtils(org.apache.http.util.EntityUtils) ApplicationPidFileWriter(org.springframework.boot.context.ApplicationPidFileWriter) WebServerPortFileWriter(org.springframework.boot.web.context.WebServerPortFileWriter) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) Qualifier(org.springframework.beans.factory.annotation.Qualifier) WebApplicationType(org.springframework.boot.WebApplicationType) HttpResponse(org.apache.http.HttpResponse) Optional(java.util.Optional) HiddenServiceDefinition(org.tbk.tor.hs.HiddenServiceDefinition) Bean(org.springframework.context.annotation.Bean) TorCtlException(org.berndpruenster.netlayer.tor.TorCtlException) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Profile(org.springframework.context.annotation.Profile) Bean(org.springframework.context.annotation.Bean)

Example 2 with ApplicationRunner

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("=================================================");
    };
}
Also used : SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) ApplicationRunner(org.springframework.boot.ApplicationRunner) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationListener(org.springframework.context.ApplicationListener) Profile(org.springframework.context.annotation.Profile) StandardCharsets(java.nio.charset.StandardCharsets) EntityUtils(org.apache.http.util.EntityUtils) ApplicationPidFileWriter(org.springframework.boot.context.ApplicationPidFileWriter) Slf4j(lombok.extern.slf4j.Slf4j) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) Qualifier(org.springframework.beans.factory.annotation.Qualifier) WebApplicationType(org.springframework.boot.WebApplicationType) HttpResponse(org.apache.http.HttpResponse) Bean(org.springframework.context.annotation.Bean) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Profile(org.springframework.context.annotation.Profile) Bean(org.springframework.context.annotation.Bean)

Example 3 with ApplicationRunner

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);
        }
    };
}
Also used : Account(com.apistudy.restapi.accounts.Account) ApplicationRunner(org.springframework.boot.ApplicationRunner) AccountService(com.apistudy.restapi.accounts.AccountService) AppProperties(com.apistudy.restapi.common.AppProperties) ApplicationArguments(org.springframework.boot.ApplicationArguments) Bean(org.springframework.context.annotation.Bean)

Example 4 with ApplicationRunner

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()));
    };
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ServiceDelegateEntity(org.hobbit.core.service.api.ServiceDelegateEntity) Logger(org.slf4j.Logger) ServiceCapableWrapper(org.aksw.commons.service.core.ServiceCapableWrapper) ApplicationRunner(org.springframework.boot.ApplicationRunner) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ApplicationListener(org.springframework.context.ApplicationListener) Function(java.util.function.Function) ApplicationContext(org.springframework.context.ApplicationContext) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) State(com.google.common.util.concurrent.Service.State) Listener(com.google.common.util.concurrent.Service.Listener) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Bean(org.springframework.context.annotation.Bean) ServiceCapable(org.hobbit.core.service.api.ServiceCapable) ApplicationListener(org.springframework.context.ApplicationListener) Listener(com.google.common.util.concurrent.Service.Listener) State(com.google.common.util.concurrent.Service.State) TimeoutException(java.util.concurrent.TimeoutException) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) TimeoutException(java.util.concurrent.TimeoutException) Bean(org.springframework.context.annotation.Bean)

Example 5 with ApplicationRunner

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()));
    };
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Logger(org.slf4j.Logger) ApplicationRunner(org.springframework.boot.ApplicationRunner) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ApplicationListener(org.springframework.context.ApplicationListener) Function(java.util.function.Function) ApplicationContext(org.springframework.context.ApplicationContext) Service(com.google.common.util.concurrent.Service) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) State(com.google.common.util.concurrent.Service.State) Listener(com.google.common.util.concurrent.Service.Listener) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Bean(org.springframework.context.annotation.Bean) ApplicationListener(org.springframework.context.ApplicationListener) Listener(com.google.common.util.concurrent.Service.Listener) State(com.google.common.util.concurrent.Service.State) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) TimeoutException(java.util.concurrent.TimeoutException) Bean(org.springframework.context.annotation.Bean)

Aggregations

ApplicationRunner (org.springframework.boot.ApplicationRunner)6 Bean (org.springframework.context.annotation.Bean)6 ApplicationListener (org.springframework.context.ApplicationListener)4 Qualifier (org.springframework.beans.factory.annotation.Qualifier)3 SpringBootApplication (org.springframework.boot.autoconfigure.SpringBootApplication)3 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 Listener (com.google.common.util.concurrent.Service.Listener)2 State (com.google.common.util.concurrent.Service.State)2 StandardCharsets (java.nio.charset.StandardCharsets)2 List (java.util.List)2 Objects (java.util.Objects)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 Function (java.util.function.Function)2 Slf4j (lombok.extern.slf4j.Slf4j)2 HttpResponse (org.apache.http.HttpResponse)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 EntityUtils (org.apache.http.util.EntityUtils)2 Logger (org.slf4j.Logger)2