Tag Archives: Java

Java 8: Parallel vs Sequential Stream Comparison

Motivated by the introduction of Lambdas in Java 8, I wrote a couple of examples to see how difficult it would be to follow a functional programming paradigm in real production code.

I will demonstrate using some features from Java 8 with a simple and fun example.

Continue reading

7 Languages in 7 Days: Day 1 – Java (1/7)

Intro

Getting Started

Let’s start with the 800lb gorilla in the room, Java. Why Java? Well, let’s start off with the fact that it’s got a ton of community support and documentation everywhere. If you Google a programming problem, chances are within the first three results you will see an example using Java. There is also a plethora of amazing editors available. A good editor is not a replacement for knowing the tools available in a language, but it can help you in learning.


Continue reading

ActiveMQ Memory Tuning

ActiveMQ is a great messaging broker.  However, using the default configuration is not recommended. This article will explain how I determined the appropriate ActiveMQ memory settings for one of our clients.

Cecil Williams
Cecil Williams

Latest posts by Cecil Williams (see all)

Continue reading

Customizing CSRF Protection In Spring Security

Starting in Spring Security 3.2, developers gained an easy solution to their Cross-Site Request Forgery problems with Spring’s implementation of the Synchronizer Token Pattern. Spring’s documentation does a great job of explaining Synchronizer Token Pattern and their implementation, so rather than talk about all of that, I’m going to show you how to tweak their configuration so you can have greater control over the urls that are protected.

Continue reading

Getting Started with Camel: Error Handling

Error handling is tricky. Not because it’s especially hard to do, but because everyone (operations, the business team, fellow programmers) seems to have a different idea of how a particular situation should be handled. A web service is down? No problem. You should try again every five seconds, but no more than 10 times. If the service doesn’t respond, send Operations an email, but don’t send me an email every time you fail to message it, just the 10th time.

These special requests result in “little gems” of code that are sprinkled throughout your application. They’re really important when everything is going wrong and ignored the rest of the time. It’s a shame really, some of the ridiculous stuff above is harder to write (and test) than some of the production code we’ve all written.

I know I’ve said this before, but I like Camel. It makes all the silly requests above trivial, and it gives me a mechanism for testing that I wired everything up correctly. Let’s look at a few ways to deal with errors that occur in your Camel routes.

Continue reading

My Top 4 Takeaways from NFJS 2013

**Android Development**
Google is working on Android Studio, which is built on the community edition of Intellij IDEA.  You can learn more and download the early access preview at http://android-developers.blogspot.com/2013/05/android-studio-ide-built-for-android.html.
A great book for Android development is “The Busy Coders Guide to Android Development” by Mark Murphy.  He uses an annual subscription model for the book and updates it regularly. He adds new material each month.  This way your book is never out of date. You can learn more at http://commonsware.com/Android/
It is possible to seed up the Android emulator!  Intel has developed the Hardware Accelerated Execution Manager (HAXM).  Selecting this as the emulator will drastically reduce the amount of time it takes to start the emulator.  You can learn more and download the HAXM at http://software.intel.com/en-us/articles/intel-hardware-accelerated-execution-manager.
Would you like to view your physical Android device on your computer?  Well now you can using a program called Droid@Screen by Jens Riboe.  You can learn more at http://droid-at-screen.ribomation.com/.
**HTML5 Offline Applications**
The HTML5 specification supports client side storage so that applications can work in an offline mode.  All of the latest desktop and mobile browsers support HTML5 client side storage. To tell the browser that you application supports client side storage you have to add the “manifest” attribute to the html tag at the top of the document.  You can learn more at http://www.ibm.com/developerworks/library/wa-offlinehtml/.
**CoffeeScript**
After seeing CoffeeScript, I don’t think I will ever write JavaScript again.  CoffeeScript is to JavaScript what Groovy is to Java.  It provides a much simpler syntax than JavaScript by incorporating language features from Ruby and Python.  CoffeeScript compiles into pure JavaScript and is compatible with all JavaScript engines.  You can learn more at http://coffeescript.org/.
**Server Side JavaScript**
There is a JavaScript runtime engine called Node.js.  The authors designed it for building fast, scalable network applications.  Node.js allows you to try JavaScript without needing a browser, by running the Read-Evaluate-Print-Loop (REPL).
Node.js has a large ecosystem.  For example, there is NodeUnit and Buster.js for testing, Jade for tempting, Express for web applications, and Connect for middleware.  Even CoffeeScript will run inside Node.js.  This ecosystem is managed using the Node Packaged Modules (NPM) registry.
NodeUnit and Buster.js provide new ways to test JavaScript, either within a browser or Node.js.  This is important since jsUnit is not longer actively maintained.

One of the best software development conferences you can attend is the No Fluff Just Stuff (NFJS) conference. The conference  is small and the speakers are mostly consultants working in the field.  I attended my 9th NFJS conference this year, and as always, came away with some great information that I can use right away.  So here are my top 4 takeaways from NFJS 2013:

Continue reading

Java method breakpoints are evil

I want to share an experience that my colleague, Travis Klotz, and I ran into recently.

I was trying to manually test a Java web application running in debug mode.  It was running really slow, taking several minutes to launch after the compile was finished.  And when it did eventually start, using the application was very slow.  Pages would take almost a minute to render.  So Travis and I started trying to determine the cause. Continue reading

Getting Started With Camel: Marshalling

In my last post on Camel, I spent some time introducing you two of its biggest players, the Context and the Route, by means of a simple problem – processing data coming in via file. If all we had to do was move the file, we could call our job done and move on, but what if we need to do something with that information?

Here’s a quick recap of our problem from last time.

  1. User puts file in directory.
  2. System reads file from directory.
  3. System breaks each line into a separate record.
  4. System processes each record.
  5. System archives the file.

I’ve marked off steps 1, 2, and 5 since we already took care of them with our simple implementation of the File Transfer pattern. Steps 3 and 4 offer us an opportunity to explore the Message Transformation Enterprise Integration Pattern (EIP), which covers interactions between systems that don’t share a common API (e.g, our file and a Java-based API that takes a plain-old-java-object (POJO)).

The Exchange

EIPs run on messages. They are read, sent, manipulated, and stored based on the individual needs of the system, and Camel’s API follows suit nicely. Remember our FileMover, the simple route we defined to grab a file out of a directory and park it somewhere else?

@Component
public class FileMover extends SpringRouteBuilder {
    @Value("${camel.ride.app.output.directory}") 
    private String outputDirectory;
 
    @Value("${camel.ride.app.input.directory}") 
    private String inputDirectory;
 
    @Override
    public void configure() throws Exception {
        from("file:" + inputDirectory).to("file:" + outputDirectory);
    } 
}

We already covered that this little route is building a connection between two of Camel’s EndPoint classes, but what happens in the middle? To start, Camel builds an Exchange, which will facilitate the transfer of a Message from one endpoint to the other. The exchange contains all the interesting bits and pieces of your message. You can get at this content several ways, but the simplest by far is with a Processor.

The Processor Interface

Attaching a Processor to an Endpoint is an easy way to hook into a route and see what is going on because a Processor has one method, process, which takes an Exchange.

@Component
public class SimpleProcessor implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        System.out.println("There is an exchange going on.");
        System.out.println(exchange.getIn().getHeader("CamelFileName"));
        System.out.println(exchange.getIn().getBody());
        System.out.println(exchange.getIn().getBody().getClass());
    }
}

All that’s left now is to inject this processor into our File Mover route.

@Component
public class FileMover extends SpringRouteBuilder {
    @Value("${camel.ride.app.output.directory}") 
    private String outputDirectory;
 
    @Value("${camel.ride.app.input.directory}") S
    private String inputDirectory;
 
    @Autowired
    private SimpleProcessor simpleProcessor;
 
    @Override
    public void configure() throws Exception {
        from("file:" + inputDirectory).process(simpleProcessor).to("file:" + outputDirectory);
    }
}

The above class gives one example of how we can solve our remaining steps, by grabbing the Body of the In message and breaking it into different objects, but there are better options, namely letting Camel marshal (or transform) that object from it’s incoming format into something we can use.

Marshalling to Spring Managed Beans

Camel’s marshalling functionality allows us to transform the data coming from our file in a variety of ways. For this example,
we’ll look specifically at comma-separate-value (CSV) file processing, but there are others available, most notably the ability to marshal to and from XML.

In a simple world, we might be handed a CSV file containing the following data:

John,Smith
Sue,Anderson

As you might guess, the values in this file represent the first and last name of a person. If we were to use a processor, like our example above, we would need stream the contents of the file and build Strings for each of the fields in our CSV, and then set them on the appropriate POJO. Instead, we’ll let Camel do the work for us, so all we have to worry about is getting a list of Strings for each row.

CSV processing is a separate module in Camel. To use it, add the following to your Maven POM file.

<dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-csv</artifactId>
     <version>${camel.version}</version>
</dependency>

Before we hook CSV processing into our route, we’ll need a bean to do something with the results. The Person class used here is just a POJO with a first and last name field.

public class CsvToPersonProcessor {
    public void process(List&lt;List&gt; csvRows) {
        for (List csvRow : csvRows) {
            Person person = new Person();
            person.setFirstName(csvRow.get(0));
            person.setLastName(csvRow.get(1));
            doSomethingTo(person);
        }
    }
}

One thing that’s a little odd here is the process method, which takes a list of a list of Strings. Why that particular method signature? The answer lies within Camel’s CSV library. Each row in our csv file is going to be broken into a list of Strings, and that list is going to be added into another list, so what you have at the end of the marshalling process is a list of all the rows from our CSV file. Yes, this means that your whole file will be in memory at once, which is bad, but hold on a second while we look at our new route definition, because we’ll get to that problem in a minute.

@Component
public class FileMover extends SpringRouteBuilder {
    @Value("${camel.ride.app.output.directory}")
    private String outputDirectory;
 
    @Value("${camel.ride.app.input.directory}")
    private String inputDirectory;
 
    @Override
    public void configure() throws Exception {
        from("file:" + inputDirectory).to("file:" + outputDirectory)
                    .unmarshal().csv().beanRef("csvToPersonProcessor", "process");
    }
 
}

Like before, we are picking up a file from one directory and moving it to another, but now, at the end, we are unmarshalling the CSV and sending the results to the process method of the bean we defined above. And like I said before, it’s going to do the entire file by default. So now, we need to stop doing that, before we run out of memory.

@Component
public class FileMover extends SpringRouteBuilder {
    @Value("${camel.ride.app.output.directory}")
    private String outputDirectory;
 
    @Value("${camel.ride.app.input.directory}")
    private String inputDirectory;
 
    @Override
    public void configure() throws Exception {
        from("file:" + inputDirectory)
                .to("file:" + outputDirectory)
                    .split(body().tokenize("\n")).streaming()
                    .unmarshal().csv()
                    .beanRef("csvToPersonProcessor", "process");
    }
 
}

In the class above, we are splitting the body of the message on each new line, and then streaming the result to the CSV unmarshalling process we had before. The big change here is that now our CsvToPersonProcessor is going to be invoked one time for each row in the CSV, which means our looming memory issue is gone.

Before I wrap everything up, you probably want to know what to do about the pesky column headers that come along with many CSV files. These are dealt with through an instance of the DataFormat interface injected into the marshalling/unmarshalling portion of your route. You are welcome to roll your own DataFormat, but the camel-csv module also includes a CsvDataFormat class which, as it turns out, you were using the whole time with your call to the csv() method. All you need to do is break it out as a variable, flip the “skipFirstLine” property, and inject the DataFormat into your unmarshal call.

@Component
public class FileMover extends SpringRouteBuilder {
    @Value("${camel.ride.app.output.directory}")
    private String outputDirectory;
 
    @Value("${camel.ride.app.input.directory}")
    private String inputDirectory;
 
    @Override
    public void configure() throws Exception {
        CsvDataFormat format = new CsvDataFormat();
        csv.setSkipFirstLine(true);
 
        from("file:" + inputDirectory)
                .to("file:" + outputDirectory)
                    .split(body().tokenize("\n")).streaming()
                    .unmarshal(csv)
                    .beanRef("csvToPersonProcessor", "process");
    }
 
}

It’s probably worth noting here that because we were using Camel’s Java DSL for all of the above examples, we didn’t need to make any additional changes to the XML snippet we defined last time.

What’s Next?

Our problem is solved, but what happens when things don’t go according to plan? In my next post, I’ll cover some of the error handling techniques available in Camel. The full source for the code above, including test coverage, is out on the marshalling branch of the camel ride repository on GitHub. Go crazy.

Getting Started with Camel, Java, and Spring

At home and at work, I find that the things that I have to do over and over are the most painful. At home, it’s the dishes; at work, it reading data out of a file or doing Hibernate mappings. No matter what I do, I can’t seem to escape them, even while, for the most part, the inventors of the tools I use have done a pretty good job of making the work less of a chore.

At a recent job, I was asked to do some comma-separated value (CSV) file processing. A Microsoft Access power user was going to be running some queries, and our business users wanted to alter the behavior of our system based off the records that he found. Multiple files were going to be produced each day, and dropped off in a directory where I could pick them up.
Continue reading

Iterators, Functors and Predicates

In this post I am sharing different ways to create ‘Custom Iterators’ and how to control the behavior of that Iterator.

Also part of this article focuses on how to apply different operations on a selected elements within a custom iterator.

The way to do this is to implement a new Iterator-subclass and pass it Functors and Predicates.

By the way Google has implemented all these and more within the Guava Framework, you just need to include this framework as part of your project.

So what are functors and predicates?

Functor: Functor is an interface implementation that apply an operation or status change on an object passed to it’s operation method.
Continue reading