DataLoader.java Source code

Java tutorial

Introduction

Here is the source code for DataLoader.java

Source

import java.io.BufferedReader;
import java.io.IOException;

import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.gemfire.GemfireTemplate;

import com.gemstone.gemfire.cache.query.SelectResults;

import biz.c24.api.LicenseException;
import biz.c24.api.RuntimeManager;
import biz.c24.io.api.C24;
import biz.c24.io.api.data.ValidationException;

public class DataLoader {
    public static void main(String[] args)
            throws IOException, ValidationException, LicenseException, JobExecutionAlreadyRunningException,
            JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {

        System.out.println(
                "System is initialising. Please wait for a few seconds then type your queries below once you see the prompt (->)");

        C24.init().withLicence("/biz/c24/api/license-ads.dat");

        // Create our application context - assumes the Spring configuration is in the classpath in a file called spring-config.xml
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        // ========================================================================
        // DEMO
        // A simple command-line interface to allow us to query the GemFire regions
        // ========================================================================

        GemfireTemplate template = context.getBean(GemfireTemplate.class);

        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));

        boolean writePrompt = true;

        try {
            while (true) {
                if (writePrompt) {
                    System.out.print("-> ");
                    System.out.flush();
                    writePrompt = false;
                }
                if (br.ready()) {
                    try {

                        String request = br.readLine();
                        System.out.println("Running: " + request);
                        SelectResults<Object> results = template.find(request);
                        System.out.println();
                        System.out.println("Result:");
                        for (Object result : results.asList()) {
                            System.out.println(result.toString());
                        }
                    } catch (Exception ex) {
                        System.out.println("Error executing last command " + ex.getMessage());
                        //ex.printStackTrace();
                    }

                    writePrompt = true;

                } else {
                    // Wait for a second and try again
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ioEx) {
                        break;
                    }
                }
            }
        } catch (IOException ioe) {
            // Write any exceptions to stderr
            System.err.print(ioe);
        }

    }

}