Feed aggregator

Why Iterative Planning?

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:30
Originally written by Jann Thomas at the LeadingAgile blog. Preview Text:  Planning and elaboration go hand in hand as items move from unknown problem -unknown solution to known problem-unknown solution to known problem – known solution. Legacy Sponsored:  unsponsored ...
Categories: Java

Big Data & Machine Learning Convergence

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:30
More Linear Algebra and Scalable Computing I recently had two pretty interesting discussions with students here at TU Berlin which I think are representative with respect to how big the divide between the machine learning community and the Big Data community still is. Preview Text:  As these two fields converge, work has to be done to provide the...
Categories: Java

R: Rook - Hello world example - 'Cannot find a suitable app in file'

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:30
I’ve been playing around with the Rook library and struggled a bit getting a basic Hello World application up and running so I thought I should document it for future me. I wanted to spin up a web server using Rook and serve a page with the text ‘Hello World’. I started with the following code: Preview Text:  I’ve been playing around with...
Categories: Java

Unit Testing – You’re Measuring it Wrong

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:30
I’ve been having this problem ever since I’ve started teaching (and preaching) about SCRUM, clean code, unit testing and TDD and any other software development practice. When implementing a change - how do you measure its success? For that matter – how can you measure a developer productivity? Preview Text:  I’ve been having this...
Categories: Java

Real-time Alerting on Anomaly and Inactivity Made Simple

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:30
  Preview Text:  When someone is looking to be alerted when something unexpected happens within their environment they are usually referring to anomaly detection. But the problem is that it’s hard to turn a complex problem (i.e. looking for something when you are not sure what it is) into an easy to use solution. Legacy ...
Categories: Java

InfoSec Insanity: Sharing the Crazy for the Betterment of Online Security

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:10
I was getting a little fed up with the craziness I kept seeing on the web when it comes to security, so I created this: Preview Text:  I was getting a little fed up with the craziness I kept seeing on the web when it comes to security, so I created this: That’s right, a great big freakin’ padlock with a straightjacket or more to the point, I...
Categories: Java

Computational Actuarial Science, with R

Javalobby Syndicated Feed - Tue, 26-Aug-14 22:00
The book Computational Actuarial Science, with R is officially out. In the introduction of the book, and on the website of CRC, it is mentioned that the datasets can be found “in an R package on CRAN“, which is unfortunately incorrect. Some datasets are too large, so the package can not be uploaded on CRAN. Hopefully, Christophe host the package on his website. Preview Text:  ...
Categories: Java

Writing JSR-352 style jobs with Spring Batch Part 1: Configuration options

codecentric Blog - Tue, 26-Aug-14 02:19

Spring Batch 3.0 supports writing and running batch jobs that comply with the JSR-352 specification, which is the standard for batch processing also included in JEE7. This article series focuses on three topics:

  • configuration options using Spring Batch’s implementation the standard way
  • integrating the possibility to run JSR-352 style jobs in your existing Spring Batch environment
  • using Spring’s dependency injection functionality within JSR-352 style jobs

Today we’ll take a look at the first two topics.

The JSR-352 spec tells us that a job may be started this way:

JobOperator jobOperator = BatchRuntime.getJobOperator();
Properties jobParameters = new Properties();
jobOperator.start("<name of job xml without suffix>", jobParameters);

where the job xml is placed in the classpath under META-INF/batch-jobs and may look like this:

<?xml version="1.0" encoding="UTF-8"?>
<job id="simpleJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/jobXML_1_0.xsd">
    <step id="chunkStep">
        <chunk item-count="2">
            <reader ref="de.codecentric.batch.item.DummyItemReader"/>
            <processor ref="de.codecentric.batch.item.LogItemProcessor"/>
            <writer ref="de.codecentric.batch.item.LogItemWriter"/>
        </chunk>
    </step>
</job>

Spring Batch supports this kind of execution, so somewhere while creating the JobOperator the whole Spring Batch configuration must be set up. So, what’s happening when you call BatchRuntime.getJobOperator()?

How the standard configuration works

The class BatchRuntime is provided by the spec, it just uses the service loader capabilities of the JDK to identify the implementation of JobOperator. In our case Spring Batch provides the text file META-INF/services/javax.batch.operations.JobOperator in its spring-batch-core distribution, and its content leads us to the class that gets instantiated when we call BatchRuntime.getJobOperator():

org.springframework.batch.core.jsr.launch.JsrJobOperator

The service loader mechanism instantiates this class via reflection with the no-arg constructor, which is this one:

public JsrJobOperator() {
    BeanFactoryLocator beanFactoryLocactor = ContextSingletonBeanFactoryLocator.getInstance();
    BeanFactoryReference ref = beanFactoryLocactor.useBeanFactory("baseContext");
    baseContext = (ApplicationContext) ref.getFactory();
    baseContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    if(taskExecutor == null) {
        taskExecutor = new SimpleAsyncTaskExecutor();
    }
}

Okay, so we’re using the ContextSingletonBeanFactoryLocator here to get the ApplicationContext containing the Spring Batch configuration. First of all it means that no matter how often we call BatchRuntime.getJobOperator(), the configuration behind it is only initialized once and kept in the ContextSingletonBeanFactoryLocator.

So, how does the ContextSingletonBeanFactoryLocator work?
It looks for a beanRefContext.xml somewhere in the classpath. This ApplicationContext xml normally doesn’t contain “normal” Spring beans but references to other xml configurations. In our case the beanRefContext.xml from the root of the spring-batch-core jar is found. It contains one reference to the baseContext.xml in the same location. Finally we found the location of the Spring Batch configuration used, so let’s take a look inside.
JobRepository, JobLauncher, a classic Spring Batch JobOperator, JobExplorer, DataSource, TransactionManager, a database initializer, JobParametersConverter, JobRegistry and a PropertyPlaceholderConfigurer are configured here, everything you need to get jobs running.
What can we do to customize this configuration? At the end of baseContext.xml the PropertyPlaceholderConfigurer is defined:

<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
        </list>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="ignoreUnresolvablePlaceholders" value="false" />
    <property name="order" value="1" />
</bean>

There are a lot of properties used in baseContext.xml, for example for defining the database connection and so on. They are loaded from a properties file with this definition:

classpath:batch-${ENVIRONMENT:hsql}.properties

If you don’t set an environment variable or JVM parameter named ENVIRONMENT, the default batch-hsql.properties is used, which gives you an in-memory HSQL database.

How to customize the standard configuration

If your customization is done with specifying connection data for your individual database or changing some other of those properties, just create a batch-{your-database}.properties, set the properties in it, place it in the root of your classpath and set the environment variable or JVM parameter ENVIRONMENT to {your-database}.

If you need to change individual beans, create a baseContext.xml with your bean definitions and place it in the root of your classpath. For example, this is necessary if you want to access resources via JNDI when running in an application server environment. DataSource, TransactionManager and TaskExecutor will be different then.

Integrating into existing Spring Batch infrastructure

What do you do if you already have your custom Spring Batch infrastructure, JobRepository, JobLauncher, database and so on, and just want to add the possibility to start JSR-352 style jobs?

That was the challenge we were facing in our own open source project spring-boot-starter-batch-web that already handled Spring Batch xml and Spring Batch JavaConfig. We didn’t want to establish another line of configuration objects, we wanted to re-use our JobRepository and co. And, to be honest, we wanted to get rid of those service locator magic and do normal dependency injection.

So we decided to instantiate the JsrJobOperator ourselves and provide the necessary dependencies to it. Unfortunately at the time of writing, the JsrJobOperator didn’t support that, because it’s not possible to set the baseContext inside JsrJobOperator to our own parent context. baseContext is private, static and has no setters.

We patched the JsrJobOperator and made it implement ApplicationContextAware (take a look here), so when it is created now inside an existing ApplicationContext, it’ll take that as baseContext. There’s already a Jira issue with this subject.

Now we can re-use JobRepository, JobExplorer and TaskExecutor to run JSR-352 style jobs with the JsrJobOperator.

Conclusion

Customization is pretty easy if you know how to do it, but unfortunately the docs are lacking there a little bit. Integrating the possibility to start JSR-352 style jobs into an existing Spring Batch infrastructure can, at the moment, only be done with a little patching.

The post Writing JSR-352 style jobs with Spring Batch Part 1: Configuration options appeared first on codecentric Blog.

Categories: Agile, Java, TDD & BDD

Publish JAR Artifact using Gradle to Artifactory

Javalobby Syndicated Feed - Mon, 25-Aug-14 23:00
So I have wasted (invested) a day or two just to find out how to publish a JAR using Gradle to a locally running  Preview Text:  So I have wasted (invested) a day or two just to find out how to publish a JAR using Gradle to a locally running Artifactory server. Legacy Sponsored:  ...
Categories: Java

Refcard Expansion Pack: Getting Started with Apache Hadoop

Javalobby Syndicated Feed - Mon, 25-Aug-14 23:00
This week, DZone released its latest Refcard: Preview Text:  This week, DZone released its latest Refcard: Getting Started with Apache Hadoop. If you're interested in learning more about Hadoop or sharpening your skills, we decided to dig into the DZone archives and find some of the most popular posts we've had on the topic. ...
Categories: Java

Microservices and PaaS (Part 2)

Javalobby Syndicated Feed - Mon, 25-Aug-14 23:00
[This article was written by John Wetherill.]This is a continuation of the Microservices and PaaS - Part I blog post I wrote last week, which was an attempt to distil the wealth of information presented at the microservices meetup hosted by Cisco, with Adrian Cockcroft and others presenting. Preview Text:  Part I provided a brief background on...
Categories: Java

Setting up Java Applications to Communicate with MongoDB, Kerberos and SSL

Javalobby Syndicated Feed - Mon, 25-Aug-14 23:00
By Alex Komyagin, Technical Services Engineer at MongoDB Setting up Kerberos authentication and SSL encryption in a MongoDB Java application is not as simple as other languages. In this post, I’m going to show you how to create a Kerberos and SSL enabled Java application that communicates with MongoDB. My original setup consists of the following: 1) KDC server: kdc.mongotest.com kerberos...
Categories: Java

The Full-Time Developer 'Curse'

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
Some time ago I wrote an article with my advice to (junior) developers about their career. Preview Text:  I work as a full-time developer : This practically means that I spend 9 hours daily working with legacy code, existing or new products of a specific domain. Legacy Sponsored:  ...
Categories: Java

Thoughts on Hibernate

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
The main purpose of Hibernate/JPA was to address and solve the Object - Relational impedance problem. Java is an object-oriented language and there is no way but to use classes and methods. So we design and develop our Java applications in the object-oriented way. Preview Text:  The way data are laid out in the columns of tables and the way they...
Categories: Java

Remote JMX Monitoring of a Mule Instance

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
In this post I will describe how to enable monitoring of a remote Mule instance using JMX. In addition I will also enable the MX4J web interface that will expose the JMX properties of the Mule instance in a web application and I will install the Jolokia Mule agent, which makes it possible to use Hawtio to monitor the Mule instance. Preview Text:  ...
Categories: Java

Java Numeric Formatting

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
I can think of numerous times when I have seen others write unnecessary Java code and I have written unnecessary Java code because of lack of awareness of a JDK class that already provides the desired functionality. Preview Text:  I have used this post to look at how NumberFormat can be used to present numbers in different ways (currency,...
Categories: Java

Parameterized Test Runner in JUnit

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
We all have written unit tests where in a single test tests for different possible input-output combinations. Lets look how its done by taking a simple fibonacci series example. The below code computes the fibonacci series for number of elements mentioned. Preview Text:  We all have written unit tests where in a single test tests for different...
Categories: Java

Partitioned Clusters Tell No Lies!

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
The problem You are happily running a 10-node cluster. You want failover and speed and are using distributed mode with 2 copies of data for each key (numOwners=2). But disaster strikes: a switch in your network crashes and 5 of your nodes can't reach the other 5 anymore! Now there are two independent clusters, each containing 5 nodes, which we are smartly going to name P1 and P2. Both P1 and...
Categories: Java

Couchbase Java SDK 2.0.0 Beta 1

Javalobby Syndicated Feed - Mon, 25-Aug-14 22:30
Originally written by Michael Nitschinger On behalf of the whole SDK team I'm happy to announce the first beta release of the Java/JVM SDK release train nicknamed Armstrong. It contains both the JVM core package "core-io" 1.0.0-beta as well as the Java SDK 2.0.0-beta. Preview Text:  On behalf of the whole SDK team I'm happy to announce the...
Categories: Java

Thread Slivers eBook at Amazon

Syndicate content