Friday, September 5, 2008

Websphere Application Server Community Edition (WAS CE) Clustering using WADI

by Rockfield.

Working on the latest version of BizAgi, we were looking for an app server that supported JDK 1.6, EJB 3.0 and, of course, clustering. One of our targets was WAS CE, and as the WAS CE Cluster through WADI is explained at the IBM website, you might think it's easy to build. However, as long as you start following the instructions you'll soon realize it's not as simple, clear and quick as shown in there. So this will be a "for dummies" walkthrough for most people who are not expert enough to overcome the possible problems that might occur when building the cluster.


Previous considerations
  • For this specific example WAS CE 2.1 was previously installed on 2 machines called server1 and server2. No special configuration was used, just the plain default installation.

  • Default ports are used, and they are the same for both machines.

  • In order to check the cluster behavior, a load balancing HTTP server is necessary. A plain Apache HTTP Server installation and configuration is provided at the end of this tutorial.

  • <WASCE_HOME> is the WAS CE installation directory.

WAS CE cluster configuration

NOTE: These steps must be executed on both servers!
  • Go to the file <WASCE_HOME>/var/config/config-substitutions.properties and assign a name to each node of the cluster replacing the value of the clusterNodeName property (by default this property's value is "NODE"). For this example we used "node1" on server1 and "node2" on server2.
    clusterNodeName=node1
  • On the same config-substitutions.properties file go to the RemoteDeployHostname property and replace the default value "localhost" with the actual server name or IP address.
    RemoteDeployHostname=server1
  • Go to the <WASCE_HOME>/var/config/config.xml file (NOTE: Make sure the server is NOT RUNNING while editing the config.xml, because when stopped all changes made while running will get lost) and look for the following tags in order to switch the load property to "true" if it does exist and is set to "false":
    <module name="org.apache.geronimo.configs/tomcat6-clustering-wadi/2.1.1/car" load="true"/>
    <module name="org.apache.geronimo.configs/wadi-clustering/2.1.1/car" load="true">
    <module name="org.apache.geronimo.configs/farming/2.1.1/car" load="true">
    NOTE: If the value is set to "true" while not running, when the server starts up, the load property disappears. It is only shown explicitly when its value is set to "false".
  • On the same config.xml file, look for the org.apache.geronimo.configs/farming/2.1.1/car key. You must add all the cluster nodes except for the current one which is already there by default. This is how the key would look like for our server1:
    <module name="org.apache.geronimo.configs/farming/2.1.1/car">
    <gbean name="NodeInfo">
    <attribute name="name">${clusterNodeName}</attribute>
    </gbean>
    <gbean name="ClusterInfo">
    <attribute name="name">${clusterName}</attribute>
    </gbean>
    <gbean name="org.apache.geronimo.configs/farming/2.1.1/car?ServiceModule=org.apache.geronimo.configs/farming/2.1.1/car,j2eeType=NodeInfo,name=NodeInfo2" gbeanInfo="org.apache.geronimo.farm.config.BasicNodeInfo">
    <attribute name="name">node2</attribute>
    <attribute propertyEditor="org.apache.geronimo.farm.config.BasicExtendedJMXConnectorInfoEditor" name="extendedJMXConnectorInfo">
    <ns:javabean class="org.apache.geronimo.farm.config.BasicExtendedJMXConnectorInfo" xmlns="" xmlns:ns4="http://geronimo.apache.org/xml/ns/attributes-1.2" xmlns:ns="http://geronimo.apache.org/xml/ns/deployment/javabean-1.0">
    <ns:property name="username">system</ns:property>
    <ns:property name="password">manager</ns:property>
    <ns:property name="protocol">rmi</ns:property>
    <ns:property name="host">server2</ns:property>
    <ns:property name="port">1099</ns:property>
    <ns:property name="urlPath">JMXConnector</ns:property>
    <ns:property name="local">false</ns:property>
    </ns:javabean>
    </attribute>
    </gbean>
    </module>
  • NOTE: On the other hand, for our server2's configuration the <attribute name="name"> tag's value would be "node1", and the <ns:property name="host"> tag's value would be "server1".

Starting the cluster

Once the node configuration is done, start the cluster nodes as you usually would if they were stand alone. In order to verify that they are clustered, on the console screen of the first running node will appear the following notification when the second node would had started:
Dec 12, 2012 11:11:53 AM org.codehaus.wadi.tribes.WadiMemberInterceptor memberAdded
INFO: memberAdded:tcp://{-84, 16, 2, 16}:4000

Application configuration

  • Add a <distributable/> tag in your application's web.xml file.

  • Check inside your deployment plan xml file, at the war definition, and add the <dep:tomcat-clustering-wadi> tag so it would look like this:
    <dep:environment>
    <dep:moduleId>
    <dep:groupId>com.javaxcross.tests</dep:groupId>
    <dep:artifactId>WarTest-war</dep:artifactId>
    <dep:version>1.0.0.0</dep:version>
    <dep:type>war</dep:type>
    </dep:moduleId>
    </dep:environment>
    <dep:tomcat-clustering-wadi></dep:tomcat-clustering-wadi>
    where, of course, you should replace the groupId, artifactId and version tags with you own app's data.

Application Deployment

In order to deploy an application on the cluster, the web console does not have the necessary options, so you must open a command console window and execute the following instruction:

deploy.bat --user <name> --password <word> <command> --targets <target> <module> <plan>

where...

  • you'll find deploy.bat (or deploy.sh for unix like o.s.) at <WASCE_HOME>/bin/deploy.bat

  • <name> is the username and <word> is the password you use to login the web console. Default values are: system/manager.

  • <command> is the required instruction to execute, in this case could be deploy or distribute. The difference relies on deploy starting the application after the distribution, while distribute doesn't.

  • Replace <target> with the destination repository. which must be MasterConfigurationStore, because of the cluster. If you need to know the exact data you should use on the <target> tag, run GShell (<WASCE_HOME>/bin/gsh.bat), provided by WAS CE installation, and run the following command
    deploy/list-targets
    to list all the posible destination repositories. Just in case you don't find MasterConfigurationStore, execute on GShell the following command
    deploy/start org.apache.geronimo.configs/farming//car
    copy the whole MasterConfigurationStore value and add it to the command replacing the
    <target> tag, the default value is:
    org.apache.geronimo.configs/farming/2.1.1/car?ServiceModule=org.apache.geronimo.configs/farming/2.1.1/car,j2eeType=ConfigurationStore,name=MasterConfigurationStore
    The fist command used on GShell will ask for username and password, use the username/password combination you usually use for connecting through the web console.

  • <module> is the application (ear, war or jar) you want to deploy on the cluster.

  • <plan> is the xml file with the deployment plan.

Verification of the application deployment on cluster.

Once the application has been deployed, you may check if the process ended successfully verifying the following items:
  • Check the virtual repository MasterConfigurationStore for the application in the machine where the deploy command was executed on, what means, look for the application in the <WASCE_HOME>/master-repository/ directory.
  • Check the actual repository ClusterStore for the application in all of the cluster nodes including the one where the deploy command was executed on, in other words, look for the application in the <WASCE_HOME>/cluster-repository/ directory

Starting/stopping the application

Using the web console you can start the application on each node separately. But if you want to start the application on all nodes of the cluster at the same time, you must open a command console window and execute:
deploy.bat --user <name> --password <word> <command> <module>
Where...

  • you'll find deploy.bat (or deploy.sh for unix like o.s.) at <WASCE_HOME>/bin/deploy.bat

  • <name> is the username and <word> is the password you use to login the web console. Default values are: system/manager.

  • <command> is the required instruction to execute, in this case could be start or stop.

  • <module> is the name wich the application was registered with when deployed on the master repository, for instance, com.javaxcross.tests.WASCEtest/1219953325295/car

Friday, June 20, 2008

JMS Send/Response implementation

In this article I want to talk about how you will create a send/response service using JMS.

There're several reasons to turn an asynchronous thing into a synchronous process, one of them is the following problem: What if you need to call an external service and you will not know how much time that will takes, but you don't want your user seat in front of the page forever? That's one of the problems I faced up some days ago, and because of that I'm writing this.

Not all the JEE implementations provide a timeout for a method transaction, some of them does and you can put a timeout for a method in the vendor specific deployment descriptor, but it's not required by the EJB spec and, if you want a JEE compliant application, you should work with the specification. There's an annotation "@TimeOut" you could use, and it's a standard supported annotation, but this will not help you at all to solve this problem, but explaining the uses of timers is not the focus of this article (if you need clarification on this, just post a comment...)

JMS Background

The sample below uses some JMS basic concepts you need to know about, so If you surf through Internet you will find a lot of tutorials about how to write a client/server JMS stuff, and I will not copy them. In order to fully understand this article I suggest you to take a bird view of Java Sun tutorial for JEE http://java.sun.com/javaee/5/docs/tutorial/doc/.

In the "normal" samples found in Internet you will find a typical solution, a simple java client and a simple java listener, but how everything works if the sender is an EJB Session bean and the JMS receiver is a MessageListener bean?

ok, lets start with the normal sample, a client sends a message to a MessageDrivenBean and the client continues with its life.


@MessageDriven(mappedName = "jms/MyQueue", activationConfig =  {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MyMessageDrivenBeanBean implements MessageListener {

public MyMessageDrivenBeanBean() {
}

public void onMessage(Message message) {
System.out.println("Received");
}
}


Here the server just gets the message and does nothing with it. The Client code is inside a Session bean method:

@Resource(name = "jms/MyQueue")
private Queue myQueue;
@Resource(name = "jms/MyQueueFactory")
private ConnectionFactory myQueueFactory;

public void sendReceiveMessage() {
try {
Connection conn = myQueueFactory.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage textMessage = session.createTextMessage("Test");
MessageProducer producer = session.createProducer(myQueue);
producer.send(textMessage);
producer.close();
session.close();
conn.close();
} catch (JMSException ex) {
Logger.getLogger(ClientBean.class.getName()).log(Level.SEVERE, null, ex);
}
}


The client code does not have anything new; it just creates a connection, a jms session with this connection, a Text Message and sends it to the JMS pool.

What's next? We will send a callback in the client message and it'll try to listen for a response on it.

JMS Callback

        // Creates a callback queue
TemporaryQueue tempQueue = session.createTemporaryQueue();
textMessage.setJMSReplyTo(tempQueue);
producer.send(textMessage);

With this simple code we created a new Queue that will exists only for the current Connection, and will let us receive the response message, and here is the code to receive that message:

        conn.start();
MessageConsumer consumer = session.createConsumer(tempQueue);
Message response = consumer.receive(10000);
if (response != null) {
System.out.println("Response received");
} else {
System.out.println("Response not received due to timeout");
}
consumer.close();

We created a message consumer using the temporal queue and listen for a response on it for 10 seconds (10000 milliseconds), if the message response is null we will know the time out was reached.

JMS Callback sender

In the code above we created a Temporal Queue and assigned it to our message, so we will be able to get it from the server side and use it to send the response.

public void onMessage(Message message) {
try {
System.out.println("Received");

if (message.getJMSReplyTo() != null) {
Connection conn = myQueueFactory.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(message.getJMSReplyTo());
producer.send(session.createTextMessage("Response message"));
producer.close();
session.close();
conn.close();
}
} catch (JMSException ex) {
Logger.getLogger(MyMessageDrivenBeanBean.class.getName()).log(Level.SEVERE, null, ex);
}
}

Note: the connection factory was injected using the @Resource annotation.

Ok, you will get the JMSReplyTo and you'll send a message to that queue, this destination will be the temporally queue you created in the client side.

And that's it.... but... is not!, you must take care of two things to work this out, the first and most important... if your session bean (the sender) is in a transaction the message will not be send until the end of the transaction is reached, so the consumer will wait for the 10 secs and will always write the message "Timeout....", how can we fix this? just put the annotation "@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)" in the caller method. And second... if you don't put the line "conn.start()" just before the consumer receive method call the consumer will not start listening... and you will get a "Timeout" message again. So beware of this two lines, they drove me crazy until I figured out what was going on the first time.

Full code of the application is downloadable from Full project download it's a netbeans project so you will be able to open it with that IDE, or just browse for the code in the subprojects JMSSendResponse-ejb and JMSSendResponse-war (actually it does not nothing but calling the sessionbean).

Wednesday, April 9, 2008

Is Swing slow?

The main problem with this question is the question itself, is a Windows application slow? is the minesweeper slow? are the legacy applications slow?



Everything depends on how you use it, if you have a car, lets say Ferrari, if you use it in the first gear all the time the answer will be "YES THE FERRARI IS THE SLOWEST CAR IN THE WORLD", but is it true? is the Ferrari slow? or is the driver the main problem? you would agree that the driver will have to take some lessons... ok... now let me do the question again: "Is Swing slow?" the answer: NO, Swing is as slow as the architect and developers are, if the swing applications have problems then we will not change the Framework, we need to change the Driver!



The architect and the developer must consider how the platform works under some circumstances and take care of the details, that's true even if you work on a windows application. Swing provides some advantages that you could not have in other languages, but it comes with responsibilities.



But, the theory could say something different from the experience, so lets put hands on and measure some times from the sample posted in the last article Swing Fast sample.



.NET vs Swing Time measure

I created a very simple class to measure the loading time in both applications, the following is the class I used in Java:




public class Timer {
private static long startTime;

public static void registerStartTime() {
startTime = Calendar.getInstance().getTimeInMillis();
}

public static long getElapsedTime() {
long currentTime = Calendar.getInstance().getTimeInMillis();

return currentTime - startTime;
}
}


and here's the .NET version:




public class Timer
{
private static long startTime;

public Timer()
{
}

public static void registerStartTime()
{
startTime = DateTime.Now.Ticks;
}

public static long getElapsedTime()
{
long currentTime = DateTime.Now.Ticks;
return currentTime - startTime;
}
}


ok, I put the statement Timer.registerStartTime(); in the main code just before the invocation of the screen, and the Message.show("Time: " + Timer.getElapsedTime()); just after the screen loaded. These are the results:




















Java ms..NET ms.
187156
125156
125156
125156
125156



(sorry for the extra space, I don't know why the blogger added this :s)

The average time for Java is: 137.5ms and 156ms for .NET, off course... I will not say at this moment that java is faster than .net, this is just the simplest sample, we need to do more tests and more complex scenarios. But the point is... if the starting time is pretty close, why the application will be slower in java than in .net? again, everything depends on how you use the technology.

New Scenario

Ok, what if we increase the number of components and the data on them? the new scenario has 4 new combos, each one with the letters from "a" to "z" as items, so what's now?





















Java ms..NET ms.
156156
157156
156156
156156
156156
157156


So, now the times are very close, right? ok, still too easy right?, I added a JTable in the swing application with 3 Columns and 20 Rows and the time is still the same 156ms!!!.

Conclusion

Most of the problems with the Client applications are not related with the platform or the framework, most of them are because of the abuse of the code, some developers (and also the architects) believe that the code in the client could do whatever they want and do a lot of rookie mistakes (I will talk about that in a new article). The main idea of this article is to prove you that Swing is not slower than other languages, most of the problems with the Swing applications are related with some of the following problems:

- Misunderstanding of the caching systems in the jndi lookups.
- Abuse of the workload in the Constructor of the class
- Abuse of the round trips to the server (when the screen gets the data for combos, tables, etc.)
- Coding problems because the Notepad coding technique.
- Performance problems in the JDK 1.4

and some others I will work on later, with their solutions.

We're close to the main topic of this series, how to build an enterprise applications with Swing as front-end and EJB as backend.

Friday, April 4, 2008

Swing - Fast Sample

In a previous article I promised to show you how to create a simple interface in an easy way using Netbeans and .NET, I will create a simple screen with some simple code, and I will do the same using .NET and Java.

Both applications (Java and .NET) will create a toolbar with two buttons, and two text boxes with their labels. The buttons will show the typical "Hello" message using the input from the text boxes. This is a very easy sample but the main idea is not to show you the architecture and complex of a big application in swing, the idea in here is to show you the point: "Swing is as easy to develop as other IDEs".

Ok, here is the .NET sample:




An here is the Java version of the same thing:


As you can see the steps are very similar, later I will show you a powerful applications using Swing without sweating your shirt. In Addition I will show you how to create enterprise applications with this technology.

Thursday, March 27, 2008

Swing... why not?

Customer: "Can you create a desktop application quick please?"
Developer: "ohhhh I desktop application, sure... I will use MS Forms"
Customer: "But why not java? you told me you feel more comfortable with java"
Developer: "Shh... Swing is a pain in the ass, its hard to develop and slower than other technologies, I suggest..."

I've heard this several times, and this led me to start this post.

The common concerns about Swing are as follows (if you know other ones, just let me know and I will include them):
1. Swing is hard to develop.
2. Swing is slow
3. Swing is not suitable for Enterprise applications
4. The user feels more comfortable if the look and feel is similar to other desktop applications like MS Office, or calc... etc. And Swing looks very different, this might confuse most of the final Users.

Swing is hard to develop.

This can not be more far from true, the main problem with the Java Technology is the lack of tools, everytime Sun releases a new JDK or specification, the users need to suffer this problem, so they must implement everything from scratch using a plain text tool application, just because the IDEs does not have support to this new feature, but... Swing is not new!!!! Swing was released a long time ago, if you use Netbeans or Eclipse you will find that creating a Swing form (class) is just like .NET, even better. (btw, I'm a netbeans lover and they are working very hard to keep the IDE as much up to date as possible.)

Using Netbeans, you can drag and drop controls, use code templates, doble-click to create code for events, etc. It's very easy to create a swing form from scratch. You can create your own controls very easy and use them as normal controls with drag and drop. In a future post I will write an example of how to create an interface swing in 5 minutes... maybe less, and How to do the same using .NET, I think you would not notice the difference between these approaches (ok, well... maybe you will notice it just because the interface in java will be auto-resizable, and some other improvements).

Take a look of this: Swing fast sample

Swing is slow

This is another lack of knowledge, the JDK 5 and 6 have a lot of improvements in this way, and there's a lot of techniques to improve your Swing code in order to have a better user experience. I will do a post with some of this techniques.

Swing is not suitable for Enterprise applications

The Swing applications could also have a good connectivity with the EJBs, and everything will depends on how you solve this interaction from the design and architecture. If you use a SessionBean to execute a work and then uses a new Session bean to retrieve data then you will have some remoting problems, but you can create a Session Bean at server side to work as Model in the MVC pattern, in this way you will be able to use this model to execute the task and retrieve the required data in one or more calls from the server side, so, as I said... everything depends on your ability as architect, create JSPs and HTML application requires a lot of work, so I think Swing will reduce the amount of work to create Enterprise applications. In a 3rd post I will compare how to do the same from a Web Application and a Swing application, then I will compare which one best suits your needs.

One of the arguments used by some people about this point is because Swing is not flexible, so everytime you change a "page" you will need to redeploy the whole jar in the client machine. Ok, here is JavaFX to solve this, so there's no excuses now. :D


Look and Feel

If you say that the look and feel is a problem... think again, the pluggable look and feel of swing will let you change everything in an application, you could even put a look and feel just like Mac even if you work on windows.

Conclusion

Swing is a very powerful framework and you won't believe how easy is. This is a starter post in a series of articles about the uses of swing in a "serious" environments.

Tuesday, March 18, 2008

Analysis of the persistence layer from the SCEA 5 perspective

The technology grew up too fast and there's a lot of different ways to perform data persistence in an Enterprise application, you can do a simple INSERT sql, like:

INSERT INTO Customer VALUES (1, 'Isaac', 'Newton')

or you could use a more sophisticated way to do that, with some ORMs frameworks, but... before to talk about the benefits of one over the other, and which one should you pick in the real life or in the SCEA, lets explain every word.

ORM (stands for Object relationship management): The definition from wikipedia says:

"Object-Relational Mapping (aka ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" which can be used from within the programming language."
but what does this means in the real life of an application?, ok... this is really simple, instead of doing this:

INSERT INTO Customer VALUES (1, 'Isaac', 'Newton')

you will do:

Customer cust = new Customer();
cust.setName('Isaac');
cust.setLastName('Newton');

somepersistenceframework.doPersist(cust);


its very clear in the second sample that the LastName is Newton and the First name is 'Issac', futhermore you don't have to care about the type mapping. What will happen if your customer has a birthday and you want to save it? lets see:

INSERT INTO Customer VALUES (1, 'Isaac', 'Newton', '1642/Dec/25');

but... will be the '1642/Dec/25' a valid SQL format? I don't think so... ok, lets fix it:

INSERT INTO Customer VALUES (1, 'Isaac', 'Newton', '1642/12/25');

Now... will this gone a work? it depends on which database you are working on, if the DB is SQL Server then it will, but if this is an Oracle DB then... nope... why? in Oracle you must specified the Date format as: "TO_DATE('YYYY/MM/DD', '1642/12/25')", then you will need to hard code your formatting options in your code if you want to create a good application.

But, what if you use ORM? then your application should look like:

Customer cust = new Customer();
cust.setName('Isaac');
cust.setLastName('Newton');
cust.setBirthDate(new java.util.Date(1642, 11, 25)); // or use a java.util.Calendar to get a Date

somepersistenceframework.doPersist(cust);


its easier right? ok, this is what ORM means, create objects and let the framework (or container) do the work of synchronize the object with the underlying DB. (this will be done in two ways, to persist the data like INSERTS, UPDATEs or DELETEs and also will be done to retrieve the data).

Note here: btw, I don't forget the use of Named parameters in the javax sql statements (that will fix the date format problem), I just put it as an example, but if you will judge me... ok, lets change the problem, what if the problem is with the identities? in SQL you have it but in Oracle you must implement it as Sequence in a dual table, then you will need code a different method for SQL and Oracle... or what will you say about the BLOB datas? or... I think I made my point in here :D


The evolution of the concept

One of the earliest implementation of this ORM was JDO as a framework and EJB 1.1 as a J2EE standard, in the EJB 1.0 the first approach of this was something called BPM (Bean Persistence Management), which means: "Separate all the SQLs from the Data and the container will let the object know when to synchronize"... so... what's the difference with the DAO Pattern? the main difference is that the server will do all the caching work and will let you know when the data must be saved or retrieved, this will ensure that your application will be scalable, and it will have a good performance (off course, everything depends on the quality of your SQL statements), also it's easily maintained. Then, how this works in our previous sample? here its is:

the Customer class will implement a two new methods named "ejbStore" and "ejbLoad", with these new methods your class should call a DAO class which will implement the persistence.

The only advantage here is the separation of concerns and, off course, the scalability, etc. But the developers quickly hated it, just because it was a lot of work and seems not to powerful. (but it was!)

With this problems in EJB 1.0 the JDO was a good start point for a really ORM framework, the main idea in here is that you will create the objects and an XML which will tell the framework how it will persist this object. JDO could work as a standalone framework, this will able your simple applications to use a persistence framework and forget the behaviour of the persistence layer, this was one of the biggest advantages over EJB.

The big disadvantage in EJB was that the developer should care about the implementation issues I talked about (the problem with formats like Oracle and SQL Server dates), so the developers were angry about that.

Then Sun came out with the CMP (Container Management persistence Entities) which has the advantage that it will take care of the SQL stuff and you will only need to create the entities, the promised land came to earth... but was not the best thing in the world, in fact Sun recommended that if you will need performance then you need to go back to the BMP because the algorithm was not the best and so the performance. That changed in EJB 2.0 and 2.1, were the new Entities had a very good performance (even better than if you write your own code), but they were too complex and the people still had in mind the poor performance in the earlier version.

Recently Sun launched EJB 3.0 based on a new ORM framework (JPA) and a new specification (mainly based over the current persistence frameworks like hibernate and JDO).

Ok, that was the quick flash back, lets move to the main of this discussion, comparison between the different persistence approaches. (maybe I miss some points in the history or omitted some details)

Analysis of the persistence layer from the SCEA 5 perspective

Ok, first of all... let me tell you something... the test will rely on the new JEE specification, so you will not need to be worried about CMP or BMP (why? just because they disappeared and were replaced by JPA). So the question will be, what is the best for my application: JPA, DAOs, JDBC, etc. in front of this topics: ease of development, performance, scalability, extensibility, and security?

The JPA is a framework based on the idea of POJOs (Plain Old JavaObjects) which means: "Leave the Customer class as you have it and the container (or framework) will take care of the persistence", the only thing you need to do its to provide some information about your object to the container, how you do that? if you're using JPA you will do that using Annotations, if you're using Hibernate then you will have the option to put it in a mapping xml file. Lets see how the Customer class will be if you put the JPA annotations:



import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Customer implements java.io.Serializable {
@Id
private int id;

private String name;
private String lastName;
private Date birthDate;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}


}



and how can you retrieve and save a Customer? just doing a minimum work:

put this as a variable in your Session Bean:

@PersistenceContext(unitName="BizAgi.RenderPU")
private EntityManager em;


and the persist the Customer using:

em.persist(c);

so, if you ask me what will be the easier method for development be my guess... the EJB 3.0 its really easy and far more maintainable than the previous EJB 2.1, what about the performance? the performance its not really a problem of one Entity or the way you access your db, I think its a problem of how did you solve your design issues before start the development, but from the SCEA perspective... again EJB 3.0 its faster because it has a lot of improves in performance, cache things and reuse, unless you create your own framework to persist and take care of all the cache issues and object queries EJB 3.0 will be faster every time. Off course, if you compare a simple statement over EJB 3.0 the statement will be faster than the entities, but... that's not the normal behaviour in an Enterprise application, you will have several tables, and several rows on them.

What about scalability? ok, the container ensures that your data will be replicated over all the nodes in the cluster, so you will not need to add anything else to keep the clusters up to date, in this way you could add new nodes to the array and made your application more scalable writing zero code lines, what about JDBC? it is not scalable? mmm scalable relies on how you would add new customers, the jdbc protocol will not be a limitation, the JDBC problem relies on the data cache, with the JDBC aproximation you will need to be in touch with your db to keep synchronization between the nodes, and this will be very expensive. In this way, the JDBC its scalable yes, but will lead you to a performance problem because the lack of cache. And JDO? well JDO and EJB 3 are pretty much the same thing (in the approach concepts) the difference relies again in the cache service, the framework will not replicate the changes and this will generate some problems. DAO will solve any of this? really... it does not, why? just because DAO its an implementation pattern and it will be implemented using JDBC, EJB 3 or JDO as the persistence layer.

In the extensibility context which one is best? all of the approaches are extensibles, but which one will needs more time? I think JDBC, just because the addition of new fields will need a lot of changes, UPDATE, INSERT, FIND and DELETE statements could change, in the EJB 3 you will need to add a new variable to the class and create its accessors (getters and setters) and thats it. Again EJB 3 wins the battle over JDBC, but what about JDO or hibernate? in terms of extensibility JDO and hibernate has a disadvantage, the mapping xml file, you need to do one more step over EJB 3, put the mapping data in that file (beware, hibernate has a new implementation that will get the entity information from the annotations, so hibernate will be as extensible as EJB 3 is).

And Security? ok, let me define security... do you need a user and password to create a connection with the db? for all the approaches the answer will be YES, so where is the difference? as far as I see none.

Conclusion

In the SCEA you will not be answered to choose between EJB 3, EJB 2.1 (CMP or BMP), JDO or Hibernate, it will assume if you are doing the SCEA test you will be approach your answers with his framework JPA, so you will be asked about how to ensure transactions or change things, you will not be asked about which one is the best solution. (From Sun's perspective EJB 3 is the best... off course... I do either :D)

Hope this article solves all the questions about the persistence you could have in order to pass the test, if you have any further questions about it please let me know and I will edit this article to solve some of them.

Wednesday, March 12, 2008

Two, Three, N-tier which one should I pick?

Jeff asked me (in the Post How to pass SCEA 5) about the following:
  • Explain the advantages and disadvantages of two-tier architectures when examined under the following topics: scalability, maintainability, reliability, availability, extensibility, performance, manageability, and security.
  • Explain the advantages and disadvantages of three-tier architectures when examined under the following topics: scalability, maintainability, reliability, availability, extensibility, performance, manageability, and security
  • Explain the advantages and disadvantages of multi-tier architectures when examined under the following topics: scalability, maintainability, reliability, availability, extensibility, performance, manageability, and security.
I will try to put my thoughts in here about this and I will use the following example to illustrate the case of study: Your assignment is to create a check in application for a counter in an airport. so it will show the current flights, allowing you to pick one, then lists the customers for that flight and assign the passenger's seat. (use your imagination here)

Two Tiers:

First a brief description of the situation, a two tier application its mainly a client application which has everything in one layer and connects to a repository (a database if you prefer), this is also known as Client-server architecture. (this is just a brief introduction, you could go client-server architecture to get more detail.

Lets ask first, if all the code is in one place (the client... like a swing application) the client will have all the power force and everything depends on its machine, is it an scalable solution? what will happen if you add new clients? the network overload will be a problem, because these type of application uses it a lot.

In the example the work flow will be as follows:

1. Executes a SQL to get all the current flights (between some date range)
2. shows the flights, and select the one using the ticket information
3. Executes a SQL to get the customers on that flight
4. Shows the list and select the passenger
5. Executes a SQL to verify the status of the ticket.
6. and so on.

The DB connections are very expensive and very slow, so, if you put new customers your application will have serious problems in bandwidth, overload, etc. So your application is not easily scalable. But... if you have only 3 clients (maybe three counters) this will be a very fast solution, the client application will do all the work and will be very effective. The troubles will come if you want to export this to Internet, because the clients will not have access to your internal network, you will need to spread the user and password for the db, etc.

Is this application maintainable? Think of this, if all the code is in the client will be easy to find an error? I don't think so.

What about reliability? the reliability states: "the ability of a system or component to perform its required functions under stated conditions for a specified period of time." so will the client-server be reliability? its hard to figure it out. If the system could be easily break down (because the code is in the client side) someone could change its reliability changing a part of the code. If the network configuration change the client might have problems to establish the connection to the DB, so the reliability will be affected, but as the definition states, everything depends on the conditions not the architecture.

The system will have a high availability? because all the code will run on the client this configuration will not react to a fail over problems, because everything is in one machine if that machine fails the system will not work, so the availability will decrease with this architecture.

Extensibility, mmm... I think no, its hard to maintain and extend a Client-server application, just because everything is in one place.

Performance, ok, here is the big question, the performance in a Client-server application relies on how it will work, if it has several calculations everything depends on the client, there's no way to distribute the work between several machines, all the work will be running in one PC, but... the client-server applications runs fast if you do some tricks. In the example above the performance could be an issue in here, just because it has a lot of DB queries dependencies.

Manageability, think if you are able to change some parameters and your application will run fast or will have a better reliability, the answer will be no, just because everything runs in the client and you will have absolutely no control over it.

In general I would say that increasing the tiers will ensure extensibility, maintainability, security but will decrease performance.

Remember, tiers refers to the layers in which your system could split off, if your application have a lot of classes but works in a single machine which connects to a DB then it's a client-server application.

Three Tiers

In the three tiers your application will have some logic in the client (usually the presentation and some validations), the business tier will have the 80% of the application logic (lets say this will have most of the business code) and the DB which could have some Stored Procedures in it.

What will be different from the previous approach? you will have a lot of things running in one machine, so its easier to maintain, you will increase security because the client will not have the user/password pair to access to the DB, and will be more extensible than the client-server application. But what will you gain or lose? the answer is not easy, I would say It's relative, If the application is a calculation centric then you might lose performance (because the server will do a lot of work for several clients at the same time) or you could gain performance if the application is mainly a db application, just because you could place the DB server near (referring to the network topology not the physical distance) to the business server in order to avoid the network overload.


In comparison with the Client-server you will have:

- better scalability, you could increase the hardware in the business machine and could have more clients.
- better maintainability, everything is in one place so you can replace a component and fix a bug quickly
- better reliability, because you have a better control of the environment in which the business code runs.
- better availability, if the business machine has problems you can easily replace it by a new machine a your client will be running in a blink of an eye
- better extensibility, you could add new functionality in a better way
- decrease or increase in performance, this depends on the application, if its a db application its 90% to get a better performance.
- Better manageability, its easy to control the environment in the business tier, so you can change configuration settings, improve connections etc
- Better security (I think this does not need further explanation...)

N-Tier

I think N-tier and 3-tier are pretty much the same thing, if you have a good security in 3-tier you will have even better security with N-tier and this applies to all the statements. (Even to the performance thing, a bad decision could lead to a poor performance if you choose N-Tier over the others and you don't pay attention on the specific requirements)

Why will you prefer N-Tier over 3-tier, you could distribute the work in several machines, so if you are working in a collaborative application (I loan application which will need a CRM, ERP etc then will be distribute its responsabilities over several machines and will have a better performance because of it)

In JEE you could easily use some patterns like Service Locator to change a N-Tier to a 3-Tier application (or, at least, behave as a 3-tier application), just changing the uses of the EJBs from Remote to Local. So if I need a tiered application I will choose N-tier over 3-tier every day of the week (and twice in Sunday)

(Please let me know if you need further explanation and I will add more information on N-Tiers)

Wednesday, March 5, 2008

How to pass SCEA 5

I'm MCP, SCJP, SCBCD and now SCEA 5, when I took the SCEA 5 Beta, I was wondering if this will be as difficult or easy as the other ones, and it was very interesting just because Beta means "you will not have any help on, nobody took this test before and you will have only your knowledge with you".

Because of this I started this blog, to share my experience in this test and let you know the key elements you must be aware to pass this out.

Certification description

The certification is compound by 3 parts.

1. The first part is a multi choice exam just like the others, it will test your knowledge about JEE and Design patterns
2. The assignation is the second part, you will get an assignation which will be similar to a real life assignation, this means that you will face it as a real application should be faced.
3. The third part is the easy one if you did the assignation, they will ask you about some common decisions made during design time, like Authentication, authorization issues, security, scalability, maintainability, and so on. This part will be evaluated as part of the assignation, so you will not have to wait to pass the second part in order to start this one.

General Advice
If you think you are ready to pass this test, just ask yourself:

- Did I worked enough with Case Use , Sequence Diagrams, Class Diagrams, Deployment Diagrams, etc? if the answer is no, go and take some practice on this, I'm pretty sure you will not pass it if you don't have some experience about these topics. (you don't need to be a guru)
- Did you architect some solutions? were they successful solutions? scalable, maintainable, etc?

Part 1
The first part is easy, you must know the following topics (these are from the java certification page):

Scalability, maintainability, reliability, availability, extensibility, performance, manageability, and security. These are very important and will be asked through all the test, you will have problems like: "How can you add new customers and keep the availability?" or "If you add a new layer you will lose in security, reliability, scalability or manageability?" (those are not exactly the answers, I'm not able to put the real ones in here)

Security. You will be asked for firewalls, protocols, etc. Its easy if you read about it.

Tiers. You will be asked about the advantages and disadvantages about two tier, three tier, n-tier, client-server. Remember, not always the n-tier is the best option, imagine you have a simple hello world application, will you create this with ejbs, db etc? be sure to take a look of this new post Two, Three, N-tier which one should I pick?

Messaging. You need to know how the JMS works, you will be asked for this, that's sure.

EJB. this test will be easy if you know EJBs and you already worked with them, GOOD NEWS: YOU WILL NOT SEE CODE IN HERE, yes... you will be asked about the life cycle, advantages, when to use Stateless, Stateful, CMT, BMT, etc, questions like: "will you use persistence to save the data in a Stateless or Stateful bean?", so be sure you create almost any JEE component in a sample and be sure how it works.

Web Tier. Here is the tricky part, you will be asked for JSF, JSP, Servlets, and will be little bit difficult if you didn't develop a web page, so... go and open your netbeans (or Eclipse) and start working in these concepts. I'm not sure why they ask the details of this, but they will. You will not find answers like: "Find the error in here ...", but you will be asked how can you use those technologies to solve some problems.

Patterns. Ok, here is the core of the test, you need to know the patterns described in the Blueprints guide, also the ones in the GoF, etc. The test does not ask too many questions about patterns, but its sure you will find some of them.

In addition, you can find some questions referring to CORBA, RMI, etc, so be sure to understand every term. If you have any further questions about the questions please let me know in the comments, I will try to answer it keeping care of the test disclosure.

Second Part

Once you get the result of the first part you will be ready for the practice test, so how it works?

You will get an assignment which you will download from a page Sun gives you, this assignment is private and its very difficult that somebody have the same (I think they must have a hundred of different assignments, but I'm guessing).

The assignment is compound by a description and a sort of Case Use you must solve, as the real life assignments it has description, domain model, requirements, and Case Use and its possible they have some errors, your job as architect is to detect those and write it down in your assumptions, for example if you get an assignment to do a check in a system for a hotel and you see that the case of use says something like: "The customer could ask for the check" you could assume the check will be in dollars, in this way you will not have to do a currency conversion. Or you could find something like: "The cat eats the dog at breakfast", and you will say "What a hell?, I think this is wrong my assumption is somebody did a mistake in here and it must be 'the dog eats the cat...' "

You must read very carefully the assignment, go to the bathroom, take a shower and go back on it, read it again and try to be sure you understood all the requirements of the system, if you fail to detect a requirement you will fail the test. Use your imagination to figure out how the steps in the Case Use works and how will your system is going to address these.

You will need to provide Class Diagram, Component Diagram, Deployment Diagram and your assumption list, also you will need to provide the sequence or collaboration diagrams (this will not influence your score, but will be a good way to get the other diagrams). Also you will need to provide a list of the top three risks you detected and how you managed them.

While you are trying to figure out how to solve your problem keep in mind the following: Scalability, maintainability, reliability, availability, extensibility, performance, manageability, and security. These are the key points in the test, if your system architecture could lead to a performance problems then you will need to address that in your design.

Remember, Sun is asking you for the architectural design, not the detailed design, so keep in mind that to avoid any unnecessary detail. How do you know if your design is too detailed, I think you should be able to explain your design to your team in a 30 minutes meeting, if you went in too much detail the designers will not have any work to do.

The third part

Ok, this is the easy one, you will be asked for some of the decisions made in the previous step, how did you address the security, the scalability, etc. What is the tricky part in here?, take a look of all the questions before begin, when I took it I tried to answer the best I can, so I put all that comes to my mind, then when I moved to the next question... puff... they are asking for something I already answered in the previous question (and you don't have the copy/cut/paste option), so I needed to move back remove the unnecessary answer in the previous one and rewrite it in the next. The time flew and I only had 5 minutes to do a full review. So, go and read all the questions before begin.

If you have any further questions please let me know in the comments section, I will try to do another blog with those questions and answers.