SNS games are basically the use of HTTP short connection, using Java to develop the server can use the Servlet+tomcat very easy to start the service side of the architecture. Here is a good use of a servlet design, I have seen a lot of HTTP request-based game server using Struts, Spring, hibernate and so on, in fact, I feel for the game to use these things very cumbersome, if the development of Java It makes sense for web apps to use SSH.
When using a servlet, we can create only one entry for all requests in the servlet left game, then use annotations to identify the method, put it in the map using reflection to collect annotations when the program starts, and use the Key= method name, Value=method object, Also define a protocol number class, the player each operation corresponds to a protocol number, in order to key= protocol number, Value=method object to the map, according to the client pass the protocol number or method name to map to remove Methodobject to execute, The obvious benefit of doing this is that all requests are handled uniformly in a servlet, so that the exception handling can be unified and the structure is very clear.
Here is the code that collects the annotation methods:
1. Create annotations
@Target (Elementtype.method) @Retention (retentionpolicy.runtime) @Documentedpublic @interface Requestmethod {String Name () Default "";}
2, the annotation method (the following method is placed in the Remoteservice class)
@RequestMethod (name = "Login") public map<object, object> login (map<string, string> params) throws Exception {return userservice.login (params);}
3, collect the annotated method put in map
private static void Processremoteservice () {try {class<?> clazz = remoteservice.getinstance (). GetClass (); for ( Method M:clazz.getmethods ()) {Requestmethod Ann = m.getannotation (Requestmethod.class); if (ann! = null) {//Check the method's parameter type and return Type//....//...if (Ann.name (). Length () = = 0) {}else {Globalservice.puttoremotemap (Ann.name (), m);}}} catch (SecurityException e) {e.printstacktrace ();}}
In the servlet, the login Method object can be executed based on the login string sent by the client.
A good design for using servlet and Java annotations in the game server