Tuesday, July 24, 2012

GWT RPC - Server communication with MongoDB

Further to the client tutorial in the previous blog, here i am going to present how client communicates with the server with the help of GWT RPC (Remote Procedure Call).
Using RPC which works asynchronously only specific part of the client components can be updated without updating the complete client.














Pre-Requisite:
Java, Eclipse with GWT plugin, MongoDB.



In this example i have extended the previous client to
  a) save data i.e. user data to database and
  b) authentication user

Client can send/receive serializable objects to/from the server. In this example server is going to use that data and insert in into DB, which is MongoDB in our case.


Create Modal
As the client can send serializable java objects over HTTP. Lets create a modal first.


User.java



Create Service
Now we need to have a service which a client can use.
In our case we name it as MongoDBService and it implements RemoteService


MongoDBService.java



@RemoteServiceRelativePath("dbservice") is annotation used for the service identification and calling, which should match with the service path defined in web.xml for servlet-mapping tag.

We need to create Asynchronous service for our MongoDBService, client is actually going to call RPC through MongoDBServiceAsync. All methods of MongoDBService will have extra parameter which is of type AsyncCallback. Client call will be notified when any asynchronous service call completes.

MongoDBServiceAsync.java



Implement service at the server side
Create class MongoDBServiceImpl which extends RemoteServiceServlet and implements MongoDBService. Basically MongoDBServiceImpl is a servlet which is extending from RemoteServiceServlet rather than the HttpServlet directly.



Updating web.xml with servlet details
make sure that all the service which are created with the annotation RemoteServiceRelativePath are added properly for each servlet.

web.xml



url pattern tag path should be formed using module/service.
In this case module name is gwtwebappdemo ( see in GwtWebAppDemo.gwt.xml for rename-to value) and service in dbservice.


Client Service Call
calling service from client





Running application

1) Start MongoDB
    In this case i have started DB without authentication and on default port.
    Default port is 27017 which we have used to connect to database in the code. See DBUtil.java in the
   example.



2) Add user into database.
    Client calls asynchronous call to server, server starts the processing. Once request processing is finished it calls back with the call back handler provided in the request. Client gets call in onFailure in case of failure and onSuccess in case of request processing successful.


    In the server side, server received the User object and sets the data into User collection which is in the mymongodb database using BasicDBObject.

MongoDBServiceImpl.java


3) Check the collection ( table) creation in dbs (database)
    In this example mymongodb is the database and User is the collection which we are using.
    Initially both database and collection is not present, when the first save happens. Both gets automatically
    created.



4) Check User Authentication with wrong input.



Download complete example from here in GwtWebAppDemo_server.rar file.

Tuesday, July 17, 2012

Google Web Toolkit - step by step GWT client tutorial

Its free!
Its opensource!
Its makes development faster!
Its amazing!
Faster development of web application with an ease.
AJAX based app develop in java and run in all browser.
Works well with Android/iPhone.

Google has done preaching on their site and blog about GWT, find more details their.
Ofcourse believers of those preaching also wrote a lot about it on their blog(s), website(s), papers, etc,
So, moving to example.:

Pre-requisetes
   Eclipse, Java.


Download and install GWT plugin for eclipse, in my case its for indigo and link is as follows:
http://dl.google.com/eclipse/plugin/3.7

for complete instruction use the following link
https://developers.google.com/eclipse/docs/install-eclipse-3.7


Create a Google Web Project -> GwtWebAppDemo

     Click on next.

    Un-check the checkbox "Use Google App Engine" as we are going to use internal app server.
    Click on finish.
    A new google web project is created with default project files.
 
    Delete all default java files from client, server and shared package.
    Add a new Entry-Point class into the client package.



One entry will be added into GwtWebAppDemo.gwt.xml.
Verify that entry point is correct in GwtWebAppDemo.gwt.xml. In this case its





Change the class in entry-point tag if you have changed/added the class manually and not as Entry Point Class.


Add RootPanel to the entry point class i.e. GwtDemoAppClient.java in onModuleLoad() method.



RootPanel rootPanel = RootPanel.get();

RootPanle is a panel to which all other widgets must ultimately be added.
RootPanels are never created directly. Rather, they are accessed via get().
Most applications will add widgets to the default root panel in their EntryPoint.onModuleLoad() methods.



After that right client on GwtDemoAppClient.java and open with GWT Designer
We are ready for adding the components by drag and drop.


I have selected LayoutPanel as it allows its children to be positioned using arbitrary constraints.



After adding the components you can see the generated code:





Add few componets to the layoutPanel and test the demo without web app to see it as preview.




If its fine than go ahead and create a web app/deploy to internal app server which is jetty.
Else go and add/remove/update components in the panel.

Change the default html content of GwtWebAppDemo.html in the war/WEB-INF folder.



Right click on the project and click on Run As Web Application
It will start new tab as development mode, copy the link and paste into your browser







For the first time it will ask for installing the plugin for gwt. 
Install it and see the application running.


Do remember to close the client running the the IDE before launching it again.



You can download sample from here  --> GwtWebAppDemo.rar 

Wednesday, March 28, 2012

Power of Script writing in the world of JavaScript.

When i was intern, i got a change to choose the technology on which i could develop a tool. PHP or Java. I choose Java, why.... hmmm... its easy to learn, use, understand, scalable (a myth), etc, etc, etc.
Later understood facebook uses php a lot.

Browsed few perl scripts few years back and did not even like it.
Later understood perl developed is easiest in many ways and faster. Faster in execution too.

JavaScript........ahhhh, what is this, i did not like it. Some kind of client side work, which looks like always messy. :-(
Later understood that its not only the client side script any more.

So suddenly my thinking about the script writing, understanding, acceptance has changed. Script programming is amazing.

First got to know about MONGODB. Oh its amazing, it understands java script. We can do all crud
operations with java script syntax. Its a document oriented storage with "NOSQL".

Than checked which mapper can be used and found node.js -> mongoose.


 And tried some hands on with node.js + mongodb + express + mongoose + jade (a template engine). Jade for view, remaining things for model and controller.

 And commonality in all these things are java script is everywhere. Commonality, an important factor for a software product with quality. Of-course there are many point and which i do not want to touch now.
So the point is: 
Server side java script. 
Client side java script. 
Database operations in java script. 
Oh, its heaven --> IT IS THE WORLD OF JAVA SCRIPT.

I am going to take you through some steps which can be useful to start with server side java script. You can download the project from my git repository at


 https://github.com/deepaksinghvi/nodejssample

1) Download Install node from http://nodejs.org/. This installs node and npm ( node package manager).
    npm is used to install all the dependent modules, like mongoose, express, jade, stylus, etc.
2) Next install all the dependent modules:
    cmd prompt> npm install mongoose express jade stylus
      or you can do one by one also.
3) Now lets write few java script files
     a) load all the required libraries which we have just installed and create an application server.


     b) create model and controller scripts.

         Creating a model (models/model.js).
The controller part normally known in the node.js development worlds as routing, i have kept in the main script file only ( which is app.js).
Routing of creation of a new user. This creates a new user and saved it into the database.

User clicks on "Create User" button and it request gets routed to above mentioned script.

Once user gets added than listing of users shown from the table ( known as collection in the world of mongodb) or it can be shown if user clicks on "Show Users" link in the above image.


4) Enjoy the sample application.
    To start the application:
    a) Start database
         mongodb/bin>mongod --dbpath /data/db 
       
    b) Start sample application
         nodejssample> node app.js


I tried to provide a very simple sample to start with node.js and mongodb. Its a perfect match for a rapid web based development. 
WebSocket also looks very promising, a lot has been written about it. I would like to try some of the exercise on that and will be sharing that as well in the next blog (hopefully).



Saturday, December 3, 2011

QuickFix/J based Fix Protocol enabled trading client


Purpose of this post is to make available a simple sample application for the people who either wants to start learning or want to develop of FIX (FIX protocol) based trading client.

FIX Acceptor ( which acts as the Stock Exchange)
FIX Initiator ( which acts as the Trading Client)

Its just a simple client without any GUI.
People who are already aware about the FIX and just want to try the sample app in java can jump directly to the example, rest of all people can take a pain to read the following:

Some jargons about the FIX protocol:
The Financial Information eXchange ("FIX") Protocol is a series of messaging specifications for the electronic communication of trade-related messages. It has been developed through the collaboration of banks, broker-dealers, exchanges, industry utilities and associations, institutional investors, and information technology providers from around the world. These market participants share a vision of a common, global language for the automated trading of financial instruments ( from Fix Protocol Organization).










Some GYAAN:
If every exchange in the world communicates "ONLY" with their own developed messages (Proprietary Protocol ) than for every exchange there will be need to develop seperately the trading client infrastructure or trading client platform. This is a painful work as to maintain a client with the proprietary messaging or Proprietary Protocol (Message) for every exchange. And keeping the client in the accordance with the exchange notification(s) for upgrades of messages to communicate is a big problem.
And in that case doing a trade with client which supports multiple exchange at the same time seems far from the reality. Of course a client can manage support for few exchange but a large number of exchange will not be possible ( which is the need in the today's world).

With FIX based application its easy to develop OMS ( Order Management System) and EMS ( Execution Management System) faster and scalable. It can be used for Algo Trading but its gonig to be bit slower, so for Algorithmic Trading Proprietary Protocol is a good choice.


A typical FIX message looks like as:
br />
<20111204-11:02:59, FIX.4.2:FixClient8019->FixAcceptor, outgoing>(8=FIX.4.2|9=80|35=A|34=14|49=FixClient8019|52=20111204-11:02:59.353|56=FixAcceptor|98=0|108=60|10=219|)
where every tag has a specific meaning, in the above tags are 8,9,35,34,49,52 etc.

Lets jump to the FIX based Acceptor and Iniator.

FixAcceptor: Which listens on a specific socket and works as a server. It is the Stock Exchange. Number of clients can connect to it. All the properties for the Acceptor is defined in the acceptor.cfg file.
FixInitiator: Which connects to the FixAcceptor on a specific port defined by Acceptor. It is a trading client. Client can connect with different fix implemenation versions which a client and exchange supports. I am using FIX.4.2 version for the example.

Explanation about the .cfg and fix message is beyond the scope of this blog. ha ha ha, i saved some time. Dirty trick to escape but i have no choice as this is a vast subject and neither i have time nor the experities to cover everything.

Create FixAcceptor.java and FixIniator.java either by implementing Application interface or by extending ApplicationAdapter class.

First some snippet from Exchange i.e. FixAcceptor.java

public class FixAcceptor extends MessageCracker implements Application





Start the acceptor based on the configuration defined in acceptor.cfg

SessionSettings settings = new SessionSettings("./conf/acceptor.cfg");

FixAcceptor acceptor = new FixAcceptor();
ScreenLogFactory screenLogFactory = new ScreenLogFactory(settings);
DefaultMessageFactory messageFactory = new DefaultMessageFactory();
FileStoreFactory fileStoreFactory = new FileStoreFactory(settings);
SocketAcceptor socketAcceptor = new SocketAcceptor(acceptor,
fileStoreFactory, settings, screenLogFactory,messageFactory);


socketAcceptor.start();

onMessage(...) : there are many overloaded onMessage() methods are defined in the api, for the purpose of example i have overridden

@Override
public void onMessage(NewOrderSingle order, SessionID sessionID)
 throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
...
}
exchange checks for the match for the requested order by the client and in case of match it process the order and updates the client with the execution report. Have a look in the example to see the implementation.



Now some snippet from Trading Client i.e. FixIniator.java
Here i am extending ApplicationAdapter instead of implementing Application as its done in FixAcceptor.java




public class FixInitator extends ApplicationAdapter
Start the initiatorr based on the configuration defined in initiator.cfg
FixInitator fixIniator = new FixInitator();
SessionSettings sessionSettings = new SessionSettings("./conf/initiator.cfg");

ApplicationAdapter application = new FixInitator();
FileStoreFactory fileStoreFactory=new FileStoreFactory(sessionSettings);
ScreenLogFactory screenLogFactory=new ScreenLogFactory(sessionSettings);
DefaultMessageFactory defaultMessageFactory=new DefaultMessageFactory();
fixIniator.socketInitiator = new SocketInitiator(application,
fileStoreFactory, sessionSettings, screenLogFactory,defaultMessageFactory);

fixIniator.socketInitiator.start();

// prepare order NewOrderSingle order = new NewOrderSingle( ...)
try {
// send order to target which is nothing but the exchange/acceptor
    Session.sendToTarget(order, sessionID);
    } catch (SessionNotFound e) {
    e.printStackTrace();
}

I have overridded method fromAdmin(...) where client will receive from the admin where one of the message is the ExecutionReport which is sent by exchange/acceptor.
@Override
public void fromApp(Message message, SessionID sessionId)

COMPLETE EXAMPLE CAN BE DOWNLOADED FROM HERE

Saturday, November 5, 2011

Android App / Mobile Trading Client

My first android app for trading client.
Very happy today, finally i did it, it was long pending task.
I have intention to use QuickFix/J a Fix Protocol based open source library.


For the android app demo purpose (click here to download sample code) i have removed the JMS web service call which can send the order to Fix Protocol based client and just shown the order



                  JMS Web Service                                         Fix Message on TCP/IP To Exchange                            
Mobile Client ===>    JMS Queue  ===> Fix based Client (Broker)   ===>  Fix based Server (Exchange)
                                        QuickFix/J based client Parser
                                     
                                      FROM CLIENT TO BROKER-EXCHANGE DATA FLOW



               Order Status Update (Broadcast-Uni-cast/ TCP-IP Message)
Fix based Server (Exchange)    ===>     Fix based Client (Broker)    ===>   Mobile Client
                                                                                           Order Status Update (may be SMS)


                                      FROM EXCHANGE-BROKER TO CLIENT DATA FLOW


User enters the required Symbol(Script) and other details for Buy/Bid and click on Order button. Android app sends the data to JMS queue which is running on a remote machine at Broker's office and which is based on JMS WebService .

Broker has a Fix protocol based parser running which collects the order from the queue and prepare the Fix based message to Fix protocol based Server, which is located at Stock Exchange.

Once order is confirmed from Exchange the same is sent to Broker and which updates the Client.

Android made the work easy for development, i am not able to find anything much to explain anything in detail. May be have look of the code.

Note: Target audience for this blog and code is only developer and not the android phone user.

Wednesday, September 14, 2011

Remote Debugging In Eclipse

Remote debugging capability with different debuggers( IDEs) are nice features for developers. Purpose of this blog is to share this information that how to use remote debugging tools effectively. Here i tried to give example for eclipse IDE.

Remote debugging in eclipse can be done in two ways:
a) Socket Attach
b) Socket Listen


Socket Attach: For e.g. we want to debug an application which is deployed on tomcat and we want to debug that. Attach the application process through eclipse once we start our tomcat in debug mode.

Step 1:
How to start tomcat in debug mode:
$./catalina.sh jpda start (in case of unix based machines) or
> catalina.bat jpda start ( in case of windows based machines)

This is going to start a tomcat in debug mode and a socket with port as 8000 can be attached to it.
In case if user wants to attach to different port than following can be done.
export JPDA_ADDRESS=8000  (in case of unix based machines)
$./catalina.sh jpda start


set JPDA_ADDRESS=8000 (in case of windows based machines)
> catalina.bat jpda start

Step 2:
Open debug configuration on eclipse and add new Remote Java Application.
Connection Type should be Standard (Socket Attach). Give a host name or ip in the value for Host in connection properties where the remote process is running.
For e.g.

















For Other application server like WebSphere:
Open admin console and open Servers->Server Type -> WebSphere Application Servers.
Than choose your server from the list of available servers. And than select Debugging service.
for e.g:
Application servers > server1 > Debugging service
Make check box "Enable service at server startup" as selected.
default port for debug is 7777, change it if you want to debug on different port.
Restart the server and attach your debugger ( IDE or eclipse) to it.


Socket Listen: When we want to start a listener first and the remote application is required to be attached to the eclipse
debugger, in that case. First we need to start the eclipse debugger in Socket Listten mode and attach the application.

Step 1:
Open debug configuration in eclipse and add new Remote Java Application.
Connection Type should be Standard (Socket Listen).
Port in connection properties should be 8000 if you want remote application should connect using this port value.
For e.g.
















Step 2:
Options a) if wants to debug with ant than in your build.xml add
<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,address=10.240.1.14:8000,server=n" /> for e.g.
<target name="execute" description="Test">
  <java classname="${main.class}" fork="true" failonerror="true">
   <classpath refid="class.path" />
   <arg value="${myargument}" />
   <jvmarg value="${myjvmargument}" />
   <jvmarg value="-Xrs" />
   <jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,address=10.240.1.14:8000,server=n" />
  </java>
</target>

     
Option b) If you wants to debug with .bat file or on command prompt

Java -Xdebug -Xrunjdwp:transport=dt_socket,address=10.240.1.14:8000 -jar MyJar.jar


Options c) if you wants to debug a application which is again an eclipse ( or eclipse based plugins) than you can start the eclipse application as


eclipse.exe -vmargs -Dosgi.requiredJavaVersion=1.6 -Xms256m -Xmx512m -Xdebug -Xrunjdwp:transport=dt_socket,address=10.240.1.14:8000,suspend=n

10.240.1.14 IP where you want to debug and where your debugger is running.
Debugging in JBoss can be debugged by modifying JAVA_OPTS in the file which is used to start the server in the similar way as mentioned above.
IBM WebSphere is already explained above.

Friday, June 24, 2011

Dynamic Proxy for gui component listener using InvocationHandler.

Listeners for gui is always easy to create and difficult to maintain.
As they will create additional classes for that and in case we need to
give a patch or something it can be a real pain.

Lets say that a Frame (MyFrame.java) has 10 components and each of them has a listener than there will be 10 additional classes ( MyFrame$1.class,... , MyFrame$10.class, etc).
Now if a fix is given for one of the component and we really do not know which one to give as a hot fix, we will be ending up in giving all the 11 classes.
Another thing which might also force to give all the classes if we change the serialversionId.

So how to overcome this, i have tested it and found it works well.
But is it the right thing to do not sure but good to know.

We can use the InvocationHandler to handle this situation which can call the required method of the frame where we can have the logic/code to be run using Dynamic Proxies to call whenever event is raised.


import java.lang.reflect.*;

/**
 * @author deepak.singhvi
 *
 */
public class InvocationHandlerProxy implements 
      InvocationHandler {

private Object obj;

public static Object newInstance(Object obj) {
  return Proxy.newProxyInstance(obj.getClass()
  .getClassLoader(), obj.getClass().getInterfaces(),
  new InvocationHandlerProxy(obj));
}

public InvocationHandlerProxy(Object obj) {
 this.obj = obj;
}

public Object invoke(Object proxy, Method m, Object[] args)
 throws Throwable {
 Object result;
  try {
    result = m.invoke(obj, args);
  } catch (InvocationTargetException e) {
    throw e.getTargetException();
  } catch (Exception e) {
    throw new RuntimeException("unexpected invocation exception: "
 + e.getMessage());
  } 
  return result;
}
}


For every gui component, for e.g. button instead of overriding actionListener for some action event we can call.

ActionListener foo = (ActionListener) InvocationHandlerProxy.newInstance(new FooWithoutArgActionListener(this,"methodWithoutArgument",new Class[0]));

Listener class would implement ActionListener as usual but the method to be called when a
specific event is raised will be defined in the same class where listener is created, without creating lister anonymously.

public class FooWithoutArgActionListener 
implements ActionListener {
  Method method;
  Object parent;
  public FooWithoutArgActionListener(Object classObject, 
    String methodName, Class[] parameterTypes ){
    method = getTargetMethod(classObject, methodName, parameterTypes);
    this.parent = classObject;
}

@Override
public void actionPerformed(ActionEvent e) {
 ....
  method.invoke(parent, new Object[0]);
 ...
}

private Method getTargetMethod(Object target,
  String methodName, Class[] parameter) 
{
// find the target method which should get 
// executed when actionPerformed is called.
}


Click here to get the Complete example

Heroku Custom Trust Store for SSL Handshake

  Working with Heroku for deploying apps (java, nodejs, etc..) is made very easy but while integrating one of the service ho...