Java Socket RPC protocol -


i've been asking questions adapting command protocol use in client server environment. however, after experimentation, have come conclusion not work me. not designed scenario. i'm @ loose end.

i have implemented sort of rpc mechanism before whereby had class entitled "operation". had enum entitled "action" contained names actions invoked on server.

now, in old project, every time client wanted invoke action on server, create instance of 'operation' , set action variable value "action" enum. example

operation serveroptoinvoke = new operation();
serveroptoinvoke.setaction(action.create_time_table);
serveroptoinvoke.setparameters(map params);
serverreply reply = networkmanager.sendoperation(serveroptoinvoke);
...

on server side, had perform horrible task of determining method invoke examining 'action' enum value load of 'if/else' statements. when match found, call appropriate method.

the problem was messy, hard maintain , bad design.

my question - there sort of pattern can follow implement nice, clean , maintainable rpc mechanism on tcp socket in java? rmi no go me here client (android) doesn't support rmi. i've exhausted avenues @ stage. other option maybe rest service. advice helpful.

thank regards

probably easiest solution loosely follow path of rmi.

you start out interface , implementation:

interface fooservice  {   bar dothis( string param );   string dothat( bar param ); }  class fooserviceimpl implements fooservice {   ... } 

you deploy interface both sides , implementation server side only.

then client object, create dynamic proxy. invocation handler nothing else serialize service classname, method name , parameters , send server (initially can use objectoutputstream can use alternative serialization techniques, xstream example).

the server listener takes request , executes using reflection, sends response back.

the implementation easy , transparent both sides, major caveat being services singletons.

i can include more implementation detail if need, general idea follow if had implement that.

having said that, i'd search bit more existing solution, webservices or similar.

update: ordinary (local) invocation handler do.

class myhandler implements invocationhandler {      private object serviceobject;      @override     public object invoke(object proxy, method method, object[] args)             throws throwable {         return method.invoke(serviceobject, args);     } } 

where serviceobject service implementation object wrapped handler.

this have cut in half, , instead of calling method, need send following server:

  1. the full name of interface (or other value uniquely identifies service interface)
  2. the name of method.
  3. the names of parameter types method expects.
  4. the args array.

the server side have to:

  1. find implementation interface (the easiest way have sort of map keys interface names , values implementation singleton instance)
  2. find method, using class.getmethod( name, paramtypes );
  3. execute method calling method.invoke(serviceobject, args); , send return value back.

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -