Wednesday, April 20, 2011

CUFP | Commercial Users of Functional Programming

I found this to be an interesting reflection on Java and JVM alternatives. I am liking Scala more and more. I also like Clojure, but I have always liked the Lisp languages and Scheme in particular. But I feel Clojure would be such a culture shock for your average Java developer, that winning approval would end right there -- not to mention leadership support.

CUFP | Commercial Users of Functional Programming

Tuesday, March 15, 2011

Spring Batch -- Unplugged

Recently I was tasked with support a legacy warehouse management system (WMS) that replied on fixed width files to process online customer orders. The current system uses IBM MQ and process event triggers. Basically, you drop a fixed width message on a queue, MQ triggers a process to run a scripts on the file system, an executable gets the message off the queue and writes it to the file system.

The application that generates the message is IBM Websphere Message Broker. Not wanting to continuing using Message Broker, I proposed using Spring as an alternative. But choosing Spring meant re-doing the fixed width message mapping currently done in message broker. Then I remembered something I heard during a recent Spring Source training workshop my employer had sponsored: there was fixed width file support in Spring Batch.

Looking at Spring Batch, I found what I was looking for, notably the FlatFileItemWriter. Liking what I saw, I tried to wrap my head around what Spring Batch had to offer and how to use it. After immersing myself in online documentation, I came to the conclusion that the way Spring Batch worked did not fit the intended implementation I had in mind. My application was to be a fairly generic Message Driven POJO using basic XML Spring configuration. Whereas Spring Batch is for, well, batch jobs. Clearly not what I was looking for.

So I figured why not just use the classes I needed from Spring Batch and inject them into my application. I added the following to my application context to use the FlatFileItemWriter in my app:



<bean id="itemWriter015" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:rdm.txt" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="actionType,crmOrderNumber" />
</bean>
</property>
<property name="format" value="015%1s%9s" />
</bean>
</property>
</bean>


As you can see, there is also a mapper involve that uses the OXM marshallers from Spring to map an order XML to domain model objects. The domain model objects have standard getters and setters. The getters are utilized by the FormatterLineAggregator; another Spring Batch object.

FormatterLineAggregator

The FormatterLineAggregator has something called a field extractor which is an instance of the Spring Batch BeanWrapperFieldExtractor class. It is on the BeanWrapperFieldExtractor where you provide the list of properties you want to write out using the line aggregator. The values in this list must match the getter methods of the object you pass to the item writer's 'write' method. As in:


List sohList = new ArrayList();
sohList.add(stockOrder.getHeader());

try {
getFfiw015().open(new ExecutionContext());
getFfiw015().write(sohList);
getFfiw015().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Here I am passing something called a stock order object that has get methods for the actionType and crmOrderNumber members. These values were set by reading the XML and unmarshalling into the stock order domain model objects.

Spring OXM Unmarshalling

To configure XML unmarshalling to Java, here is the bean definitions from the application context:


<bean id="sodMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.gap.gid.stockorder.model.StockOrder</value>
</list>
</property>
<property name="schemas">
<list>
<value>classpath:com/gap/gid/stockorder/model/schema/omssod.xsd</value>
</list>
</property>
</bean>

<bean id="stockOrderMapper" class="com.gap.gid.stockorder.model.StockOrderMapper">
<property name="marshaller">
<ref bean="sodMarshaller" />
</property>
<property name="unmarshaller">
<ref bean="sodMarshaller" />
</property>
</bean>


After grabbing the XML message off the queue, hand it off to the mapper/unmarshaller:


InputStream is = null;
StockOrder stockOrder = null;
try {
is = new ByteArrayInputStream(xmlMessage.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stockOrder = (StockOrder) stockOrderMapper.readObjectFromXml(new StreamSource(is));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


The unmarshaller knows which elements to map to which object members by using annotations:


@XmlRootElement(name="StockOrder")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class StockOrder implements Serializable {

/**
*
*/
private static final long serialVersionUID = 9126450659071953120L;
/**
*
*/

@XmlElement(name="StockOrderHeader")
private StockOrderHeader header;

/**
* @return the header
*/
public StockOrderHeader getHeader() {
return header;
}

/**
* @param header the header to set
*/
public void setHeader(StockOrderHeader header) {
this.header = header;
}

}

Monday, January 31, 2011

Rails Sample App Status (part 3)

My new goal with getting a sample app up and running on my Ubuntu 10.04 vm is to use rails 3.0.3 and ruby version 1.9.2. I had trouble with zlib while installing gems, but found this bit of magic on the rvm site:

$ rvm package install zlib
$ rvm remove 1.9.2
$ rvm install 1.9.2 --with-zlib-dir=$rvm_path/usr

After that it was smooth sailing:

$ rvm use 1.9.2 --default
$ rvm use --create 1.9.2@rails
$ gem install sqlite3-ruby
$ gem install rails

After getting rvm, ruby and rails configured, I decide to try out the devise_example rails application using authentication.

This posed some issues with setup. The documented installation steps didn't quite work. When I executed 'bundle install' in the devise_example directory, the sqlite3 'gem' (which isn't really a gem), failed.

Seems you need this first:

$ sudo apt-get install libsqlite3-dev.

FYI, make sure you have your /etc/apt/apt.conf set up like this:

Acquire::http::proxy "http://user:pw@host.com:8080/"

But you knew that already, right?

A whole lot of issues when I tried to run 'rake devise:setup'. It was complaining about missing file openssl.h. OK, after poking around online I was able to see there were other folks who had a similar issue, but what worked for me was:

$ sudo apt-get install openssl
$ sudo apt-get install libssl-dev

After that, setup was able to finish.




Next comes the fun...trying out the devise_example.





Thursday, January 27, 2011

Rails Sample App Status (part 2)

Whew! Finally got rvm installed after many false starts. Boy, I _love_ open source! No, I really do. Where else can you work on cool technology and solve really challenging puzzles -- together!

Yet again, I had proxy issues. This time I needed to set the proxy in the .gemrc file in my home directory. You need a line like this:

gem: --http-proxy http://username:pw@proxyhost.com:8080

You fill in the blanks for your system if you are working behind a proxy.

The other thing I had to fix was the date. I am still working on getting the date to stay current on my Ubuntu 10.04 virtual machine. I am having network issues, but that's another story.

Because the date was way off, the rvm make step was confused and in an infinite loop filling up my disk with huge log files in the rvm log directory.

After manually setting the date, I was able to fetch and install ruby 1.8.7 and 1.9.2. I set 1.9.2 as the default for now.

Wednesday, January 19, 2011

Random Grails Links

I recently attended a webinar entitled "What's new with Groovy & Grails Tooling in the SpringSource Tool Suite?"

I found it to be somewhat interesting. It was nice to see Grails development in a Eclipse based environment. Though it is still not as refined as Java development, it is a vast improvement.

I asked about whether who can deploy grails applications to vanilla Tomcat as opposed to their tc server. The answer I got was no, not yet. I am not sure whether I like the idea of being forced to use tc server. But I suspect there is more that needs to be discovered. I personally don't know of any technical reasons why a grails application, which is supposed to be just a Java runtime application, can't be deployed to native Tomcat. I guess I can try and see what happens.

Anyway, here is the blurb sent from VMware [bill.anderson@vmware.com] after the presentation. It contains some useful links at the bottom which I intend to dig into. I want to try the eclipse plugin for grails with STS and regular eclipse.

Thank you for joining last week’s webinar “What's new with Groovy & Grails Tooling in the SpringSource Tool Suite?” The webinar recording and slides are now posted.

This webinar covered the recent tooling improvements for Groovy & Grails now available in the SpringSource Tool Suite.

- SpringSource Tool Suite (STS) - provides the best Eclipse-powered development environment for building Spring-powered enterprise applications and supplies tools for all the latest enterprise Java, Spring, Groovy and Grails-based technologies. Also included with STS is the developer edition of vFabric tc Server. Download SpringSource Tool Suite today!

- Grails is an advanced and innovative open source web application platform delivering new levels of developer productivity. Visit www.grails.org to download Grails 1.3.6, view available Grails plugins, and get the latest news.

- Groovy is the leading open source dynamic language for the Java Virtual machine offering a flexible Java-like syntax that most Java developers can learn in a matter of hours. Visit http://groovy.codehaus.org to download Groovy 1.7, read the release notes and access other resources.

Additional resources include:
- STS+Grails FAQ: http://www.grails.org/STS+FAQ
- STS+Grails getting started guide http://www.grails.org/STS+Integration#
- Groovy Eclipse homepage http://groovy.codehaus.org/Eclipse+Plugin
- Best place for help is the STS Forum http://forum.springsource.org/forumdisplay.php?f=32
- SpringSource Team Blog http://blog.springsource.com/

Wednesday, December 1, 2010

Rails Sample App Status

Oh well, it seems the restful authentication is incompatible with my version of ruby. I think it requires rails 2.1? At any rate, going to install rvm to see if I can play with the ruby versions and get it to work.

I am using:

balexander@ubuntu001:~/SampleApp$ gem environment

RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /var/lib/gems/1.8/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /var/lib/gems/1.8
- /home/balexander/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--http-proxy http://XXXX"
- :sources => ["http://gems.rubyforge.org", "http://gems.github.com"]
- REMOTE SOURCES:
- http://gems.rubyforge.org
- http://gems.github.com

balexander@ubuntu001:~/SampleApp$ script/console development
Loading development environment (Rails 2.2.3)

Monday, November 29, 2010

Rails Sample App Status

I got the sample rails movie application running and am now working on integrating the restful-authentication plugin from technoweenie on github.

Setting up proxy to pull the source from Git was somewhat of a pain though, git config --global http.proxy user:pw@hostname:port is your friend in this case.