Example usage for org.springframework.context ConfigurableApplicationContext registerShutdownHook

List of usage examples for org.springframework.context ConfigurableApplicationContext registerShutdownHook

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext registerShutdownHook.

Prototype

void registerShutdownHook();

Source Link

Document

Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Usage

From source file:org.openbaton.nfvo.main.Application.java

public static void main(String[] args) {

    ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
    context.registerShutdownHook();
}

From source file:com.oreilly.springdata.rest.client.ProductsClient.java

public static void main(String[] args) {

    // Bootstrap RestOperations instance using Spring
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
    context.registerShutdownHook();
    RestOperations operations = context.getBean(RestOperations.class);

    // Access root resource
    ResourceSupport root = operations.getForObject(ClientConfiguration.BASE_URL, Resource.class);
    Link productLink = root.getLink(ClientConfiguration.PRODUCTS_REL);

    // Follow link to access products
    Products products = operations.getForObject(productLink.getHref(), Products.class);
    System.out.println(products.getContent().iterator().next());
}

From source file:com.oreilly.springdata.rest.client.CustomerClient.java

public static void main(String[] args) {

    // Setup RestTemplate though Spring
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
    context.registerShutdownHook();
    RestOperations restOperations = context.getBean(RestOperations.class);

    // Access root resource
    ResourceSupport result = restOperations.getForObject(ClientConfiguration.BASE_URL, Resource.class);

    Link link = result.getLink(ClientConfiguration.CUSTOMERS_REL);
    System.out.println("Following: " + link.getHref());

    // Follow link relation for customers to access those
    Customers customers = restOperations.getForObject(link.getHref(), Customers.class);

    for (Customer dto : customers) {
        com.oreilly.springdata.rest.core.Customer customer = dto.getContent();
        System.out.println(customer.getFirstname() + " " + customer.getLastname());
    }//from   w w  w. ja va 2  s .  c  o  m
}

From source file:org.spring.data.gemfire.app.main.LocatorApp.java

public static void main(final String... args) {
    ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            LOCATOR_CONFIGURATION_FILE);

    applicationContext.registerShutdownHook();

    LocatorLauncher locator = applicationContext.getBean("locator", LocatorLauncher.class);

    locator.start();/*w w  w.  j  av  a  2 s .c  o m*/

    System.out.printf("Starting Locator (%1$s) on port (%2$d)...%n", locator.getMember(), locator.getPort());

    locator.waitOnLocator();

    System.out.printf("Locator stopping...");
}

From source file:com.oreilly.springdata.rest.client.OrderClient.java

public static void main(String[] args) {

    // Bootstrap RestOperations instance using Spring
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
    context.registerShutdownHook();
    RestOperations operations = context.getBean(RestOperations.class);

    // Access root resource
    ResourceSupport root = operations.getForEntity(ClientConfiguration.BASE_URL, Resource.class).getBody();
    Link orderLink = root.getLink(ClientConfiguration.ORDERS_REL);

    // Follow link to access orders
    Orders orders = operations.getForObject(orderLink.getHref(), Orders.class);

    for (Order order : orders) {

        // Follow link to access customer of the order
        Link link = order.getLink(ClientConfiguration.ORDER_CUSTOMER_REL);
        Customer customer = operations.getForObject(link.getHref(), CustomerClient.Customer.class);
        com.oreilly.springdata.rest.core.Customer domainObject = customer.getContent();
        System.out.println(/*from   w w  w . j  a va2s . c  o  m*/
                "Order for customer: " + domainObject.getFirstname() + " " + domainObject.getLastname());
    }
}

From source file:com.acapulcoapp.alloggiatiweb.AddRegion.java

public static void main(String[] args) throws UnknownHostException, IOException {
    // TODO code application logic here

    SpringApplication app = new SpringApplication(AcapulcoappApp.class);
    SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
    addDefaultProfile(app, source);/*from ww  w .ja  va2  s  .c  om*/

    ConfigurableApplicationContext context = app.run(args);

    initBeans(context);

    runTask();

    context.registerShutdownHook();

    System.exit(0);
}

From source file:ar.com.zauber.labs.kraken.providers.wikipedia.apps.administrative.MainAdministrative.java

/**
 * Lee la pagina /* ww w.  ja v  a  2s . co  m*/
 * http://es.wikipedia.org/wiki/Departamentos_y_partidos_de_la_Argentina
 * la tabla de localidades
 */
public static void main(final String[] args)
        throws IOException, JAXBException, SAXException, URISyntaxException {
    final ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
            "ar/com/zauber/labs/kraken/providers/wikipedia/apps/administrative/"
                    + "config/context-administrative-kraken-spring.xml");
    ctx.registerShutdownHook();

    new ArgentinaDepartmentsSync(
            new BufferedReader(new InputStreamReader(
                    ArgentinaDepartmentsSync.class.getResourceAsStream("es.txt"), "utf-8")),
            (WikiPageRetriever) ctx.getBean("httpWikiPageRetriever"), LangUtils.ES,
            (Closure) ctx.getBean("administrativeClosure"));
}

From source file:com.devicehive.application.DeviceHiveApplication.java

public static void main(String... args) {
    ConfigurableApplicationContext context = new SpringApplicationBuilder().sources(DeviceHiveApplication.class)
            .web(true).run(args);//from w ww .  j  av  a  2  s .co m

    DeviceHiveApplication app = context.getBean(DeviceHiveApplication.class);
    context.registerShutdownHook();
}

From source file:com.acapulcoapp.alloggiatiweb.FileReader.java

public static void main(String[] args) throws UnknownHostException, IOException {
    // TODO code application logic here

    SpringApplication app = new SpringApplication(AcapulcoappApp.class);
    SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
    addDefaultProfile(app, source);//from w w w .j  av  a 2s  . c  o m

    ConfigurableApplicationContext context = app.run(args);

    initBeans(context);

    Map<LocalDate, List<List<String>>> map = new TreeMap<>();

    List<File> files = new ArrayList<>(FileUtils.listFiles(new File("/Users/chiccomask/Downloads/ALLOGGIATI"),
            new String[] { "txt" }, true));

    Collections.reverse(files);

    int count = 0;

    for (File file : files) {

        //            List<String> allLines = FileUtils.readLines(file, "windows-1252");
        List<String> allLines = FileUtils.readLines(file, "UTF-8");

        for (int i = 0; i < allLines.size();) {

            count++;

            List<String> record = new ArrayList<>();

            String line = allLines.get(i);
            String type = TIPO_ALLOGGIO.parse(line);

            switch (type) {
            case "16":
                record.add(line);
                i++;
                break;
            case "17": {
                record.add(line);
                boolean out = false;
                while (!out) {
                    i++;
                    if (i < allLines.size()) {
                        String subline = allLines.get(i);
                        String subtype = TIPO_ALLOGGIO.parse(subline);
                        if (!subtype.equals("19")) {
                            out = true;
                        } else {
                            record.add(subline);
                        }
                    } else {
                        out = true;
                    }
                }
                break;
            }
            case "18": {
                record.add(line);
                boolean out = false;
                while (!out) {
                    i++;
                    if (i < allLines.size()) {
                        String subline = allLines.get(i);
                        String subtype = TIPO_ALLOGGIO.parse(subline);
                        if (!subtype.equals("20")) {
                            out = true;
                        } else {
                            record.add(subline);
                        }
                    } else {
                        out = true;
                    }
                }
                break;
            }
            default:
                break;
            }

            LocalDate arrived = LocalDate.parse(DATA_ARRIVO.parse(line),
                    DateTimeFormatter.ofPattern(DATE_PATTERN));
            if (!map.containsKey(arrived)) {
                map.put(arrived, new ArrayList<>());
            }
            map.get(arrived).add(record);
        }
    }

    for (LocalDate date : map.keySet()) {

        System.out.println();
        System.out.println("process day " + date);

        for (List<String> record : map.get(date)) {

            System.out.println();
            System.out.println("process record ");
            for (String line : record) {
                System.out.println(line);
            }

            CheckinRecord checkinRecord = new CheckinRecord();

            //non lo setto per adesso
            String firstLine = record.get(0);

            String typeStr = TIPO_ALLOGGIO.parse(firstLine);
            CheckinType cht = checkinTypeRepository.find(typeStr);
            checkinRecord.setCheckinType(cht);

            int days = Integer.parseInt(PERMANENZA.parse(firstLine));
            checkinRecord.setDays(days);
            checkinRecord.setArrived(date);

            boolean isMain = true;

            List<Person> others = new ArrayList<>();

            for (String line : record) {
                Person p = extractPerson(line);

                if (p.getDistrictOfBirth() == null) {
                    System.out.println("district of birth not found " + p);
                }

                List<Person> duplicates = personRepository.findDuplicates(p.getSurname(), p.getName(),
                        p.getDateOfBirth());

                if (duplicates.isEmpty()) {
                    System.out.println("add new person " + p.getId() + " " + p);
                    personRepository.saveAndFlush(p);
                } else if (duplicates.size() == 1) {

                    Person found = duplicates.get(0);

                    if (p.getIdentityDocument() != null) {
                        //we sorted by date so we suppose 
                        //the file version is newer so we update the entity
                        p.setId(found.getId());
                        System.out.println("update person " + p.getId() + " " + p);
                        personRepository.saveAndFlush(p);

                    } else if (found.getIdentityDocument() != null) {
                        //on db there are more data so I use them.
                        p = found;
                        System.out.println("use already saved person " + p.getId() + " " + p);
                    } else {
                        p.setId(found.getId());
                        System.out.println("update person " + p.getId() + " " + p);
                        personRepository.saveAndFlush(p);
                    }

                } else {
                    throw new RuntimeException("More duplicated for " + p.getName());
                }

                if (isMain) {
                    checkinRecord.setMainPerson(p);
                    isMain = false;
                } else {
                    others.add(p);
                }
            }

            checkinRecord.setOtherPeople(new HashSet<>(others));

            if (checkinRecordRepository.alreadyExists(checkinRecord.getMainPerson(), date) != null) {
                System.out.println("already exists " + date + " p " + checkinRecord.getMainPerson());
            } else {
                System.out.println("save record ");
                checkinRecordRepository.saveAndFlush(checkinRecord);
            }
        }
    }

    //
    //            if (type.equals("16")) {
    //                List<String> record = new ArrayList<>();
    //                record.add(line);
    //                keepOpen = false;
    //            }
    //
    //            map.get(arrived).add(record);
    //        map.values().forEach((list) -> {
    //
    //            for (String line : list) {
    //
    //                Person p = null;
    //
    //                try {
    //
    //                    p = extractPerson(line);
    //
    //                    List<Person> duplicates = personRepository.findDuplicates(p.getSurname(), p.getName(), p.getDateOfBirth());
    //
    //                    if (duplicates.isEmpty()) {
    //                        personRepository.saveAndFlush(p);
    //
    //                    } else if (duplicates.size() > 1) {
    //                        System.out.println();
    //                        System.out.println("MULIPLE DUPLICATED");
    //
    //                        for (Person dd : duplicates) {
    //                            System.out.println(dd);
    //                        }
    //                        System.out.println("* " + p);
    //                        throw new RuntimeException();
    //                    } else {
    //
    ////                        if (!duplicates.get(0).getDistrictOfBirth().equals(p.getDistrictOfBirth())) {
    ////                        int index = 0;
    ////
    ////                        System.out.println();
    ////                        System.out.println("DUPLICATED");
    ////
    ////                        for (Person dd : duplicates) {
    ////                            System.out.println(dd);
    ////                            index++;
    ////                        }
    ////                        System.out.println("* " + p);
    ////                        System.out.println(file.getAbsolutePath() + " " + p);
    ////
    ////                        System.out.println();
    ////                        System.out.println();
    ////                        }
    ////                        duplicates.remove(0);
    ////                        personRepository.deleteInBatch(duplicates);
    ////                System.out.println();
    ////                System.out.println("Seleziona scelta");
    ////                Scanner s = new Scanner(System.in);
    ////                int selected;
    ////                try {
    ////                    selected = s.nextInt();
    ////                } catch (InputMismatchException e) {
    ////                    selected = 0;
    ////                }
    ////
    ////                if (duplicates.size() <= selected) {
    ////                    personRepository.deleteInBatch(duplicates);
    ////                    personRepository.saveAndFlush(p);
    ////                } else {
    ////                    duplicates.remove(selected);
    ////                    personRepository.deleteInBatch(duplicates);
    ////                }
    //                    }
    //
    //                } catch (Exception e) {
    //
    //                    System.out.println();
    ////                    System.out.println("ERROR READING lineCount=" + allLines.indexOf(line) + " line=" + line);
    ////                    System.out.println(file.getAbsolutePath());
    //                    System.out.println(p);
    //                    e.printStackTrace();
    //                    System.out.println();
    //                }
    //            }
    //        });
    context.registerShutdownHook();

    System.exit(0);
}

From source file:org.spring.data.gemfire.app.main.AbstractApp.java

protected ConfigurableApplicationContext initApplicationContext(final String... args) {
    ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            getConfigurationFile(args));
    applicationContext.registerShutdownHook();
    return applicationContext;
}