Appliweb

All dimensions of your web developments

Affichage des articles marqués java

My work really consist in making different tools and java framework work together. I need to find solutions so that any developper can understand and use them afterwards.

This time the project involves developing lots of web services, and some batch treatments accessing to a single and complete business model. The business model is defined by an XSD file. This file will often change during the lifetime of the projet, so I need some automatic solutions. I decided to have a library containing the Java classes of the Business Model, and this library will be used by the web services and the batches. I’m using Maven, jaxb2 and jaxws, all under Netbeans, and I will explain how I managed to do that.

Generating jaxb2 beans using Maven:

First, I use the maven jaxb2 plugin to generate the corresponding Java Classes. Netbeans doesn’t support Jaxb2 mapping with Maven (only with ant), so I had to find and modify the pom.xml file manually. Once properly configured (with generated classes under target/generated-sources/xjc), the generated source code is automatically recognized by Netbeans (and greyed so that you know it’s generated, like in the screenshot below).

Creating the WebService:

Next, I create the .wsdl files, I’m not really expert in web services, so I create a web service using Netbeans (with the option to generate a wsdl from java classes), this was an easy way to get the .wsdl. Then I discard the other generated files from Netbeans, and told it to generate again the web service, this time using the .wsdl tweaked to import my Business Model .xsd.

The nice thing with Netbeans 6.7 and Maven, is that Netbeans modifies your maven project when you create a web service. The web service generation is really done by the maven project (using jax:ws plugin) and so is not dependent at all of Netbeans. Using other IDEs, you’ll have to create the web service using the wizards, and then manually modify the Maven project. Below is the modification made by Netbeans to the maven project file:

The problem:

When generating the Java source from the wsdl, the imported xsd is mapped again to Java classes. With the same files than the ones generated by Jaxb2. So I tried to tell the jaxws plugin that the .xsd file was already mapped. Documentation says you can use « episode » files, but this only works if the namespace of the webservice is different from the namespace of the XSD. That was not the case.

One solution is be to re-generate the source code, and delete the duplicated source so that only the classes in the business model project are used. That should work, but I need to keep some generated java classes: The ones from the Web Service itself. So I want to generate the .xsd classes in another package than the classes from the .wsdl (all with the same namespace !!)

The solution:

After lots of trials and errors, I eventually managed to do this by carefully crafting two files: one for jaxb and the other for jaxws, like this:

The content of  bindings-jaxb.xml:

And the content of bindings-jaxw.xml:

I get the web service interface and factory generated  in com.xxx.xxx.ws.mediation.service, and the source code from the business model in com.xxx.xxx.sas.data. It was then really easy to tell maven project to delete duplicates generated source code before compilation.

And as Netbeans compiles using maven, I get this behavior completely integrated into my IDE.

GC.

Following the first post about « using Netbeans for an IDEA user », I would like to add some more informations about maven support.

Skipping unit tests:

Each time I compile my maven project in Netbeans, it runs the unit tests, even for small changes. Fortunately, you can easily tell Netbeans to avoid unit test during compilation. Simply right-click on the project, then select Properties. You can check the « Skip tests » control like in the dialog above:

Importing a maven Web project:

I have successfully opened a Maven project that generates a .war file into Netbeans. It is recognized as a web project, you simply by sets the target application server, and then you can run and debug your web application.

It generates the .war file using Maven, and deploys it automatically to the application server.

The neat feature with Web projects:

It’s easy to use, and great for day-to-day web development: It’s tedious to wait for Maven to generate the war and deploys it each time you modify a .jsp or a .xhtml page. So Netbeans automatically deploy the webapp resources files when you save them. Great ! And IDEA does it too.

In this third installment of the Android Game Development series, I’ll try to explain how to construct the project. I know the LunaLander sample directly uses thread and SurfaceHolder.Callback, but my first goal is simply to display an image, I will complexify later.

Games don’t use Views

In Android, most of the games don’t really use the View hierarchy of objects, because it’s too slow and cumbersome. They instead write directly to a Canvas.

In order to get the Canvas displayed to the screen, you create a SurfaceView object.

Thus, the page is a  FrameLayout, and inside we put our SurfaceView class, as shown in the main resource file:

How to insert an Image into Android project ?

Using Gimp, I have drawn a small red ball:  Nice isn’t it ?

How can I use it in my project. Well it’s quite simple, you copy the file in your project res/drawable subdirectory, you update your Eclipse Project (F5 key). Automagically, you see your new image in the project, and the Eclipse Plugin regenerates the R class so that you can easily access the resource in your code or Layout file. Just take a look at this sample (called by the constructor of our SurfaceView):

We can now easily override the onDraw method on our SurfaceView derived class, and display the image:

Notice we set the position and dimension to display in the image itself, and tells it to display into the canvas. I would have expected the other way round (calling a Canvas method to draw the image).

I run the  project and here is the result :

Well something got wrong, but what ? Maybe I should check in Android documentation how to see some sort of logs or whatever, but for now, each time I get this, I’m clueless about what went wrong.
This time it was easy, I put the wrong class name in the layout.xml file, and it couldn’t find my SurfaceView class. Too bad Eclipse didn’t warn me about this. I fixed the error, started again, and then:

A Black screen. No Error but no red ball neither….

Running it under debugger shows me that the onDraw method on my SurfaceView class is not called !

After some more experiments, (checked the LunarLander sample, tried to draw a big green square on Canvas, etc….) I finally managed to make it work.
To have the SurfaceView displayed, you have to set a background color. With my init method like that:

I get the « good » result as follow: Yeepeee !!!


GC.

Welcome into part 2 of the Android blog series !

Today, I will try to explain the concepts I discovered when creating the project.

Finding a Sample:

When trying new libraries or new functionalities, I always try to find sample code I can analyse and copy to my own project. Fortunately, Android project pages contains such samples.

One of them seems to be a perfect starting point for my game: The Lunar Lander Sample. It’s a classic game where you want to land a lunar module by switching off and on reactors. The difficulty is that the module follows gravity rules, and you really need to predict it’s behavior.

Remember this game now ?

The documentation explains to a great extend the source code of the sample, so I won’t repeat it here. Well, just in two words then.

General Guidelines:

The Main class is called an Activity. An Activity is like a controler (in MVC pattern), there can be many activities in a single application. Think of it has a screen in your application. It creates a View class, either programmatically or by reading an XML description file. The View class represents, errr…. the View part of the MVC pattern.

All images, view xml files and string resources are parsed by a sdk tool that generates a class, the R class (what’s this name ???), that contains the Ids of these resources. You can write your application like that:

- You design you application U.I. using the XML file (and the plugin shows you instantly the result).

- You override the  onCreate function in your activity class to load and set the view to display.

- From the View class you just created, you get the Java classes of your controls  (buttons, input fields) by using their Ids in the generated R.class

-  You then can get / set  the values of these controls, listen to a click, etc….

- Of course, for a game, you don’t use the View classes hierarchy, but directly draw into a canvas. More on that later.

NB: The eclipse plugin handles SDK tools for you.  It re-generates automatically the R.class (Did I told you the name was awful ?) whenever you create a new resource, it immediatly shows a bad reference in your layout file, it can display the result of your layout (screen below), and it packs the project and installs it into the emulator when you click on the Eclipse Run button.

GC.

As a side project, I’m trying to develop a small game in Android.

As you know, Android is the new open source platform, designed by Google, for smartphones.

It is based on Linux, and the language used to develop applications on it is Java. So I decided to try to develop a small application for it: A game.

I will try to  explain here the things are discovered in this journey.

The project:

A very simple game that uses the touch screen capabilities of the phone: You throw a ball using you fingers and it must either go into holes, avoid pitfalls, bump into, errr…. bumpers and so on. You will have many different levels with many kinds of obstacles to overcome. This game will involve precision in your fingertips !

Starting:

First I downloaded the Android SDK, along with Eclipse and it’s plugin. All is clearly explained here and installs without an itch. Along with the SDK comes a smartphone emulator, that shows this:

Nice no ?

The Eclipse plugin integrates Android SDK seamlessly with eclipse: You can create a new android project, run it in the emulator, design the screen (with preview), etc….

Next part will talk about creating the game project using a provided sample  project.

s you may know, I was using the trial version of IntelliJ IDEA to develop Appliweb.

Alas, the version expired, so I decided to switch to Netbeans 6.5.1. No I’m not using Eclipse, because I don’t like how it is organized.

I will try to highlight the differences I see between the 2 IDEs.I’ll try to stay neutral, so that you can choose which you prefer.

Today we talk about Maven support:

  • IDEA imports maven pom.xml files to recreate it’s own project, whereas netbeans uses directly the maven files. Netbeans compiles using maven, not IDEA. It imports correctly sub-projects, and sub-sub-projects.
  • Completion in pom.xml file seems to be the same. Maybe Idea completes a little more data.
  • Changes you made in pom.xml files are faster to import in Netbeans. Almost immediate.
  • Netbeans uses Maven when building or Running a Unit test. That’s a good and a bad thing.
  • Good thing: More often than not, you get the unit tests running in your IDE and not in Maven. With Netbeans, you’re sure that it works in maven, hence in the Integration servers.
  • Bad thing: In netbeans, each build runs all the unit tests by default (sloooow), and running a unit test class takes a long time because it is run with maven.

I’m really pleased for now by the overall behavior of Netbeans, kudos for the maven support.

GC.

One of the recurrent task in my job is to create batch programs in Java.

Some are extraction batches (we get data from a DB and write it to an XML file), some are import (from XML to DB), and some are DB treatments only. All these batches are using Spring-Batch Library to do their tasks.

Spring Batch is a well thought-out library, that, among other things, allows you to:

  • Handle large data by splitting it in small chunks
  • Restart a batch exactly where it has stopped
  • Define how many handled chunks per commit steps
  • Automatic Rollback / Retry in case of error
  • Etc….

The diagram below shows a typical execution of a batch:


To simplify things, let’s say a batch is composed of steps, and a Step is a Reader and a Writer communicating together.

  1. At the beginning of the step, the Reader and  Writer are opened
  2. The Reader opens a cursor on the DB, using a query.
  3. The Writer creates the XML file to write
  4. Then for each element read by the reader (For each row of the result set, we convert it into a Java Object)
  5. The element read is sent to the Writer
  6. The Writer convert it to XML then write it to the file.
  7. When the Reader returns null, that means no more data to process
  8. The Step tells the Writer to flush all it’s data
  9. The Step closes the Writer and Reader.

Recently, I was asked to optimize a slow extraction batch.

Profiling the code,  I see that the Reader must execute 15 SQL requests to get all the data to send to the Writer. As all elements are handled sequentially, that meant 150 000 requests for 10 000 elements ! A quick look at the database status with a batch running showed me that the database was not fully utilized.

I decided to Multi-thread the queries done in the Reader. By default, Spring-Batch does not easily support Multi-threading, and I didn’t want to do much change in the existing code,  so I used the famous Java Concurrent API features of Java.

In my multithreaded batch, the Reader don’t execute the queries to get data for the Writer, but instead creates a FutureTask, and adds it to a Multithreaded ExecutorService. This FutureTask is then send to the Writer  which  ony stores it.

When writer.flush is called, we get the data one by one from each of the FutureTasks. All the threads of the ExecutorService are busy querying the DB to fill the data for the FutureTask. Thus all the reading will be multi-threaded.

Next, let’s deep a little bit into the code:

We create  a Callable class that will execute the queries, we suppose we want to read contracts from the DB:

1
2
3
4
5
6
7
8
9
10
11
12
protected  class ContractCreator implements Callable  {
protected long elementId;

public ContractCreator (long elementId)
{
this.elementId=elementId;
}
public Contract call() throws Exception {
// Called by the threads
// Execute all the queries here
// And return the real data object
} }

The reader creates the ExecutorService with a pool of 10 threads:

1
protected ExecutorService executor=Executors.newFixedThreadPool(10);

Then the reader creates the Callable class for each element to read and submits it to the threads of the ExecutorService.

1
2
3
4
5
6
public Object read (ResultSet rs, int rowNum) throws SQLException {
long elementId=rs.getLong("ELEMENT_ID");
// Multithreaded execution
Future result=executor.submit(new ContractCreator (elementId));
return result;
}

The Future created is sent to the Writer which only stores it.

1
2
3
4
5
6
7
8
/**
* We store all the contracts running in parallel here
*/

protected List> contracts=new ArrayList> (10);

public void write(Object output) {
contracts.add((Future) output);
}

It’s only when flush is called that the Writer tries to get each contract and write them to the XML file:

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void flush() throws FlushFailedException {
for (FuturecontractCreator:contracts)
{
// Get the data read by the threads
// Will block until the data is avaiable
Contract contract = (Contract) contractCreator.get();
writeContractToFile (contract):
}
contracts.clear();
super.flush();
}

We then have now 10 threads querying the DB in parallel with little efforts ! All the basic thread / synchronization handling stuff is done by the Concurrent API, and the resulting file is exactly the same as with the single threaded example.

I hope this Spring-Batch example allows you to understand better the power of the Concurrent API of Java !

I totally agree with what the blogger said about swing 2.0: Here

The Java language has much evolved since Swing and Swing API should use the « new » language features.

And while we’re at it, Java beans API should be updated too. Support for Collection should be added for example.

A Web Designer

My business is to develop web applications in Java.

I would like to talk about a simple business application I do, where users insert / review data from a database.
We choose the standards librairies: JSF-Seam-Hibernate with RichFaces as the U.I. components.
This time, we were lucky enough to hire a web designer ! Big Time !

Let me share with you some of the things I’ve learned working with him.

Organization

First, how the development was organized ?

1. The web designer write the screens in jpg using Photoshop and  Illustrator

2. After several round-trip with the client, the « final » version eventually emerges

3. The web designer then writes the pages in HTML / CSS with the same look and feel

4. I develop a basic JSF page with the Seam backing beans. It only displays the needed information

5. We incorporate its HTML / CSS  into my JSF pages.

The last step is the  tricky one, so let me update with the problems we encountered….

HTML to JSF

CSS problems

The web designer of course came with it’s own set of css styles, with it’s own way of creating the page structure and graphics.
However, the RichFaces JSF library comes with it’s own set of css styles and page structure.

For example, the datatable component generates an HTML <table>, whereas the web designer was expecting <div>. We overcome this problem by changing its HTML with <table>. It’s quite understandable to use a <table> for displaying tabular data in HTML. It’s only by using <table> to position precisely elements that you make a mistake.

Aligning text with images.

Next problem : « align » attribute is not generated by the JSF graphicImage component.
Of course, our web design uses this attribute to align text with image…. Once again, the web designer changed it’s page design to support this. It is now better to use CSS to do this anyway.

A damned simple button

This one is really scary. The web designer don’t use <input type= »submit » /> in it’s forms, but <button>.
Never seen this tag anywhere, but it’s a standard one.

The web designer used it to enable the rollover effect, and to insert <span>…</span> around the text to apply images in the button via css. It’s button was: <button type= »submit »><span><span><span>Submit</span></span></span></button>
I haven’t seen any JSF library with a command button generating HTML.

Of course, you can tell me to create one new JSF Component, but common, I’ve already created some, and it’s way too complex for a simple button ! I have to rewrite the HTML renderer, manage all the subtle things in the implementation…
Currently, our solution is to created a javascript code (called using click event) to submit the form. Not very nice.

Conclusion

We’ve done only the first page of our application, and I’m really wondering if we took the right library. I believe now that in case you have a web designer in your team, you shouldn’t use libraries that generate HTML for you, like JSF or gwt, but instead use a framework allowing to generate the precise HTML the designer wants (maybe Tapestry, or plain JSP ?)

Hi,

I found some other neat things while editing Spring files in my project using IDEA.

1- Searching for the Usage of a class shows  where it is used in Spring XML files:

Here you can see it has found that DbHelper is used in several spring xml files. Cool !

2- Completion works for .properties values

When you edit a Spring file, you can  set values by referencing a property defined in a .properties file.
For example, in the screenshot below, by using ${DbSchema.commun} as a value of an bean attribute, Spring can be instructed to load the masteri.properties file, and replace  ${DbSchema.commun} with the value defined there.

IDEA is so smart (well it’s development team is), that code completion will display the properties defined in the .properties files!

Another very handy feature….

GC