A good design for using servlet and Java annotations in the game server

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.