Friday, August 17, 2007

Server Push with Flex & J2EE

This is one of the features which have been done for one of Xpflow releases and quite exciting in nature which has prompted me to write this article. There are cases where the data being displayed in textual or graphical format changes over-time and we would like the users to be aware about it even without having to refresh the screen.

Typical applications: Email – when we get a new email - we want our selves to be notified about it automatically. Also if we want to develop a monitoring application where the status of network, hardware etc which needs to be monitored at the real time, Stock market observers, viewing cricket scores and many more.

What is wrong in refreshing the screen?
User does not know where there is a data and user will have to regularly press the refresh button – this is what we call busy wait – which means we are taking time for the user but still being at wait stage – i.e. unnecessary usage of a persons time. Also if the UI is image rich or the page refresh is time consuming then that aspect gets magnified.

Pull/poll Vs Push.
Now there are options to either poll the backend services at a regular interval or let the server push the content whenever available.

Whats wrong in a poll ?
It’s a busy wait - consumes more CPU cycle from client machine. (http://en.wikipedia.org/wiki/Busy_waiting)

Alternate to Flex:

Without using Flex what are the options we have to develop a page which refreshes automatically with revised contents?
Well .. there is nothing like Push in HTML but i.e. we can't "push" anything to the browser.
Option 1: We can write applets or other client-resident programs that refresh, poll, monitor, listen to a server.
Option 2: Using HTTP META tag for with a dynamic page: like JSP or ASP and have that page re-render the results.

This way the browser generates a request every 10 sec say. Thus we can write a small jsp which shows the Cricket score and have the meta tag defined – by virtue of which the page refreshes regularly and shows the updated score.

When the whole page should not be refreshed: We can also use frames in HTML page and have the HTML frame point to a dynamic server-side component like a JSP page which in turn can be opened in a new page with updated content.

How internally data services work? Does it take a lot of network bandwidth?

Flex Data Services offers a number of data transport channels for communicating between a Flex client and the Flex Data Services server. These channels include RTMP(s), AMF(s) and HTTP(s). Out of which data push is done over RTMP.

http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol

So this means its not a pull mechanism from the browser.

Architecture:






Above diagram is the conceptual representation of Server Push Architecture. This means the on client side a set of libraries reside and talks to Flex Servlet which internally performs the connection between the server and the client open and performs the data push from server to client without having to perform any client side refresh.

Code:

Here is a scenario where user has a list of tasks which he needs to work on and on run-time he needs to ge notified about any new task arrivals.

Message publisher in this can tells which all user ids have received a new item in the list of pending tasks to the Flex side action script components.

Message Publisher:
public static void refreshDashBoard(final String mailids){

final MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
final String clientID = UUIDUtils.createUUID(false);
final AsyncMessage msg = new AsyncMessage();
msg.setDestination("feed");
msg.setClientId(clientID);
msg.setMessageId(UUIDUtils.createUUID(false));
msg.setTimestamp(System.currentTimeMillis());
msg.setBody(mailids);
msgBroker.routeMessageToService(msg, null);
}


Messaging-config.xml: This stores configuration of the Message Queue.

<destination id="feed">
<properties>
<network>
<session-timeout>0</session-timeout>
</network>
<server>
<max-cache-size>1000</max-cache-size>
<message-time-to-live>0</message-time-to-live>
<durable>false</durable>
</server>
</properties>
<channels>
<channel ref="my-rtmp"/>
</channels>
</destination>


In Flex side: A message consumer has been kept which once receives this message takes an action.

When a new message arrived a mssageHandler method gets called which makes a synchronous call to fetch the data.


<mx:Consumer id="consumer" destination="feed" message="messageHandler(event.message)"/>

private function messageHandler(message:IMessage):void{
if(chekForEmailIds(message.body+"",model.globalvalues.loggedUserEmail)){
var dbdel:DashBoardDelegate = dbdel = new DashBoardDelegate();
dbdel.getDashBoardData();
}
}



The component of Flex which actually performs the messaging: (from web.xml)


<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<init-param>
<param-name>flex.write.path</param-name>
<param-value>/WEB-INF/flex</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>





Learning smart tools: Unix Shell

As a Java/J2EE person most of the time working in development probably deals with using IDE’s and less on command lines. But I will express my love for command line tools here since it makes things faster and useful.

IDE’s are good but has its share of limitations:

a) Processing log files when running application server: Log files generate different statements and may be you would like to know all instances when a particular event happened. Following Unix command will bring all lines which contains Exception instead of having to go thru the file looking for it.

> cat server.loggrep Exception

b) Want to automate the build: If we have used command line options for builds using tools like Ant - its just one step away in QA environment. No separate effort for QA

c) Multiple steps thru IDE are error prone: Ex: suppose before we start testing the application we would like to copy some files to the server. Every-time we do it manually or using features like linking folders is not preferred since human steps are error prone and the development directory gets fixed to a given location if we use linking.

d) Suppose before every check-in you want to test your code automatically: From Ant build scripts we can keep a target which will refresh the database, perform unit tests for all the service classes.

e) Single enter is easier than multiple clicks: when you have to get a job done in a UI using multiple clicks and if the same has been put in s script – all you have to do is press enter. Easy – isn’t it !!

As a command line interface Unix shells brings a lot of tools to perform jobs quickly:

a) Need to find which process is running in a given port.

>Fport grep 8080

Fport is a windows tool which tells you which process is running at different ports.

b) Need to copy a file to a Unix server running SSH server. No ftp server is running for security reasons.

Ø scp

(Win-scp is one utility which allows one to do so)

c) Schedule a job: crontab – very nice tools in Unix and for Windows there is ‘at’ command to do this job.
d) Few more tools which perform better file management and transfer and without having to download and configure additional tools are: Tar, zip etc.

The key features which makes it powerful over dos shells is its capability of using the output of one command as the input to other and which allows someone to perform an operation by using pipes and achieving a complex operation is a small script.


Editors in Unix:
Well probably I wont refer to Unix editors for editing the java files although its much quicker to work with it. People when gets used to it don’t wanna quit but for initial users its less intuitive than a Visual IDE. Plus IDE has support for debugging etc. The main thing is one does not have to take his hand out of the keyboard while working with IDE mean working with mouse and the keyboard and going back and forth between it. So simple editing I will still use VI but for java programming Intellij IDEA/Eclipse.

UnixShell in Windows:

For unix lovers there are several options to simulate unix shells in windows. Options are
a) Cygwin
b) MKS toolkit
c) Interix


Shell scripting:
Moment someone starts working on application maintenance the need to log file processing to understand and track-down a defect or outage becomes even more prominent. Shell scripts to add logic and the single line commands to programmatic option add a lot of value. The key file processing and parsing utilities are awk and sed.
There are scripting languages like Perl also helps to perform some of these operations really well.

To conclude:

What is engineering?

Engineering means that the truth is present – i.e. we know how to solve a problem and there are possibly many ways to do it. Engineering will means using the right tool for the right purpose and every engineer needs to know more than one tool to perform the job. The good engineer knows more tools – i.e. for a java engineer knowing just java is not good enough since its not going to solve all the problems. A single line of Unix command can take 500 lines of Java code. Knowing the tools and applying the right tool when you need it makes all the difference.

What is science ?

Science is about finding truth. Most of the times all we do is engineering thus should be in constant lookout for tools which makes our life easier.

References:

http://en.wikipedia.org/wiki/Sed
http://en.wikipedia.org/wiki/AWK_(programming_language)
http://en.wikipedia.org/wiki/Crontab

Wednesday, August 15, 2007

How to become Technical Architect?

This is a broad topic. Surely. Anyway.. A short answer will be wisdom i.e. taking more interest on how solutions are built and meets the requirements which are beyond functional requirements and trying to meet the non-functional requirements like scalability, readability, maintainability etc.

Learning technologies: Its never sufficient. We will need to know different ways to solve the same problem. Can be Java, MS technologies etc.
Solution is important technology is not: From a business person there may be many ways to solve a given problem. As long as the solution gets done people are happy and thats why people need to take a look at different technologies constantly.
Learn building blocks: There will be many tools which are commercial and open source and depending on the cusotmers need: Time to Market or cost or both - technical architects can suggest tools which are useful. Build is NOT always the best solution.
Learn smart tools: Same work can be done by differnt people differently the main part is knowledge and usage of tools. This can be web spiders which help crawl the pages better.
Learn development methodologies: Different development methodologies like RUP, Agile etc are also important.

Future posts will take up different architectural problems and solutions using different techology set.

Code refactoring

Continuuous improvement of the quality is a process which is one of the ways Agile developments really depends on. However within Agile or outside agile improving code quality is a common problem to address. Now what are the common reasons of having a code to be refatored:
a) Partial business knowledge
b) Lack of understanding of the intended architecture on the programmers part
c) Deadline pressure: Programmers end up coding something which just meets the deadline needs.

Common mechanisms of solving this problem:
a) Manual Code reviews: These are of two type - peer led reviews- where one programmer does the review of other progarmmers. Also Architect driven reviews where the technical architect performs the code review of solution being developed.
b) Automatted Code reviews: Tools like PMD performs automated code reviews.
c) Pair programming: This is a principle used in Agile and XP style development where two people looks at the computer at the same time. This does not exactly helps refactor but while development it can achieve higher quality.

IDEs like Eclipse and Idea has support for wide varieties of code cleanup and refactoring. Jboss IDE 2.0 has the feature of code cleanup which is pretty neat. Much of these refatorings are borrowed from Intellj though Eclipse has gor quite some attention.