Skip to content

pathikrit/Kotha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Kotha solves the important problem of designing strongly typed APIs over the web in Java. Kotha in Bengali means "talk".

Problem description:
You are writing your own server in Java that talks to a Java client. You want to provide some APIs that the clients can call on the server.
The server is running on another machine. How do you make API calls easily from the client to the server?

Current solutions:
1) Use some API description language like WSDL - This works but not only you have to mess with XML files and learn a whole set of new tools and DSLs, the auto generated code looks ugly and generates tons of boiler plate codes and files. Also there is no easy way to talk to multiple different servers
2) Use the Java RMI framework - This again works and guarantees type safety but you have to mess with rmiregistries and security settings and there is no easy way to connect to different servers or add new API methods
3) Roll something of your own - This usually involves creating some kind function map from special opcodes sent over the wire to actual server side API implementations. This has the opposite problem of the above - its much smaller and easier to maintain and no DSLs involved and you can handle multiple servers but its very hard to do type checking or add new API methods
4) Use Hessian - This is cool and very similar to the ideas presented here but you need to run your own servlet container to run the server and can't simply run a single simple Java server/client program
5) Use KryoNet's ObjectSpace RMI library - This is probably the best current solution out there but you still have to write Client side boiler plate code to register your messages
6) Use akka.io - Nice clean syntax but no type safety

My solution:
Here are the steps to add a new API method of your own using the Kotha framework. Let's say you want to add a new API "boolean isEven(int x)" which returns true iff x is even:
1) Add the "Future<Boolean> isEven(Integer)" to the API.java interface file in Common module
2) Implement it in the Server module
3) There is no step 3! You are done and you can make a strongly typed API call over the network from the Client.java to Server.java now!

Simplicity:
The source code here provides an example usage of Kotha. To test it yourself, run Server1.java and Server2.java and then run Client.java.
The source code is itself only 300 lines long, spread over just 3 Java files and has only 2 public methods you can call:
1) KothaServer.java - call KothaServer.start(port) to start a server (call from your Server class)
2) KothaClient.java - call KothaClient.connecTo(servers...) to connect to servers (call from your Client class)
1) KothaCommon.java - An internal utility class containing common methods used by KothaServer and KothaClient

TODO:
1) Stress testing frameworks
2) Unit tests
3) Use Guice to bind APIs instead for Client?
4) Get rid of Futures - introduce sync and async. On client side it would be API sync = async
5) Handle server going down
6) Javadoc
7) Implement Cacheable annotation

About

A simple Java framework to write strongly typed remote APIs

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages