A good design for using Servlet and Java annotations on the game server

Source: Internet
Author: User

A good design for using Servlet and Java annotations on the game server

SNS games basically use HTTP short connections. When using Java to develop the server, you can use Servlet + Tomcat to easily start the server. Here we will introduce a better design with Servlet. I have also seen many HTTP-based game servers using Struts, Spring, and Hibernate, in fact, I feel that it is complicated for games to use these things. It is reasonable to use SSH for Java Web applications.

When using Servlet, we can create only one Servlet portal for all requests in the left game, and then use annotations to identify the methods. When the program starts, we use reflection to collect annotations and place them in Map, you can use the key = Method name, value = Method Object Method, or define another Protocol Number class. Each player operation corresponds to a protocol number, with the key = Protocol number, value = Method Object is saved to Map, and MethodObject is retrieved from Map based on the protocol number or Method name passed by the client, the obvious advantage of doing so is that all requests are processed in a Servlet in a unified manner, so that exception handling can be unified and the structure is very clear.

The following code collects annotation methods:

1. Create Annotation

 

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestMethod {String name() default "";}
2. annotation method (the following method is placed in the RemoteService class)

 

 

@RequestMethod(name = "login")public Map
 
   login(Map
  
    params) throws Exception {return userService.login(params);}
  
 
3. Collect annotated methods and put them 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 parameter type and return type of the method //.... //... if (ann. name (). length () = 0) {} else {GlobalService. putToRemoteMap (ann. name (), m) ;}}} catch (SecurityException e) {e. printStackTrace ();}}
In the Servlet, you can retrieve the login Method Object and execute it based on the Login string passed by the client.

 


 

Related Article

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.