I want to build a very simple servlet that will read the http posted data at a single url endpoint like:
http://localhost:8080/post
It will then have to:
1. authenicate the request based appId and userId that was posted
2. if authentication went ok, gather the form fields and save it to the db.
This will be a very simple servlet, don’t really any heavy frameworks for this.
I will have maybe 10K-50K users, and I don’t want to perform a database lookup to authenticate.
I just want to store this information in a dictionary/hash, this has to be thread safe.
If a new user registers, I have to be able to invalidate this in-memory dictionary and reload the app/user data from mysql, but only if a new person registers.
The specifications require this to work under heavy load, up to 5K requests per second.
I’m new to java and just experimenting on how this could be done in java/servlets.
How could I have this dictionary as a static type field accessible to my servlet’s doGet method, yet be able to re-initialize it when a new user joins.
Here is a simple servlet that I want to build on top of:
package test;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
}
Please don’t assume much from me, code snippets are greatly appreciated on how to setup this dictionary.
The key thing I want to understand is how to setup this dictionary that can be used in my servlet to response to requests and perform lookups, and to initially load it from mysql and then be able to re-initialize it in a thread-safe manner.
Your post is a bit light on what specifically you’re having trouble with but the ConcurrentHashMap class will probably be of interest to you.
This can be a static field and you can then store and retrieve the information you need using the
getandputmethods.