batchee-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Romain Manni-Bucau (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (BATCHEE-92) Create a File based PersistenceManagerService
Date Tue, 19 Jan 2016 10:55:40 GMT

    [ https://issues.apache.org/jira/browse/BATCHEE-92?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15106582#comment-15106582
] 

Romain Manni-Bucau edited comment on BATCHEE-92 at 1/19/16 10:55 AM:
---------------------------------------------------------------------

[~struberg] Side note/tip for batchee-cli: i used derby jdbc persistence for it, makes it
easy to maintain and move later if needed:

{code}
public class CustomCliConfiguration extends DefaultCliConfiguration {
    @Override
    public String name() {
        final String version = CustomCliConfiguration.class.getPackage().getImplementationVersion();
        return "java -jar batches-" + (version == null ? "{version}" : version) + "-all.jar";
    }

    @Override
    public String description() {
        return "batches";
    }

    @Override
    public Iterator<Class<? extends UserCommand>> userCommands() {
        return Arrays.<Class<? extends UserCommand>>asList(MyBatch.class, MyOtherBatch.class).iterator();
    }

    @Override
    public Runnable decorate(final Runnable runnable) {
        return () -> {
            final Properties properties = loadConfiguration();
            setupDatabase(properties);
            setupLogging(properties);
            runnable.run();
        };
    }

    private Properties loadConfiguration() {
        final Properties properties = new Properties(System.getProperties());
        of(ofNullable(System.getProperty("batch.configuration"))
            .map(File::new)
            .orElseGet(() -> new File(jarLocation(CustomCliConfiguration.class), "conf/batch.properties")))
            .filter(File::isFile)
            .ifPresent(file -> {
                try (final FileInputStream fis = new FileInputStream(file)) {
                    properties.load(fis);
                } catch (final IOException e) {
                    throw new IllegalStateException(e);
                }
            });
        return properties;
    }

    private void setupLogging(final Properties properties) {
        final String file = properties.getProperty("batch.logging.file");
        if (file != null) {
            try (final FileInputStream fis = new FileInputStream(file)) {
                final LogManager logManager = LogManager.getLogManager();
                logManager.readConfiguration(fis);
            } catch (final IOException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }

    private void setupDatabase(final Properties properties) {
        try {
            DriverManager.getDriver("jdbc:batch");
        } catch (final SQLException driverNotRegistered) {
            try {

                final String dbPath = properties.getProperty("batch.persistence.path");
                final String delegateUrl = "jdbc:derby:" + (dbPath == null ? "memory:batch"
: dbPath) + ";create=true";
                print("Using database: " + delegateUrl);
                if (dbPath == null) {
                    print("You can customize persistence path adding before '-jar': -Dbatch.persistence.path=<execution
persistence path>");
                }
                DriverManager.registerDriver(new ConfigurableDriver(delegateUrl)); // stupid
driver
            } catch (final SQLException e1) {
                throw new IllegalStateException(e1);
            }
        }
    }

    private void print(final String x) {
        System.out.println(x);
    }
}

// with

public class ConfigurableDriver implements Driver {
    static {
        new EmbeddedDriver();
    }

    private final String delegateUrl;

    public ConfigurableDriver(final String delegateUrl) {
        this.delegateUrl = delegateUrl;
    }

    @Override
    public Connection connect(final String url, final Properties info) throws SQLException
{
        return acceptsURL(url) ? DriverManager.getConnection(delegateUrl, info) : null;
    }

    @Override
    public boolean acceptsURL(final String url) throws SQLException {
        return "jdbc:batch".equalsIgnoreCase(url);
    }

    @Override
    public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) throws
SQLException {
        return new DriverPropertyInfo[0];
    }

    @Override
    public int getMajorVersion() {
        return 4;
    }

    @Override
    public int getMinorVersion() {
        return 2;
    }

    @Override
    public boolean jdbcCompliant() {
        return true;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException();
    }
}
{code}


was (Author: romain.manni-bucau):
[~struberg] i used derby jdbc persistence for it, makes it easy to maintain and move later
if needed:

{code}
public class CustomCliConfiguration extends DefaultCliConfiguration {
    @Override
    public String name() {
        final String version = CustomCliConfiguration.class.getPackage().getImplementationVersion();
        return "java -jar batches-" + (version == null ? "{version}" : version) + "-all.jar";
    }

    @Override
    public String description() {
        return "batches";
    }

    @Override
    public Iterator<Class<? extends UserCommand>> userCommands() {
        return Arrays.<Class<? extends UserCommand>>asList(MyBatch.class, MyOtherBatch.class).iterator();
    }

    @Override
    public Runnable decorate(final Runnable runnable) {
        return () -> {
            final Properties properties = loadConfiguration();
            setupDatabase(properties);
            setupLogging(properties);
            runnable.run();
        };
    }

    private Properties loadConfiguration() {
        final Properties properties = new Properties(System.getProperties());
        of(ofNullable(System.getProperty("batch.configuration"))
            .map(File::new)
            .orElseGet(() -> new File(jarLocation(CustomCliConfiguration.class), "conf/batch.properties")))
            .filter(File::isFile)
            .ifPresent(file -> {
                try (final FileInputStream fis = new FileInputStream(file)) {
                    properties.load(fis);
                } catch (final IOException e) {
                    throw new IllegalStateException(e);
                }
            });
        return properties;
    }

    private void setupLogging(final Properties properties) {
        final String file = properties.getProperty("batch.logging.file");
        if (file != null) {
            try (final FileInputStream fis = new FileInputStream(file)) {
                final LogManager logManager = LogManager.getLogManager();
                logManager.readConfiguration(fis);
            } catch (final IOException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }

    private void setupDatabase(final Properties properties) {
        try {
            DriverManager.getDriver("jdbc:batch");
        } catch (final SQLException driverNotRegistered) {
            try {

                final String dbPath = properties.getProperty("batch.persistence.path");
                final String delegateUrl = "jdbc:derby:" + (dbPath == null ? "memory:batch"
: dbPath) + ";create=true";
                print("Using database: " + delegateUrl);
                if (dbPath == null) {
                    print("You can customize persistence path adding before '-jar': -Dbatch.persistence.path=<execution
persistence path>");
                }
                DriverManager.registerDriver(new ConfigurableDriver(delegateUrl)); // stupid
driver
            } catch (final SQLException e1) {
                throw new IllegalStateException(e1);
            }
        }
    }

    private void print(final String x) {
        System.out.println(x);
    }
}

// with

public class ConfigurableDriver implements Driver {
    static {
        new EmbeddedDriver();
    }

    private final String delegateUrl;

    public ConfigurableDriver(final String delegateUrl) {
        this.delegateUrl = delegateUrl;
    }

    @Override
    public Connection connect(final String url, final Properties info) throws SQLException
{
        return acceptsURL(url) ? DriverManager.getConnection(delegateUrl, info) : null;
    }

    @Override
    public boolean acceptsURL(final String url) throws SQLException {
        return "jdbc:batch".equalsIgnoreCase(url);
    }

    @Override
    public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) throws
SQLException {
        return new DriverPropertyInfo[0];
    }

    @Override
    public int getMajorVersion() {
        return 4;
    }

    @Override
    public int getMinorVersion() {
        return 2;
    }

    @Override
    public boolean jdbcCompliant() {
        return true;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException();
    }
}
{code}

> Create a File based PersistenceManagerService
> ---------------------------------------------
>
>                 Key: BATCHEE-92
>                 URL: https://issues.apache.org/jira/browse/BATCHEE-92
>             Project: BatchEE
>          Issue Type: New Feature
>          Components: jbatch-core
>    Affects Versions: 0.3-incubating
>            Reporter: Mark Struberg
>
> Currently we only have a 'transient' (in memory) batch info storage or we write it to
the database. 
> For cases where you e.g. use batchee-cli with the openejb embedded lifecycle and start
up your batch on the command line as own JVM it could be great to store all the checkpoints
etc into a file based PersistenceManagerService.
> That way one could restart the batch even if the JVM crashes for example.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message