带有内存字典的 Java servlet 是线程安全的,并且可以重新初始化

发布于 2024-12-12 18:19:42 字数 1180 浏览 1 评论 0原文

我想构建一个非常简单的 servlet,它将在单个 url 端点读取 http 发布的数据,例如:

http://localhost:8080/post

然后它必须:

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.

这将是一个非常简单的 servlet,实际上不需要任何重型框架。

我可能有 10K-50K 用户,并且我不想执行数据库查找来进行身份验证。

我只想将此信息存储在字典/哈希中,这必须是线程安全的。

如果新用户注册,我必须能够使内存中的字典无效并从 mysql 重新加载应用程序/用户数据,但前提是新用户注册。

规范要求其在重负载下工作,每秒最多 5K 请求。

我是 java 新手,只是尝试如何在 java/servlet 中完成此操作。

我怎样才能将此字典作为静态类型字段来访问我的 servlet 的 doGet 方法,但又能够在新用户加入时重新初始化它。

这是一个我想在其上构建的简单 servlet:

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();
  }
}

请不要对我做太多假设,非常感谢有关如何设置此字典的代码片段。

我想了解的关键是如何设置可以在我的 servlet 中使用的字典来响应请求并执行查找,并最初从 mysql 加载它,然后能够以线程安全的方式重新初始化它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-12-19 18:19:45

您的帖子对您​​遇到的具体问题有点简单,但是 ConcurrentHashMap 类感兴趣。

这可以是静态字段,然后您可以使用 getput 方法存储和检索所需的信息。

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 get and put methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文